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

Commit 28dafd2

Browse files
committed
Introduce a ComposerManager class to simplify the commands
1 parent b61f637 commit 28dafd2

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
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Filesystem\Filesystem;
2929
use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException;
3030
use Symfony\Installer\Exception\AbortException;
31+
use Symfony\Installer\Manager\ComposerManager;
3132

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

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

384374
return $this;
385375
}
@@ -418,17 +408,13 @@ protected function createGitIgnore()
418408
*/
419409
protected function getInstalledSymfonyVersion()
420410
{
421-
$composer = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
411+
$symfonyVersion = $this->composerManager->getPackageVersion('symfony/symfony');
422412

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

429-
return $package['version'];
430-
}
431-
}
417+
return $symfonyVersion;
432418
}
433419

434420
/**
@@ -520,7 +506,6 @@ protected function getExecutedCommand()
520506
}
521507

522508
$commandName = $this->getName();
523-
$commandArguments = '';
524509

525510
if ('new' === $commandName) {
526511
$commandArguments = sprintf('%s %s', $this->projectName, ('latest' !== $this->version) ? $this->version : '');
@@ -601,107 +586,6 @@ protected function getUrlContents($url)
601586
return $client->get($url)->getBody()->getContents();
602587
}
603588

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

0 commit comments

Comments
 (0)