diff --git a/app/code/Magento/Backend/Console/Command/MaintenanceAllowIpsCommand.php b/app/code/Magento/Backend/Console/Command/MaintenanceAllowIpsCommand.php index 230c6a6814ebc..8cddc014b8855 100644 --- a/app/code/Magento/Backend/Console/Command/MaintenanceAllowIpsCommand.php +++ b/app/code/Magento/Backend/Console/Command/MaintenanceAllowIpsCommand.php @@ -1,7 +1,7 @@ setName('maintenance:allow-ips') + $this->setName(self::NAME) ->setDescription('Sets maintenance mode exempt IPs') ->setDefinition(array_merge($arguments, $options)); diff --git a/app/code/Magento/Backend/Console/Command/MaintenanceDisableCommand.php b/app/code/Magento/Backend/Console/Command/MaintenanceDisableCommand.php index 5108866fbe65c..81e4bf736fed6 100644 --- a/app/code/Magento/Backend/Console/Command/MaintenanceDisableCommand.php +++ b/app/code/Magento/Backend/Console/Command/MaintenanceDisableCommand.php @@ -1,7 +1,7 @@ setName('maintenance:disable')->setDescription('Disables maintenance mode'); + $this->setName(self::NAME)->setDescription('Disables maintenance mode'); parent::configure(); } diff --git a/app/code/Magento/Backend/Console/Command/MaintenanceEnableCommand.php b/app/code/Magento/Backend/Console/Command/MaintenanceEnableCommand.php index 7e5e034483d20..095faa09ebf8c 100644 --- a/app/code/Magento/Backend/Console/Command/MaintenanceEnableCommand.php +++ b/app/code/Magento/Backend/Console/Command/MaintenanceEnableCommand.php @@ -1,7 +1,7 @@ setName('maintenance:enable')->setDescription('Enables maintenance mode'); + $this->setName(self::NAME)->setDescription('Enables maintenance mode'); parent::configure(); } diff --git a/app/code/Magento/Backend/Console/Command/MaintenanceStatusCommand.php b/app/code/Magento/Backend/Console/Command/MaintenanceStatusCommand.php index f1a880666226f..25b0ce4261747 100644 --- a/app/code/Magento/Backend/Console/Command/MaintenanceStatusCommand.php +++ b/app/code/Magento/Backend/Console/Command/MaintenanceStatusCommand.php @@ -1,7 +1,7 @@ setName('maintenance:status') + $this->setName(self::NAME) ->setDescription('Displays maintenance mode status'); parent::configure(); diff --git a/dev/tests/static/testsuite/Magento/Test/Integrity/Library/DependencyTest.php b/dev/tests/static/testsuite/Magento/Test/Integrity/Library/DependencyTest.php index 2512405d243c7..4e67a47928adb 100644 --- a/dev/tests/static/testsuite/Magento/Test/Integrity/Library/DependencyTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Integrity/Library/DependencyTest.php @@ -1,7 +1,7 @@ firstMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); + $this->secondMockCommandLoader = $this->getMockBuilder(CommandLoaderInterface::class)->getMock(); + $this->aggregateCommandLoader = new Aggregate([$this->firstMockCommandLoader, $this->secondMockCommandLoader]); + } + + /** + * Test the various cases of `has` for the aggregate command loader: + * - When at least one "internal" command loader has a command, the aggregate does as well + * - When none of the "internal" command loaders has a command, neither does the aggregate + * + * @dataProvider provideTestCasesForHas + */ + public function testHas(bool $firstResult, bool $secondResult, bool $overallResult): void + { + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstResult); + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondResult); + + $this->assertEquals($overallResult, $this->aggregateCommandLoader->has('foo')); + } + + public function provideTestCasesForHas(): array + { + return [ + [true, false, true], + [false, true, true], + [false, false, false] + ]; + } + + /** + * Test the various cases of `get` for the aggregate command loader. Similar to `has`, + * the return value of `Aggregate::get` mirrors its internal command loaders. + * + * For simplicity, this test does not cover the "no results" case. @see testGetThrow + * + * @dataProvider provideTestCasesForGet + */ + public function testGet(?Command $firstCmd, ?Command $secondCmd): void + { + $firstHas = (bool)$firstCmd; + $secondHas = (bool)$secondCmd; + + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn($firstHas); + if ($firstHas) { + $this->firstMockCommandLoader->method('get')->with('foo')->willReturn($firstCmd); + } + + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn($secondHas); + if ($secondHas) { + $this->secondMockCommandLoader->method('get')->with('foo')->willReturn($secondCmd); + } + + $this->assertInstanceOf(Command::class, $this->aggregateCommandLoader->get('foo')); + } + + public function provideTestCasesForGet(): array + { + return [ + [ + new Command(), + null + ], + [ + null, + new Command() + ] + ]; + } + + /** + * When none of the internal command loaders have matching commands, the aggregate command loader + * will throw an exception. @see CommandNotFoundException + */ + public function testGetThrow(): void + { + $this->firstMockCommandLoader->method('has')->with('foo')->willReturn(false); + $this->secondMockCommandLoader->method('has')->with('foo')->willReturn(false); + + $this->expectException(CommandNotFoundException::class); + $this->aggregateCommandLoader->get('foo'); + } + + /** + * An aggregate command loader's `getNames` method returns the merged array of the `getNames` + * return values of all its internal command loaders + */ + public function testGetNames(): void + { + $this->firstMockCommandLoader->method('getNames')->willReturn(['foo', 'bar']); + $this->secondMockCommandLoader->method('getNames')->willReturn(['baz', 'qux']); + + $this->assertEquals(['foo', 'bar', 'baz', 'qux'], $this->aggregateCommandLoader->getNames()); + } +} diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Console/CommandLoaderTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Console/CommandLoaderTest.php new file mode 100644 index 0000000000000..61a0bd59ae226 --- /dev/null +++ b/lib/internal/Magento/Framework/App/Test/Unit/Console/CommandLoaderTest.php @@ -0,0 +1,117 @@ +objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)->getMock(); + } + + /** + * Test that the command loader, when provided zero commands, does not have a command named "foo" + */ + public function testHasWithZeroCommands(): void + { + $subj = new CommandLoader($this->objectManagerMock, []); + + $this->assertFalse($subj->has('foo')); + } + + /** + * Test that the command loader will return true when provided with a command "foo" + */ + public function testHasWithAtLeastOneCommand(): void + { + $subj = new CommandLoader($this->objectManagerMock, [ + [ + 'name' => 'foo', + 'class' => FooCommand::class + ] + ]); + + $this->assertTrue($subj->has('foo')); + } + + /** + * Test that the command loader will throw a CommandNotFoundException when it does not have the requested command + */ + public function testGetWithZeroCommands(): void + { + $subj = new CommandLoader($this->objectManagerMock, []); + + $this->expectException(CommandNotFoundException::class); + + $subj->get('foo'); + } + + /** + * Test that the command loader returns a command when one it has is requested + */ + public function testGetWithAtLeastOneCommand(): void + { + $this->objectManagerMock + ->method('create') + ->with(FooCommand::class) + ->willReturn(new FooCommand()); + + $subj = new CommandLoader($this->objectManagerMock, [ + [ + 'name' => 'foo', + 'class' => FooCommand::class + ] + ]); + + $this->assertInstanceOf(FooCommand::class, $subj->get('foo')); + } + + /** + * Test that the command loader will return an empty "names" array when it has none + */ + public function testGetNamesWithZeroCommands(): void + { + $subj = new CommandLoader($this->objectManagerMock, []); + + $this->assertEquals([], $subj->getNames()); + } + + /** + * Test that the command loader returns an array of its command names when `getNames` is called + */ + public function testGetNames(): void + { + $subj = new CommandLoader($this->objectManagerMock, [ + [ + 'name' => 'foo', + 'class' => FooCommand::class + ], + [ + 'name' => 'bar', + 'class' => 'BarCommand' + ] + ]); + + $this->assertEquals(['foo', 'bar'], $subj->getNames()); + } +} + +// phpcs:ignore PSR1.Classes.ClassDeclaration +class FooCommand extends Command +{ +} diff --git a/lib/internal/Magento/Framework/Console/Cli.php b/lib/internal/Magento/Framework/Console/Cli.php index ca82e495e5a59..c3bd6d0f5f71d 100644 --- a/lib/internal/Magento/Framework/Console/Cli.php +++ b/lib/internal/Magento/Framework/Console/Cli.php @@ -1,27 +1,30 @@ serviceManager->setService(\Symfony\Component\Console\Application::class, $this); $this->logger = $this->objectManager->get(LoggerInterface::class); + $this->setCommandLoader($this->getCommandLoader()); } /** @@ -146,11 +150,6 @@ protected function getApplicationCommands() { $commands = []; try { - if (class_exists(\Magento\Setup\Console\CommandList::class)) { - $setupCommandList = new \Magento\Setup\Console\CommandList($this->serviceManager); - $commands = array_merge($commands, $setupCommandList->getCommands()); - } - if ($this->objectManager->get(DeploymentConfig::class)->isAvailable()) { /** @var CommandListInterface */ $commandList = $this->objectManager->create(CommandListInterface::class); @@ -232,4 +231,23 @@ protected function getVendorCommands($objectManager) return array_merge([], ...$commands); } + + /** + * Generate and return the Command Loader + * + * @throws \LogicException + * @throws \BadMethodCallException + */ + private function getCommandLoader(): Console\CommandLoader\CommandLoaderInterface + { + $commandLoaders = []; + if (class_exists(SetupCommandLoader::class)) { + $commandLoaders[] = new SetupCommandLoader($this->serviceManager); + } + $commandLoaders[] = $this->objectManager->create(CommandLoader::class); + + return $this->objectManager->create(Aggregate::class, [ + 'commandLoaders' => $commandLoaders + ]); + } } diff --git a/lib/internal/Magento/Framework/Console/CommandLoader.php b/lib/internal/Magento/Framework/Console/CommandLoader.php new file mode 100644 index 0000000000000..195a2219c503c --- /dev/null +++ b/lib/internal/Magento/Framework/Console/CommandLoader.php @@ -0,0 +1,77 @@ + 'Fully\Qualified\ClassName' ] + * @var array + */ + private array $commands; + + /** @var ObjectManagerInterface */ + private ObjectManagerInterface $objectManager; + + /** + * @param ObjectManagerInterface $objectManager + * @param array $commands + */ + public function __construct(ObjectManagerInterface $objectManager, array $commands = []) + { + $this->objectManager = $objectManager; + $this->commands = array_combine(array_column($commands, 'name'), array_column($commands, 'class')); + } + + /** + * Using the ObjectManager, instantiate the requested command. + * + * If the command name is not configured, throw a CommandNotFoundException. + * + * @param string $name + * @return Command + * @throws CommandNotFoundException + */ + public function get(string $name): Command + { + if ($this->has($name)) { + return $this->objectManager->create($this->commands[$name]); + } + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); + } + + /** + * Return whether the requested $name is present in the commands array + * + * @param string $name + * @return bool + */ + public function has(string $name): bool + { + return isset($this->commands[$name]); + } + + /** + * Return an array of the available command names + * + * @return string[] + */ + public function getNames(): array + { + return array_keys($this->commands); + } +} diff --git a/lib/internal/Magento/Framework/Console/CommandLoader/Aggregate.php b/lib/internal/Magento/Framework/Console/CommandLoader/Aggregate.php new file mode 100644 index 0000000000000..620035ea3be58 --- /dev/null +++ b/lib/internal/Magento/Framework/Console/CommandLoader/Aggregate.php @@ -0,0 +1,79 @@ +commandLoaders = $commandLoaders; + } + + /** + * Intiantiate and return the command referred to by $name within the internal command loaders. + * + * If $name does not refer to a command, throw a CommandNotFoundException. + * + * @param string $name + * @return Command + * @throws CommandNotFoundException + */ + public function get(string $name): Command + { + foreach ($this->commandLoaders as $commandLoader) { + if ($commandLoader->has($name)) { + return $commandLoader->get($name); + } + } + + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); + } + + /** + * Return whether $name refers to a command within the internal command loaders. + * + * @param string $name + * @return bool + */ + public function has(string $name): bool + { + foreach ($this->commandLoaders as $commandLoader) { + if ($commandLoader->has($name)) { + return true; + } + } + + return false; + } + + /** + * Return an array of all the command names provided by the internal command loaders. + * + * @return string[] + */ + public function getNames(): array + { + return array_merge([], ...array_map(static function (CommandLoaderInterface $commandLoader) { + return $commandLoader->getNames(); + }, $this->commandLoaders)); + } +} diff --git a/setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php b/setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php index 8e64aae20573c..e0f4e1cf390af 100644 --- a/setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php +++ b/setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php @@ -1,7 +1,7 @@ setName('admin:user:create') + $this->setName(self::NAME) ->setDescription('Creates an administrator') ->setDefinition($this->getOptionsList()); parent::configure(); diff --git a/setup/src/Magento/Setup/Console/Command/BackupCommand.php b/setup/src/Magento/Setup/Console/Command/BackupCommand.php index 841e766ebcb90..7b399f5d7c8c0 100644 --- a/setup/src/Magento/Setup/Console/Command/BackupCommand.php +++ b/setup/src/Magento/Setup/Console/Command/BackupCommand.php @@ -1,7 +1,7 @@ setName('setup:backup') + $this->setName(self::NAME) ->setDescription('Takes backup of Magento Application code base, media and database') ->setDefinition($options); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/ConfigSetCommand.php b/setup/src/Magento/Setup/Console/Command/ConfigSetCommand.php index 9380a7b72d6b2..7a69531a9e832 100644 --- a/setup/src/Magento/Setup/Console/Command/ConfigSetCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ConfigSetCommand.php @@ -1,7 +1,7 @@ configModel->getAvailableOptions(); - $this->setName('setup:config:set') + $this->setName(self::NAME) ->setDescription('Creates or modifies the deployment configuration') ->setDefinition($options); diff --git a/setup/src/Magento/Setup/Console/Command/DbDataUpgradeCommand.php b/setup/src/Magento/Setup/Console/Command/DbDataUpgradeCommand.php index ae0c2f5c4e27f..bd4de7ddf2b2d 100644 --- a/setup/src/Magento/Setup/Console/Command/DbDataUpgradeCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DbDataUpgradeCommand.php @@ -1,7 +1,7 @@ setName('setup:db-data:upgrade')->setDescription('Installs and upgrades data in the DB'); + $this->setName(self::NAME)->setDescription('Installs and upgrades data in the DB'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/DbSchemaUpgradeCommand.php b/setup/src/Magento/Setup/Console/Command/DbSchemaUpgradeCommand.php index 3ef9a441b868f..4116102909a5c 100644 --- a/setup/src/Magento/Setup/Console/Command/DbSchemaUpgradeCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DbSchemaUpgradeCommand.php @@ -1,7 +1,7 @@ setName('setup:db-schema:upgrade') + ->setName(self::NAME) ->setDefinition( [ new InputOption( @@ -70,7 +71,7 @@ protected function configure() } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/DbStatusCommand.php b/setup/src/Magento/Setup/Console/Command/DbStatusCommand.php index be18bd58037ea..f5b9055dcfead 100644 --- a/setup/src/Magento/Setup/Console/Command/DbStatusCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DbStatusCommand.php @@ -1,7 +1,7 @@ setName('setup:db:status') + $this->setName(self::NAME) ->setDescription('Checks if DB schema or data requires upgrade'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/DependenciesShowFrameworkCommand.php b/setup/src/Magento/Setup/Console/Command/DependenciesShowFrameworkCommand.php index 3ea98d7aa252d..0cc5ed50ccdc3 100644 --- a/setup/src/Magento/Setup/Console/Command/DependenciesShowFrameworkCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DependenciesShowFrameworkCommand.php @@ -1,7 +1,7 @@ setDescription('Shows number of dependencies on Magento framework') - ->setName('info:dependencies:show-framework'); + ->setName(self::NAME); parent::configure(); } diff --git a/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCircularCommand.php b/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCircularCommand.php index c9cfdd7526136..61f5512dab550 100644 --- a/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCircularCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCircularCommand.php @@ -1,7 +1,7 @@ setDescription('Shows number of circular dependencies between modules') - ->setName('info:dependencies:show-modules-circular'); + ->setName(self::NAME); parent::configure(); } diff --git a/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCommand.php b/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCommand.php index 13499b56412a1..a087a2fa1a9b7 100644 --- a/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DependenciesShowModulesCommand.php @@ -1,7 +1,7 @@ setDescription('Shows number of dependencies between modules') - ->setName('info:dependencies:show-modules'); + ->setName(self::NAME); parent::configure(); } diff --git a/setup/src/Magento/Setup/Console/Command/DeployStaticContentCommand.php b/setup/src/Magento/Setup/Console/Command/DeployStaticContentCommand.php index 1afdd86cdff3f..63d3354ec4ae2 100644 --- a/setup/src/Magento/Setup/Console/Command/DeployStaticContentCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DeployStaticContentCommand.php @@ -1,7 +1,7 @@ setName('setup:static-content:deploy') + $this->setName(self::NAME) ->setDescription('Deploys static view files') ->setDefinition($this->options->getOptionsList()); diff --git a/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php b/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php index 24e370758e317..8b577e8a48919 100644 --- a/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php +++ b/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php @@ -1,7 +1,7 @@ setName('setup:performance:generate-fixtures') + $this->setName(self::NAME) ->setDescription('Generates fixtures') ->setDefinition([ new InputArgument( diff --git a/setup/src/Magento/Setup/Console/Command/I18nCollectPhrasesCommand.php b/setup/src/Magento/Setup/Console/Command/I18nCollectPhrasesCommand.php index bb8ab4e4cc49a..36e800676364c 100644 --- a/setup/src/Magento/Setup/Console/Command/I18nCollectPhrasesCommand.php +++ b/setup/src/Magento/Setup/Console/Command/I18nCollectPhrasesCommand.php @@ -1,7 +1,7 @@ setName('i18n:collect-phrases') + $this->setName(self::NAME) ->setDescription('Discovers phrases in the codebase'); $this->setDefinition([ new InputArgument( @@ -57,7 +58,7 @@ protected function configure() } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/I18nPackCommand.php b/setup/src/Magento/Setup/Console/Command/I18nPackCommand.php index 7c5cc89387dcf..997a63ab81f38 100644 --- a/setup/src/Magento/Setup/Console/Command/I18nPackCommand.php +++ b/setup/src/Magento/Setup/Console/Command/I18nPackCommand.php @@ -1,7 +1,7 @@ setName('i18n:pack') + $this->setName(self::NAME) ->setDescription('Saves language package'); $this->setDefinition([ new InputArgument( @@ -73,7 +74,7 @@ protected function configure() } /** - * {@inheritdoc} + * @inheritdoc * @throws \InvalidArgumentException */ protected function execute(InputInterface $input, OutputInterface $output) diff --git a/setup/src/Magento/Setup/Console/Command/InfoAdminUriCommand.php b/setup/src/Magento/Setup/Console/Command/InfoAdminUriCommand.php index 006afc27a0826..3ece84ea64511 100644 --- a/setup/src/Magento/Setup/Console/Command/InfoAdminUriCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InfoAdminUriCommand.php @@ -1,7 +1,7 @@ setName('info:adminuri') + $this->setName(self::NAME) ->setDescription('Displays the Magento Admin URI'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/InfoBackupsListCommand.php b/setup/src/Magento/Setup/Console/Command/InfoBackupsListCommand.php index 94337dd0742e3..bb2e41708c78a 100644 --- a/setup/src/Magento/Setup/Console/Command/InfoBackupsListCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InfoBackupsListCommand.php @@ -1,7 +1,7 @@ setName('info:backups:list') + $this->setName(self::NAME) ->setDescription('Prints list of available backup files'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/InfoCurrencyListCommand.php b/setup/src/Magento/Setup/Console/Command/InfoCurrencyListCommand.php index 91cfb5e7f6b5c..b4ba0cdeb7e81 100644 --- a/setup/src/Magento/Setup/Console/Command/InfoCurrencyListCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InfoCurrencyListCommand.php @@ -1,7 +1,7 @@ setName('info:currency:list') + $this->setName(self::NAME) ->setDescription('Displays the list of available currencies'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/InfoLanguageListCommand.php b/setup/src/Magento/Setup/Console/Command/InfoLanguageListCommand.php index 8950bd5edb2fa..99999485e4e52 100644 --- a/setup/src/Magento/Setup/Console/Command/InfoLanguageListCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InfoLanguageListCommand.php @@ -1,7 +1,7 @@ setName('info:language:list') + $this->setName(self::NAME) ->setDescription('Displays the list of available language locales'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/InfoTimezoneListCommand.php b/setup/src/Magento/Setup/Console/Command/InfoTimezoneListCommand.php index 2ff1d228dfe24..1c061c3f3c834 100644 --- a/setup/src/Magento/Setup/Console/Command/InfoTimezoneListCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InfoTimezoneListCommand.php @@ -1,7 +1,7 @@ setName('info:timezone:list') + $this->setName(self::NAME) ->setDescription('Displays the list of available timezones'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/InstallCommand.php b/setup/src/Magento/Setup/Console/Command/InstallCommand.php index 1f716043c3846..fe521ab6a5beb 100644 --- a/setup/src/Magento/Setup/Console/Command/InstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InstallCommand.php @@ -1,7 +1,7 @@ setName('setup:install') + $this->setName(self::NAME) ->setDescription('Installs the Magento application') ->setDefinition($inputOptions); parent::configure(); diff --git a/setup/src/Magento/Setup/Console/Command/InstallStoreConfigurationCommand.php b/setup/src/Magento/Setup/Console/Command/InstallStoreConfigurationCommand.php index 7eb8aec274888..e4c4d52b48d7c 100644 --- a/setup/src/Magento/Setup/Console/Command/InstallStoreConfigurationCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InstallStoreConfigurationCommand.php @@ -1,7 +1,7 @@ installerFactory = $installerFactory; $this->deploymentConfig = $deploymentConfig; - $this->objectManager = $objectManagerProvider->get(); $this->localeValidator = $localeValidator; $this->timezoneValidator = $timezoneValidator; $this->currencyValidator = $currencyValidator; @@ -97,18 +86,18 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ protected function configure() { - $this->setName('setup:store-config:set') + $this->setName(self::NAME) ->setDescription('Installs the store configuration. Deprecated since 2.2.0. Use config:set instead') ->setDefinition($this->getOptionsList()); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -310,9 +299,9 @@ private function validateBinaryValue($value, $key) /** * Validate codes for languages, currencies or timezones * - * @param LocaleValidator|TimezoneValidator|CurrencyValidator $lists - * @param string $code - * @param string $type + * @param LocaleValidator|TimezoneValidator|CurrencyValidator $lists + * @param string $code + * @param string $type * @return string */ private function validateCodes($lists, $code, $type) diff --git a/setup/src/Magento/Setup/Console/Command/ModuleConfigStatusCommand.php b/setup/src/Magento/Setup/Console/Command/ModuleConfigStatusCommand.php index 70dbf14728bd7..d01384e6bd424 100644 --- a/setup/src/Magento/Setup/Console/Command/ModuleConfigStatusCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ModuleConfigStatusCommand.php @@ -1,7 +1,7 @@ setName('module:config:status') + ->setName(self::NAME) ->setDescription( 'Checks the modules configuration in the \'app/etc/config.php\' file ' . 'and reports if they are up to date or not' diff --git a/setup/src/Magento/Setup/Console/Command/ModuleDisableCommand.php b/setup/src/Magento/Setup/Console/Command/ModuleDisableCommand.php index cf3dd328616ba..663683e9db14f 100644 --- a/setup/src/Magento/Setup/Console/Command/ModuleDisableCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ModuleDisableCommand.php @@ -1,7 +1,7 @@ setName('module:disable') + $this->setName(self::NAME) ->setDescription('Disables specified modules'); parent::configure(); } diff --git a/setup/src/Magento/Setup/Console/Command/ModuleEnableCommand.php b/setup/src/Magento/Setup/Console/Command/ModuleEnableCommand.php index 62a514ada9b8d..74ac6f57def48 100644 --- a/setup/src/Magento/Setup/Console/Command/ModuleEnableCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ModuleEnableCommand.php @@ -1,7 +1,7 @@ setName('module:enable') + $this->setName(self::NAME) ->setDescription('Enables specified modules'); parent::configure(); } diff --git a/setup/src/Magento/Setup/Console/Command/ModuleStatusCommand.php b/setup/src/Magento/Setup/Console/Command/ModuleStatusCommand.php index 4e5e73f53478e..bdd44d2918717 100644 --- a/setup/src/Magento/Setup/Console/Command/ModuleStatusCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ModuleStatusCommand.php @@ -1,9 +1,8 @@ setName('module:status') + $this->setName(self::NAME) ->setDescription('Displays status of modules') ->addArgument( 'module-names', diff --git a/setup/src/Magento/Setup/Console/Command/ModuleUninstallCommand.php b/setup/src/Magento/Setup/Console/Command/ModuleUninstallCommand.php index 5754bbca986c8..5ec48a1bbc018 100644 --- a/setup/src/Magento/Setup/Console/Command/ModuleUninstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/ModuleUninstallCommand.php @@ -1,7 +1,7 @@ setName('module:uninstall') + $this->setName(self::NAME) ->setDescription('Uninstalls modules installed by composer') ->setDefinition($options); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function isModuleRequired() { @@ -220,7 +215,7 @@ protected function isModuleRequired() } /** - * {@inheritdoc} + * @inheritdoc * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ diff --git a/setup/src/Magento/Setup/Console/Command/RollbackCommand.php b/setup/src/Magento/Setup/Console/Command/RollbackCommand.php index e114c84ba79bc..6d491d0d375d3 100644 --- a/setup/src/Magento/Setup/Console/Command/RollbackCommand.php +++ b/setup/src/Magento/Setup/Console/Command/RollbackCommand.php @@ -1,7 +1,7 @@ setName('setup:rollback') + $this->setName(self::NAME) ->setDescription('Rolls back Magento Application codebase, media and database') ->setDefinition($options); parent::configure(); diff --git a/setup/src/Magento/Setup/Console/Command/UninstallCommand.php b/setup/src/Magento/Setup/Console/Command/UninstallCommand.php index 0163ef3846c83..5c31341de30f7 100644 --- a/setup/src/Magento/Setup/Console/Command/UninstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/UninstallCommand.php @@ -1,7 +1,7 @@ setName('setup:uninstall') + $this->setName(self::NAME) ->setDescription('Uninstalls the Magento application'); parent::configure(); } /** - * {@inheritdoc} + * @inheritdoc */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php index 84a0897ace10d..44f887d940491 100644 --- a/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php +++ b/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php @@ -1,8 +1,8 @@ setName('setup:upgrade') + $this->setName(self::NAME) ->setDescription('Upgrades the Magento application, DB data, and schema') ->setDefinition($options); parent::configure(); diff --git a/setup/src/Magento/Setup/Console/CommandList.php b/setup/src/Magento/Setup/Console/CommandList.php deleted file mode 100644 index ae65e82bba12b..0000000000000 --- a/setup/src/Magento/Setup/Console/CommandList.php +++ /dev/null @@ -1,97 +0,0 @@ -serviceManager = $serviceManager; - } - - /** - * Gets list of setup command classes - * - * @return string[] - */ - protected function getCommandsClasses() - { - return [ - \Magento\Setup\Console\Command\AdminUserCreateCommand::class, - \Magento\Setup\Console\Command\BackupCommand::class, - \Magento\Setup\Console\Command\ConfigSetCommand::class, - \Magento\Setup\Console\Command\DbDataUpgradeCommand::class, - \Magento\Setup\Console\Command\DbSchemaUpgradeCommand::class, - \Magento\Setup\Console\Command\DbStatusCommand::class, - \Magento\Setup\Console\Command\DependenciesShowFrameworkCommand::class, - \Magento\Setup\Console\Command\DependenciesShowModulesCircularCommand::class, - \Magento\Setup\Console\Command\DependenciesShowModulesCommand::class, - \Magento\Setup\Console\Command\DiCompileCommand::class, - \Magento\Setup\Console\Command\GenerateFixturesCommand::class, - \Magento\Setup\Console\Command\I18nCollectPhrasesCommand::class, - \Magento\Setup\Console\Command\I18nPackCommand::class, - \Magento\Setup\Console\Command\InfoAdminUriCommand::class, - \Magento\Setup\Console\Command\InfoBackupsListCommand::class, - \Magento\Setup\Console\Command\InfoCurrencyListCommand::class, - \Magento\Setup\Console\Command\InfoLanguageListCommand::class, - \Magento\Setup\Console\Command\InfoTimezoneListCommand::class, - \Magento\Setup\Console\Command\InstallCommand::class, - \Magento\Setup\Console\Command\InstallStoreConfigurationCommand::class, - \Magento\Setup\Console\Command\ModuleEnableCommand::class, - \Magento\Setup\Console\Command\ModuleDisableCommand::class, - \Magento\Setup\Console\Command\ModuleStatusCommand::class, - \Magento\Setup\Console\Command\ModuleUninstallCommand::class, - \Magento\Setup\Console\Command\ModuleConfigStatusCommand::class, - \Magento\Setup\Console\Command\RollbackCommand::class, - \Magento\Setup\Console\Command\UpgradeCommand::class, - \Magento\Setup\Console\Command\UninstallCommand::class, - \Magento\Setup\Console\Command\DeployStaticContentCommand::class - ]; - } - - /** - * Gets list of command instances. - * - * @return \Symfony\Component\Console\Command\Command[] - * @throws \Exception - */ - public function getCommands() - { - $commands = []; - - foreach ($this->getCommandsClasses() as $class) { - if (class_exists($class)) { - $commands[] = $this->serviceManager->get($class); - } else { - // phpcs:ignore Magento2.Exceptions.DirectThrow - throw new \Exception('Class ' . $class . ' does not exist'); - } - } - - return $commands; - } -} diff --git a/setup/src/Magento/Setup/Console/CommandLoader.php b/setup/src/Magento/Setup/Console/CommandLoader.php new file mode 100644 index 0000000000000..4bdc46535d98e --- /dev/null +++ b/setup/src/Magento/Setup/Console/CommandLoader.php @@ -0,0 +1,110 @@ + Command\AdminUserCreateCommand::class, + Command\BackupCommand::NAME => Command\BackupCommand::class, + Command\ConfigSetCommand::NAME => Command\ConfigSetCommand::class, + Command\DbDataUpgradeCommand::NAME => Command\DbDataUpgradeCommand::class, + Command\DbSchemaUpgradeCommand::NAME => Command\DbSchemaUpgradeCommand::class, + Command\DbStatusCommand::NAME => Command\DbStatusCommand::class, + Command\DependenciesShowFrameworkCommand::NAME => Command\DependenciesShowFrameworkCommand::class, + Command\DependenciesShowModulesCircularCommand::NAME => Command\DependenciesShowModulesCircularCommand::class, + Command\DependenciesShowModulesCommand::NAME => Command\DependenciesShowModulesCommand::class, + Command\DiCompileCommand::NAME => Command\DiCompileCommand::class, + Command\GenerateFixturesCommand::NAME => Command\GenerateFixturesCommand::class, + Command\I18nCollectPhrasesCommand::NAME => Command\I18nCollectPhrasesCommand::class, + Command\I18nPackCommand::NAME => Command\I18nPackCommand::class, + Command\InfoAdminUriCommand::NAME => Command\InfoAdminUriCommand::class, + Command\InfoBackupsListCommand::NAME => Command\InfoBackupsListCommand::class, + Command\InfoCurrencyListCommand::NAME => Command\InfoCurrencyListCommand::class, + Command\InfoLanguageListCommand::NAME => Command\InfoLanguageListCommand::class, + Command\InfoTimezoneListCommand::NAME => Command\InfoTimezoneListCommand::class, + Command\InstallCommand::NAME => Command\InstallCommand::class, + Command\InstallStoreConfigurationCommand::NAME => Command\InstallStoreConfigurationCommand::class, + Command\ModuleEnableCommand::NAME => Command\ModuleEnableCommand::class, + Command\ModuleDisableCommand::NAME => Command\ModuleDisableCommand::class, + Command\ModuleStatusCommand::NAME => Command\ModuleStatusCommand::class, + Command\ModuleUninstallCommand::NAME => Command\ModuleUninstallCommand::class, + Command\ModuleConfigStatusCommand::NAME => Command\ModuleConfigStatusCommand::class, + Command\RollbackCommand::NAME => Command\RollbackCommand::class, + Command\UpgradeCommand::NAME => Command\UpgradeCommand::class, + Command\UninstallCommand::NAME => Command\UninstallCommand::class, + Command\DeployStaticContentCommand::NAME => Command\DeployStaticContentCommand::class + ]; + + /** + * + * @param ServiceManager $serviceManager + */ + public function __construct(ServiceManager $serviceManager) + { + $this->serviceManager = $serviceManager; + } + + /** + * Generate a symfony console command with the laminas service manager + * + * @param string $name + * @return SymfonyCommand + * @throws CommandNotFoundException + */ + public function get(string $name): SymfonyCommand + { + if ($this->has($name)) { + /** @var SymfonyCommand $command */ + $command = $this->serviceManager->get($this->commands[$name]); + return $command; + } + + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); + } + + /** + * Return whether the command loader has a command configured for given $name + * + * @param string $name + * @return bool + */ + public function has(string $name): bool + { + return isset($this->commands[$name]); + } + + /** + * Return the list of configured commands for the command loader + * + * @return string[] + */ + public function getNames(): array + { + return array_keys($this->commands); + } +} diff --git a/setup/src/Magento/Setup/Test/Unit/Console/Command/InstallStoreConfigurationCommandTest.php b/setup/src/Magento/Setup/Test/Unit/Console/Command/InstallStoreConfigurationCommandTest.php index 7e29c1ab90d9a..e6ac5ffd349db 100644 --- a/setup/src/Magento/Setup/Test/Unit/Console/Command/InstallStoreConfigurationCommandTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Console/Command/InstallStoreConfigurationCommandTest.php @@ -1,14 +1,13 @@ deploymentConfig = $this->createMock(DeploymentConfig::class); $this->installer = $this->createMock(Installer::class); $objectManagerProvider = $this->createMock(ObjectManagerProvider::class); - $this->objectManager = $this->getMockForAbstractClass( - ObjectManagerInterface::class, - [], - '', - false - ); - $objectManagerProvider->expects($this->once())->method('get')->willReturn($this->objectManager); + $this->command = new InstallStoreConfigurationCommand( $this->installerFactory, $this->deploymentConfig, diff --git a/setup/src/Magento/Setup/Test/Unit/Console/CommandListTest.php b/setup/src/Magento/Setup/Test/Unit/Console/CommandListTest.php deleted file mode 100644 index 09b756433be28..0000000000000 --- a/setup/src/Magento/Setup/Test/Unit/Console/CommandListTest.php +++ /dev/null @@ -1,40 +0,0 @@ -serviceManager = $this->createMock(ServiceManager::class); - $this->commandList = new CommandList($this->serviceManager); - } - - public function testGetCommands() - { - $this->serviceManager->expects($this->atLeastOnce()) - ->method('get'); - - $this->commandList->getCommands(); - } -} diff --git a/setup/src/Magento/Setup/Test/Unit/Console/CommandLoaderTest.php b/setup/src/Magento/Setup/Test/Unit/Console/CommandLoaderTest.php new file mode 100644 index 0000000000000..d90fc08641e29 --- /dev/null +++ b/setup/src/Magento/Setup/Test/Unit/Console/CommandLoaderTest.php @@ -0,0 +1,46 @@ +serviceManager = $this->createMock(ServiceManager::class); + $this->commandLoader = new CommandLoader($this->serviceManager); + } + + public function testServiceManagerIsUsedToInitializeCommands(): void + { + $command = $this->getMockBuilder(Command::class) + ->disableOriginalConstructor() + ->getMock(); + $this->serviceManager->expects($this->once()) + ->method('get') + ->willReturn($command); + + $firstCommandName = current($this->commandLoader->getNames()); + $this->commandLoader->get($firstCommandName); + } +}