|
29 | 29 | use Symfony\Component\Filesystem\Filesystem;
|
30 | 30 | use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException;
|
31 | 31 | use Symfony\Installer\Exception\AbortException;
|
| 32 | +use Symfony\Installer\Manager\ComposerManager; |
32 | 33 |
|
33 | 34 | /**
|
34 | 35 | * Abstract command used by commands which download and extract compressed Symfony files.
|
@@ -83,6 +84,9 @@ abstract class DownloadCommand extends Command
|
83 | 84 | */
|
84 | 85 | protected $requirementsErrors = array();
|
85 | 86 |
|
| 87 | + /** @var ComposerManager */ |
| 88 | + protected $composerManager; |
| 89 | + |
86 | 90 | /**
|
87 | 91 | * Returns the type of the downloaded application in a human readable format.
|
88 | 92 | * It's mainly used to display readable error messages.
|
@@ -364,23 +368,9 @@ private function getSymfonyRequirementsFilePath()
|
364 | 368 | *
|
365 | 369 | * @return $this
|
366 | 370 | */
|
367 |
| - protected function updateComposerJson() |
| 371 | + protected function updateComposerConfig() |
368 | 372 | {
|
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(); |
384 | 374 |
|
385 | 375 | return $this;
|
386 | 376 | }
|
@@ -419,17 +409,13 @@ protected function createGitIgnore()
|
419 | 409 | */
|
420 | 410 | protected function getInstalledSymfonyVersion()
|
421 | 411 | {
|
422 |
| - $composer = json_decode(file_get_contents($this->projectDir.'/composer.lock'), true); |
| 412 | + $symfonyVersion = $this->composerManager->getPackageVersion('symfony/symfony'); |
423 | 413 |
|
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 | + }; |
429 | 417 |
|
430 |
| - return $package['version']; |
431 |
| - } |
432 |
| - } |
| 418 | + return $symfonyVersion; |
433 | 419 | }
|
434 | 420 |
|
435 | 421 | /**
|
@@ -501,7 +487,6 @@ protected function getExecutedCommand()
|
501 | 487 | }
|
502 | 488 |
|
503 | 489 | $commandName = $this->getName();
|
504 |
| - $commandArguments = ''; |
505 | 490 |
|
506 | 491 | if ('new' === $commandName) {
|
507 | 492 | $commandArguments = sprintf('%s %s', $this->projectName, ('latest' !== $this->version) ? $this->version : '');
|
@@ -582,107 +567,6 @@ protected function getUrlContents($url)
|
582 | 567 | return $client->get($url)->getBody()->getContents();
|
583 | 568 | }
|
584 | 569 |
|
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 |
| - |
686 | 570 | /**
|
687 | 571 | * Enables the signal handler.
|
688 | 572 | *
|
|
0 commit comments