Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

Commit ff32aac

Browse files
committed
feature #282 Introduce a ComposerManager class to simplify the commands (javiereguiluz)
This PR was squashed before being merged into the 1.0-dev branch (closes #282). Discussion ---------- Introduce a ComposerManager class to simplify the commands This replaces #279. Commits ------- 28dafd2 Introduce a ComposerManager class to simplify the commands
2 parents 9173137 + 28dafd2 commit ff32aac

File tree

5 files changed

+227
-197
lines changed

5 files changed

+227
-197
lines changed

src/Symfony/Installer/DemoCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Installer\Exception\AbortException;
18+
use Symfony\Installer\Manager\ComposerManager;
1819

1920
/**
2021
* This command creates a full-featured Symfony demo application.
@@ -60,6 +61,8 @@ protected function initialize(InputInterface $input, OutputInterface $output)
6061
$this->projectDir = $this->fs->isAbsolutePath($directory) ? $directory : getcwd().DIRECTORY_SEPARATOR.$directory;
6162
$this->projectName = basename($directory);
6263
}
64+
65+
$this->composerManager = new ComposerManager($this->projectDir);
6366
}
6467

6568
/**
@@ -75,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7578
->download()
7679
->extract()
7780
->cleanUp()
78-
->updateComposerJson()
81+
->updateComposerConfig()
7982
->createGitIgnore()
8083
->checkSymfonyRequirements()
8184
->displayInstallationResult()

src/Symfony/Installer/DownloadCommand.php

Lines changed: 11 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Filesystem\Filesystem;
3030
use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException;
3131
use Symfony\Installer\Exception\AbortException;
32+
use Symfony\Installer\Manager\ComposerManager;
3233

3334
/**
3435
* Abstract command used by commands which download and extract compressed Symfony files.
@@ -83,6 +84,9 @@ abstract class DownloadCommand extends Command
8384
*/
8485
protected $requirementsErrors = array();
8586

87+
/** @var ComposerManager */
88+
protected $composerManager;
89+
8690
/**
8791
* Returns the type of the downloaded application in a human readable format.
8892
* It's mainly used to display readable error messages.
@@ -364,23 +368,9 @@ private function getSymfonyRequirementsFilePath()
364368
*
365369
* @return $this
366370
*/
367-
protected function updateComposerJson()
371+
protected function updateComposerConfig()
368372
{
369-
$composerConfig = $this->getProjectComposerConfig();
370-
371-
if (isset($composerConfig['config']['platform']['php'])) {
372-
unset($composerConfig['config']['platform']['php']);
373-
374-
if (empty($composerConfig['config']['platform'])) {
375-
unset($composerConfig['config']['platform']);
376-
}
377-
378-
if (empty($composerConfig['config'])) {
379-
unset($composerConfig['config']);
380-
}
381-
}
382-
383-
$this->saveProjectComposerConfig($composerConfig);
373+
$this->composerManager->initializeProjectConfig();
384374

385375
return $this;
386376
}
@@ -419,17 +409,13 @@ protected function createGitIgnore()
419409
*/
420410
protected function getInstalledSymfonyVersion()
421411
{
422-
$composer = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
412+
$symfonyVersion = $this->composerManager->getPackageVersion('symfony/symfony');
423413

424-
foreach ($composer['packages'] as $package) {
425-
if ('symfony/symfony' === $package['name']) {
426-
if ('v' === substr($package['version'], 0, 1)) {
427-
return substr($package['version'], 1);
428-
};
414+
if (!empty($symfonyVersion) && 'v' === substr($symfonyVersion, 0, 1)) {
415+
return substr($symfonyVersion, 1);
416+
};
429417

430-
return $package['version'];
431-
}
432-
}
418+
return $symfonyVersion;
433419
}
434420

435421
/**
@@ -501,7 +487,6 @@ protected function getExecutedCommand()
501487
}
502488

503489
$commandName = $this->getName();
504-
$commandArguments = '';
505490

506491
if ('new' === $commandName) {
507492
$commandArguments = sprintf('%s %s', $this->projectName, ('latest' !== $this->version) ? $this->version : '');
@@ -582,107 +567,6 @@ protected function getUrlContents($url)
582567
return $client->get($url)->getBody()->getContents();
583568
}
584569

585-
/**
586-
* It returns the project's Composer config as a PHP array.
587-
*
588-
* @return $this|array
589-
*/
590-
protected function getProjectComposerConfig()
591-
{
592-
$composerJsonFilepath = $this->projectDir.'/composer.json';
593-
594-
if (!is_writable($composerJsonFilepath)) {
595-
if ($this->output->isVerbose()) {
596-
$this->output->writeln(sprintf(
597-
" <comment>[WARNING]</comment> Project's Composer config cannot be updated because\n".
598-
" the <comment>%s</comment> file is not writable.\n",
599-
$composerJsonFilepath
600-
));
601-
}
602-
603-
return $this;
604-
}
605-
606-
return json_decode(file_get_contents($composerJsonFilepath), true);
607-
}
608-
609-
/**
610-
* It saves the given PHP array as the project's Composer config. In addition
611-
* to JSON-serializing the contents, it synchronizes the composer.lock file to
612-
* avoid out-of-sync Composer errors.
613-
*
614-
* @param array $config
615-
*/
616-
protected function saveProjectComposerConfig(array $config)
617-
{
618-
$composerJsonFilepath = $this->projectDir.'/composer.json';
619-
$this->fs->dumpFile($composerJsonFilepath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
620-
621-
$this->syncComposerLockFile();
622-
}
623-
624-
/**
625-
* Updates the hash values stored in composer.lock to avoid out-of-sync
626-
* problems when the composer.json file contents are changed.
627-
*/
628-
private function syncComposerLockFile()
629-
{
630-
$composerJsonFileContents = file_get_contents($this->projectDir.'/composer.json');
631-
$composerLockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
632-
633-
if (array_key_exists('hash', $composerLockFileContents)) {
634-
$composerLockFileContents['hash'] = md5($composerJsonFileContents);
635-
}
636-
637-
if (array_key_exists('content-hash', $composerLockFileContents)) {
638-
$composerLockFileContents['content-hash'] = $this->getComposerContentHash($composerJsonFileContents);
639-
}
640-
641-
$this->fs->dumpFile($this->projectDir.'/composer.lock', json_encode($composerLockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
642-
}
643-
644-
/**
645-
* Returns the md5 hash of the sorted content of the composer file.
646-
*
647-
* @see https://github.com/composer/composer/blob/master/src/Composer/Package/Locker.php (getContentHash() method)
648-
*
649-
* @param string $composerJsonFileContents The contents of the composer.json file.
650-
*
651-
* @return string The hash of the composer file content.
652-
*/
653-
private function getComposerContentHash($composerJsonFileContents)
654-
{
655-
$composerConfig = json_decode($composerJsonFileContents, true);
656-
657-
$relevantKeys = array(
658-
'name',
659-
'version',
660-
'require',
661-
'require-dev',
662-
'conflict',
663-
'replace',
664-
'provide',
665-
'minimum-stability',
666-
'prefer-stable',
667-
'repositories',
668-
'extra',
669-
);
670-
671-
$relevantComposerConfig = array();
672-
673-
foreach (array_intersect($relevantKeys, array_keys($composerConfig)) as $key) {
674-
$relevantComposerConfig[$key] = $composerConfig[$key];
675-
}
676-
677-
if (isset($composerConfig['config']['platform'])) {
678-
$relevantComposerConfig['config']['platform'] = $composerConfig['config']['platform'];
679-
}
680-
681-
ksort($relevantComposerConfig);
682-
683-
return md5(json_encode($relevantComposerConfig));
684-
}
685-
686570
/**
687571
* Enables the signal handler.
688572
*

0 commit comments

Comments
 (0)