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

Commit 627b1f0

Browse files
committed
feature #256 Remove the "config.platform.php" option when installing the Symfony Demo app (javiereguiluz)
This PR was squashed before being merged into the 1.0-dev branch (closes #256). Discussion ---------- Remove the "config.platform.php" option when installing the Symfony Demo app This fixes #255. Commits ------- 842cb4f Remove the "config.platform.php" option when installing the Symfony Demo app
2 parents bce5ac4 + 842cb4f commit 627b1f0

File tree

4 files changed

+149
-97
lines changed

4 files changed

+149
-97
lines changed

src/Symfony/Installer/DemoCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7575
->download()
7676
->extract()
7777
->cleanUp()
78+
->updateComposerJson()
7879
->createGitIgnore()
7980
->checkSymfonyRequirements()
8081
->displayInstallationResult()

src/Symfony/Installer/DownloadCommand.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,33 @@ protected function checkSymfonyRequirements()
325325
return $this;
326326
}
327327

328+
/**
329+
* Updates the composer.json file to provide better values for some of the
330+
* default configuration values.
331+
*
332+
* @return $this
333+
*/
334+
protected function updateComposerJson()
335+
{
336+
$composerConfig = $this->getProjectComposerConfig();
337+
338+
if (isset($composerConfig['config']['platform']['php'])) {
339+
unset($composerConfig['config']['platform']['php']);
340+
341+
if (empty($composerConfig['config']['platform'])) {
342+
unset($composerConfig['config']['platform']);
343+
}
344+
345+
if (empty($composerConfig['config'])) {
346+
unset($composerConfig['config']);
347+
}
348+
}
349+
350+
$this->saveProjectComposerConfig($composerConfig);
351+
352+
return $this;
353+
}
354+
328355
/**
329356
* Creates the appropriate .gitignore file for a Symfony project if it doesn't exist.
330357
*
@@ -544,6 +571,107 @@ protected function getUrlContents($url)
544571
return $client->get($url)->getBody()->getContents();
545572
}
546573

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

src/Symfony/Installer/NewCommand.php

Lines changed: 9 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -337,48 +337,22 @@ protected function updateParameters()
337337
*/
338338
protected function updateComposerJson()
339339
{
340-
$filename = $this->projectDir.'/composer.json';
340+
parent::updateComposerJson();
341341

342-
if (!is_writable($filename)) {
343-
if ($this->output->isVerbose()) {
344-
$this->output->writeln(sprintf(
345-
" <comment>[WARNING]</comment> Project name cannot be configured because\n".
346-
" the <comment>%s</comment> file is not writable.\n",
347-
$filename
348-
));
349-
}
350-
351-
return $this;
352-
}
353-
354-
$contents = json_decode(file_get_contents($filename), true);
342+
$composerConfig = $this->getProjectComposerConfig();
355343

356-
$contents['name'] = $this->generateComposerProjectName();
357-
$contents['license'] = 'proprietary';
344+
$composerConfig['name'] = $this->generateComposerProjectName();
345+
$composerConfig['license'] = 'proprietary';
358346

359-
if (isset($contents['description'])) {
360-
unset($contents['description']);
347+
if (isset($composerConfig['description'])) {
348+
unset($composerConfig['description']);
361349
}
362350

363-
if (isset($contents['config']['platform']['php'])) {
364-
unset($contents['config']['platform']['php']);
365-
366-
if (empty($contents['config']['platform'])) {
367-
unset($contents['config']['platform']);
368-
}
369-
370-
if (empty($contents['config'])) {
371-
unset($contents['config']);
372-
}
373-
}
374-
375-
if (isset($contents['extra']['branch-alias'])) {
376-
unset($contents['extra']['branch-alias']);
351+
if (isset($composerConfig['extra']['branch-alias'])) {
352+
unset($composerConfig['extra']['branch-alias']);
377353
}
378354

379-
file_put_contents($filename, json_encode($contents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
380-
381-
$this->syncComposerLockFile();
355+
$this->saveProjectComposerConfig($composerConfig);
382356

383357
return $this;
384358
}
@@ -441,66 +415,4 @@ protected function getRemoteFileUrl()
441415
{
442416
return 'http://symfony.com/download?v=Symfony_Standard_Vendors_'.$this->version;
443417
}
444-
445-
/**
446-
* Updates the hash values stored in composer.lock to avoid out-of-sync
447-
* problems when the composer.json file contents are changed.
448-
*/
449-
private function syncComposerLockFile()
450-
{
451-
$composerJsonFileContents = file_get_contents($this->projectDir.'/composer.json');
452-
$composerLockFileContents = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true);
453-
454-
if (array_key_exists('hash', $composerLockFileContents)) {
455-
$composerLockFileContents['hash'] = md5($composerJsonFileContents);
456-
}
457-
458-
if (array_key_exists('content-hash', $composerLockFileContents)) {
459-
$composerLockFileContents['content-hash'] = $this->getComposerContentHash($composerJsonFileContents);
460-
}
461-
462-
file_put_contents($this->projectDir.'/composer.lock', json_encode($composerLockFileContents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."\n");
463-
}
464-
465-
/**
466-
* Returns the md5 hash of the sorted content of the composer file.
467-
*
468-
* @see https://github.com/composer/composer/blob/master/src/Composer/Package/Locker.php (getContentHash() method)
469-
*
470-
* @param string $composerJsonFileContents The contents of the composer.json file.
471-
*
472-
* @return string The hash of the composer file content.
473-
*/
474-
private function getComposerContentHash($composerJsonFileContents)
475-
{
476-
$content = json_decode($composerJsonFileContents, true);
477-
478-
$relevantKeys = array(
479-
'name',
480-
'version',
481-
'require',
482-
'require-dev',
483-
'conflict',
484-
'replace',
485-
'provide',
486-
'minimum-stability',
487-
'prefer-stable',
488-
'repositories',
489-
'extra',
490-
);
491-
492-
$relevantContent = array();
493-
494-
foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
495-
$relevantContent[$key] = $content[$key];
496-
}
497-
498-
if (isset($content['config']['platform'])) {
499-
$relevantContent['config']['platform'] = $content['config']['platform'];
500-
}
501-
502-
ksort($relevantContent);
503-
504-
return md5(json_encode($relevantContent));
505-
}
506418
}

tests/Symfony/Installer/Tests/IntegrationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function testDemoApplicationInstallation()
5050

5151
$output = $this->runCommand('php app/console --version', $projectDir);
5252
$this->assertRegExp('/Symfony version 2\.\d+\.\d+(-DEV)? - app\/dev\/debug/', $output);
53+
54+
$composerConfig = json_decode(file_get_contents($projectDir.'/composer.json'), true);
55+
56+
$this->assertArrayNotHasKey('platform', $composerConfig['config'], 'The composer.json file does not define any platform configuration.');
5357
}
5458

5559
/**
@@ -75,6 +79,13 @@ public function testSymfonyInstallation($versionToInstall, $messageRegexp, $vers
7579
}
7680

7781
$this->assertRegExp($versionRegexp, $output);
82+
83+
$composerConfig = json_decode(file_get_contents($projectDir.'/composer.json'), true);
84+
$this->assertArrayNotHasKey(
85+
isset($composerConfig['config']) ? 'platform' : 'config',
86+
isset($composerConfig['config']) ? $composerConfig['config'] : $composerConfig,
87+
'The composer.json file does not define any platform configuration.'
88+
);
7889
}
7990

8091
public function testSymfonyInstallationInCurrentDirectory()

0 commit comments

Comments
 (0)