|
8 | 8 | namespace Magento\CloudPatches; |
9 | 9 |
|
10 | 10 | use Composer\Composer; |
| 11 | +use Composer\InstalledVersions; |
11 | 12 | use Magento\CloudPatches\Command; |
12 | 13 | use Psr\Container\ContainerInterface; |
| 14 | +use Symfony\Component\Console\Command\Command as SymfonyCommand; |
| 15 | +use Symfony\Component\Console\Application as SymfonyApplication; |
| 16 | +use Throwable; |
13 | 17 |
|
14 | 18 | /** |
15 | 19 | * @inheritDoc |
16 | 20 | */ |
17 | | -class ApplicationEce extends \Symfony\Component\Console\Application |
| 21 | +class ApplicationEce extends SymfonyApplication |
18 | 22 | { |
| 23 | + /** |
| 24 | + * Default package name to look for in InstalledVersions and composer.json. |
| 25 | + */ |
| 26 | + private const DEFAULT_PACKAGE_NAME = 'magento/magento-cloud-patches'; |
| 27 | + |
19 | 28 | /** |
20 | 29 | * @var ContainerInterface |
21 | 30 | */ |
22 | | - private $container; |
| 31 | + private ContainerInterface $container; |
23 | 32 |
|
24 | 33 | /** |
25 | | - * @param ContainerInterface $container |
| 34 | + * Command classes registered by default in the application. |
| 35 | + * |
| 36 | + * @var class-string<SymfonyCommand>[] |
| 37 | + */ |
| 38 | + private const DEFAULT_COMMAND_CLASSES = [ |
| 39 | + Command\Ece\Apply::class, |
| 40 | + Command\Ece\Revert::class, |
| 41 | + Command\Status::class, |
| 42 | + Command\Verify::class |
| 43 | + ]; |
| 44 | + |
| 45 | + /** |
| 46 | + * Initialize the application and resolve its name and version. |
| 47 | + * |
| 48 | + * Version information is resolved from InstalledVersions, |
| 49 | + * composer.json, or Composer package metadata. |
| 50 | + * |
| 51 | + * @param ContainerInterface $container Application container. |
26 | 52 | */ |
27 | 53 | public function __construct(ContainerInterface $container) |
28 | 54 | { |
29 | | - $this->container = $container; |
| 55 | + $this->container = $container; |
| 56 | + [$name, $version] = $this->resolveApplicationMetadata($container); |
30 | 57 |
|
31 | | - parent::__construct( |
32 | | - $container->get(Composer::class)->getPackage()->getPrettyName(), |
33 | | - $container->get(Composer::class)->getPackage()->getPrettyVersion() |
34 | | - ); |
| 58 | + parent::__construct($name, $version); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Read composer.json safely. |
| 63 | + * This method attempts to read the composer.json file and decode its contents. |
| 64 | + * It handles potential errors gracefully, returning null if the file cannot be read or parsed. |
| 65 | + * |
| 66 | + * @return array|null Decoded composer.json data or null if unavailable. |
| 67 | + */ |
| 68 | + private function readComposerJson(): ?array |
| 69 | + { |
| 70 | + if (class_exists(InstalledVersions::class) |
| 71 | + && InstalledVersions::isInstalled(self::DEFAULT_PACKAGE_NAME) |
| 72 | + ) { |
| 73 | + $installPath = InstalledVersions::getInstallPath(self::DEFAULT_PACKAGE_NAME); |
| 74 | + |
| 75 | + if ($installPath) { |
| 76 | + $path = $installPath . '/composer.json'; |
| 77 | + |
| 78 | + if (is_file($path)) { |
| 79 | + try { |
| 80 | + return json_decode( |
| 81 | + (string) file_get_contents($path), |
| 82 | + true, |
| 83 | + 512, |
| 84 | + JSON_THROW_ON_ERROR |
| 85 | + ); |
| 86 | + } catch (Throwable $e) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return null; |
35 | 94 | } |
36 | 95 |
|
37 | 96 | /** |
38 | | - * @inheritDoc |
| 97 | + * Resolve application name and version. |
| 98 | + * This method ensures that the application can accurately report its own name and |
| 99 | + * version regardless of how it is installed or executed. |
| 100 | + * |
| 101 | + * @param ContainerInterface $container The application container used to create Composer instance if needed. |
| 102 | + * @return array An array containing the resolved application name and version. |
| 103 | + */ |
| 104 | + private function resolveApplicationMetadata(ContainerInterface $container): array |
| 105 | + { |
| 106 | + // Read composer.json to get the default package name dynamically |
| 107 | + $composerJson = $this->readComposerJson(); |
| 108 | + $defaultPackage = $composerJson['name'] ?? self::DEFAULT_PACKAGE_NAME; |
| 109 | + |
| 110 | + // 1️. Composer InstalledVersions: If the package is installed via Composer, |
| 111 | + // it retrieves the name and version from InstalledVersions. |
| 112 | + if (class_exists(InstalledVersions::class) && InstalledVersions::isInstalled($defaultPackage)) { |
| 113 | + return [ |
| 114 | + $defaultPackage, |
| 115 | + InstalledVersions::getPrettyVersion($defaultPackage) |
| 116 | + ?? InstalledVersions::getVersion($defaultPackage) |
| 117 | + ?? 'unknown' |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + // 2. composer.json: If InstalledVersions is not available, |
| 122 | + // it uses the already-read composer.json data. |
| 123 | + if ($composerJson) { |
| 124 | + return [ |
| 125 | + $composerJson['name'] ?? self::DEFAULT_PACKAGE_NAME, |
| 126 | + $composerJson['version'] ?? 'unknown' |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + // 3️. Composer runtime metadata: If both previous methods fail, it falls back to using the Composer |
| 131 | + // runtime metadata to determine the name and version. |
| 132 | + $composer = $container->get(Composer::class); |
| 133 | + $package = $composer->getPackage(); |
| 134 | + |
| 135 | + return [ |
| 136 | + $package->getPrettyName(), |
| 137 | + $package->getPrettyVersion() |
| 138 | + ]; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get the default commands that should always be available. |
| 143 | + * |
| 144 | + * @return SymfonyCommand[] |
39 | 145 | */ |
40 | 146 | protected function getDefaultCommands(): array |
41 | 147 | { |
42 | | - return array_merge(parent::getDefaultCommands(), [ |
43 | | - $this->container->get(Command\Ece\Apply::class), |
44 | | - $this->container->get(Command\Ece\Revert::class), |
45 | | - $this->container->get(Command\Status::class), |
46 | | - $this->container->get(Command\Verify::class) |
47 | | - ]); |
| 148 | + $instances = array_map( |
| 149 | + fn (string $commandClass): SymfonyCommand => |
| 150 | + $this->container->get($commandClass), |
| 151 | + self::DEFAULT_COMMAND_CLASSES |
| 152 | + ); |
| 153 | + |
| 154 | + return array_merge(parent::getDefaultCommands(), $instances); |
48 | 155 | } |
49 | 156 | } |
0 commit comments