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

Introduce a ComposerManager class to simplify the commands #282

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Installer/DemoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Installer\Exception\AbortException;
use Symfony\Installer\Manager\ComposerManager;

/**
* This command creates a full-featured Symfony demo application.
Expand Down Expand Up @@ -60,6 +61,8 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$this->projectDir = $this->fs->isAbsolutePath($directory) ? $directory : getcwd().DIRECTORY_SEPARATOR.$directory;
$this->projectName = basename($directory);
}

$this->composerManager = new ComposerManager($this->projectDir);
}

/**
Expand All @@ -75,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->download()
->extract()
->cleanUp()
->updateComposerJson()
->updateComposerConfig()
->createGitIgnore()
->checkSymfonyRequirements()
->displayInstallationResult()
Expand Down
138 changes: 11 additions & 127 deletions src/Symfony/Installer/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException;
use Symfony\Installer\Exception\AbortException;
use Symfony\Installer\Manager\ComposerManager;

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

/** @var ComposerManager */
protected $composerManager;

/**
* Returns the type of the downloaded application in a human readable format.
* It's mainly used to display readable error messages.
Expand Down Expand Up @@ -363,23 +367,9 @@ private function getSymfonyRequirementsFilePath()
*
* @return $this
*/
protected function updateComposerJson()
protected function updateComposerConfig()
{
$composerConfig = $this->getProjectComposerConfig();

if (isset($composerConfig['config']['platform']['php'])) {
unset($composerConfig['config']['platform']['php']);

if (empty($composerConfig['config']['platform'])) {
unset($composerConfig['config']['platform']);
}

if (empty($composerConfig['config'])) {
unset($composerConfig['config']);
}
}

$this->saveProjectComposerConfig($composerConfig);
$this->composerManager->initializeProjectConfig();

return $this;
}
Expand Down Expand Up @@ -418,17 +408,13 @@ protected function createGitIgnore()
*/
protected function getInstalledSymfonyVersion()
{
$composer = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
$symfonyVersion = $this->composerManager->getPackageVersion('symfony/symfony');

foreach ($composer['packages'] as $package) {
if ('symfony/symfony' === $package['name']) {
if ('v' === substr($package['version'], 0, 1)) {
return substr($package['version'], 1);
};
if (!empty($symfonyVersion) && 'v' === substr($symfonyVersion, 0, 1)) {
return substr($symfonyVersion, 1);
};

return $package['version'];
}
}
return $symfonyVersion;
}

/**
Expand Down Expand Up @@ -520,7 +506,6 @@ protected function getExecutedCommand()
}

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

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

/**
* It returns the project's Composer config as a PHP array.
*
* @return $this|array
*/
protected function getProjectComposerConfig()
{
$composerJsonFilepath = $this->projectDir.'/composer.json';

if (!is_writable($composerJsonFilepath)) {
if ($this->output->isVerbose()) {
$this->output->writeln(sprintf(
" <comment>[WARNING]</comment> Project's Composer config cannot be updated because\n".
" the <comment>%s</comment> file is not writable.\n",
$composerJsonFilepath
));
}

return $this;
}

return json_decode(file_get_contents($composerJsonFilepath), true);
}

/**
* It saves the given PHP array as the project's Composer config. In addition
* to JSON-serializing the contents, it synchronizes the composer.lock file to
* avoid out-of-sync Composer errors.
*
* @param array $config
*/
protected function saveProjectComposerConfig(array $config)
{
$composerJsonFilepath = $this->projectDir.'/composer.json';
$this->fs->dumpFile($composerJsonFilepath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");

$this->syncComposerLockFile();
}

/**
* Updates the hash values stored in composer.lock to avoid out-of-sync
* problems when the composer.json file contents are changed.
*/
private function syncComposerLockFile()
{
$composerJsonFileContents = file_get_contents($this->projectDir.'/composer.json');
$composerLockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);

if (array_key_exists('hash', $composerLockFileContents)) {
$composerLockFileContents['hash'] = md5($composerJsonFileContents);
}

if (array_key_exists('content-hash', $composerLockFileContents)) {
$composerLockFileContents['content-hash'] = $this->getComposerContentHash($composerJsonFileContents);
}

$this->fs->dumpFile($this->projectDir.'/composer.lock', json_encode($composerLockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
}

/**
* Returns the md5 hash of the sorted content of the composer file.
*
* @see https://github.com/composer/composer/blob/master/src/Composer/Package/Locker.php (getContentHash() method)
*
* @param string $composerJsonFileContents The contents of the composer.json file.
*
* @return string The hash of the composer file content.
*/
private function getComposerContentHash($composerJsonFileContents)
{
$composerConfig = json_decode($composerJsonFileContents, true);

$relevantKeys = array(
'name',
'version',
'require',
'require-dev',
'conflict',
'replace',
'provide',
'minimum-stability',
'prefer-stable',
'repositories',
'extra',
);

$relevantComposerConfig = array();

foreach (array_intersect($relevantKeys, array_keys($composerConfig)) as $key) {
$relevantComposerConfig[$key] = $composerConfig[$key];
}

if (isset($composerConfig['config']['platform'])) {
$relevantComposerConfig['config']['platform'] = $composerConfig['config']['platform'];
}

ksort($relevantComposerConfig);

return md5(json_encode($relevantComposerConfig));
}

/**
* Enables the signal handler.
*
Expand Down
Loading