From 1886c105df2772c0a1a17fa739318c3bfb731ce9 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Mon, 9 Jun 2025 17:40:54 +0200 Subject: [PATCH] [Console] Simplify using invokable commands when the component is used standalone --- UPGRADE-7.4.md | 10 + .../Twig/Tests/Command/DebugCommandTest.php | 14 +- .../Twig/Tests/Command/LintCommandTest.php | 6 +- .../Bundle/FrameworkBundle/CHANGELOG.md | 5 + .../FrameworkBundle/Console/Application.php | 22 +- .../Command/AboutCommand/AboutCommandTest.php | 2 +- .../Command/CachePoolClearCommandTest.php | 2 +- .../Command/CachePoolDeleteCommandTest.php | 4 +- .../Tests/Command/CachePruneCommandTest.php | 2 +- .../Tests/Command/RouterMatchCommandTest.php | 4 +- .../Command/TranslationDebugCommandTest.php | 2 +- ...ranslationExtractCommandCompletionTest.php | 2 +- .../Command/TranslationExtractCommandTest.php | 2 +- .../Tests/Command/WorkflowDumpCommandTest.php | 7 +- .../Tests/Command/XliffLintCommandTest.php | 7 +- .../Tests/Command/YamlLintCommandTest.php | 7 +- .../Tests/Console/ApplicationTest.php | 2 +- .../Tests/Functional/BundlePathsTest.php | 2 +- .../Functional/CachePoolClearCommandTest.php | 2 +- .../Functional/CachePoolListCommandTest.php | 2 +- .../Functional/ConfigDebugCommandTest.php | 2 +- .../ConfigDumpReferenceCommandTest.php | 2 +- .../Functional/DebugAutowiringCommandTest.php | 2 +- src/Symfony/Component/Console/Application.php | 39 ++- src/Symfony/Component/Console/CHANGELOG.md | 2 + .../Console/SingleCommandApplication.php | 2 +- .../Console/Tests/ApplicationTest.php | 234 ++++++++++++------ .../Console/Tests/Command/CommandTest.php | 4 +- .../Tests/Command/CompleteCommandTest.php | 2 +- .../Console/Tests/Command/HelpCommandTest.php | 2 +- .../Console/Tests/Command/ListCommandTest.php | 8 +- .../Console/Tests/ConsoleEventsTest.php | 2 +- .../Descriptor/ApplicationDescriptionTest.php | 2 +- .../Tests/Fixtures/DescriptorApplication2.php | 8 +- .../DescriptorApplicationMbString.php | 2 +- .../Tests/Tester/CommandTesterTest.php | 2 +- .../Tests/phpt/alarm/command_exit.phpt | 2 +- .../Tests/phpt/signal/command_exit.phpt | 2 +- .../Dotenv/Tests/Command/DebugCommandTest.php | 6 +- .../Tests/Command/DotenvDumpCommandTest.php | 7 +- .../Tests/Command/ErrorDumpCommandTest.php | 9 +- .../Form/Tests/Command/DebugCommandTest.php | 12 +- .../Command/ConsumeMessagesCommandTest.php | 36 ++- .../Tests/Command/DebugCommandTest.php | 6 +- .../Component/Runtime/SymfonyRuntime.php | 6 +- .../Runtime/Tests/phpt/application.php | 6 +- .../Runtime/Tests/phpt/command_list.php | 6 +- .../Command/TranslationLintCommandTest.php | 6 +- .../Command/TranslationPullCommandTest.php | 13 +- .../Command/TranslationPushCommandTest.php | 19 +- .../Tests/Command/XliffLintCommandTest.php | 7 +- .../Tests/Command/GenerateUlidCommandTest.php | 7 +- .../Tests/Command/GenerateUuidCommandTest.php | 7 +- .../VarDumper/Resources/bin/var-dump-server | 9 +- .../Component/Yaml/Resources/bin/yaml-lint | 9 +- .../Yaml/Tests/Command/LintCommandTest.php | 7 +- 56 files changed, 442 insertions(+), 161 deletions(-) diff --git a/UPGRADE-7.4.md b/UPGRADE-7.4.md index 6623f1f6cd2bb..487bf6f5007a6 100644 --- a/UPGRADE-7.4.md +++ b/UPGRADE-7.4.md @@ -8,6 +8,16 @@ Read more about this in the [Symfony documentation](https://symfony.com/doc/7.4/ If you're upgrading from a version below 7.3, follow the [7.3 upgrade guide](UPGRADE-7.3.md) first. +Console +------- + + * Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()` + +FrameworkBundle +--------------- + + * Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()` + HttpClient ---------- diff --git a/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php index 7ba828c667214..2107ca2efc498 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php @@ -304,7 +304,12 @@ public function testComplete(array $input, array $expectedSuggestions) $environment = new Environment($loader); $application = new Application(); - $application->add(new DebugCommand($environment, $projectDir, [], null, null)); + $command = new DebugCommand($environment, $projectDir, [], null, null); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->find('debug:twig')); $suggestions = $tester->complete($input, 2); @@ -339,7 +344,12 @@ private function createCommandTester(array $paths = [], array $bundleMetadata = } $application = new Application(); - $application->add(new DebugCommand($environment, $projectDir, $bundleMetadata, $defaultPath, null)); + $command = new DebugCommand($environment, $projectDir, $bundleMetadata, $defaultPath, null); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $command = $application->find('debug:twig'); return new CommandTester($command); diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 9e4e23a87e813..39b47d5c3b485 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -179,7 +179,11 @@ private function createCommand(): Command $command = new LintCommand($environment); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return $application->find('lint:twig'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index ce62c9cdf836b..203644c0172d9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()` + 7.3 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php index 274e7b06d3462..8eb3808a5f4df 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Application.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Application.php @@ -159,11 +159,29 @@ public function getLongVersion(): string return parent::getLongVersion().\sprintf(' (env: %s, debug: %s)', $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false'); } + /** + * @deprecated since Symfony 7.4, use Application::addCommand() instead + */ public function add(Command $command): ?Command + { + trigger_deprecation('symfony/framework-bundle', '7.4', 'The "%s()" method is deprecated and will be removed in Symfony 8.0, use "%s::addCommand()" instead.', __METHOD__, self::class); + + return $this->addCommand($command); + } + + public function addCommand(callable|Command $command): ?Command { $this->registerCommands(); - return parent::add($command); + if (!method_exists(BaseApplication::class, 'addCommand')) { + if (!$command instanceof Command) { + throw new \LogicException('Using callables as commands requires symfony/console 7.4 or higher.'); + } + + return parent::add($command); + } + + return parent::addCommand($command); } protected function registerCommands(): void @@ -197,7 +215,7 @@ protected function registerCommands(): void foreach ($container->getParameter('console.command.ids') as $id) { if (!isset($lazyCommandIds[$id])) { try { - $this->add($container->get($id)); + $this->addCommand($container->get($id)); } catch (\Throwable $e) { $this->registrationErrors[] = $e; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/AboutCommand/AboutCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/AboutCommand/AboutCommandTest.php index bcf3c7fe0da76..ee3904be36a7c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/AboutCommand/AboutCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/AboutCommand/AboutCommandTest.php @@ -82,7 +82,7 @@ public function testAboutWithUnreadableFiles() private function createCommandTester(TestAppKernel $kernel): CommandTester { $application = new Application($kernel); - $application->add(new AboutCommand()); + $application->addCommand(new AboutCommand()); return new CommandTester($application->find('about')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolClearCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolClearCommandTest.php index 3a927f217874d..c98d7ed920274 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolClearCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolClearCommandTest.php @@ -36,7 +36,7 @@ protected function setUp(): void public function testComplete(array $input, array $expectedSuggestions) { $application = new Application($this->getKernel()); - $application->add(new CachePoolClearCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo'])); + $application->addCommand(new CachePoolClearCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo'])); $tester = new CommandCompletionTester($application->get('cache:pool:clear')); $suggestions = $tester->complete($input); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php index 3db39e12173e6..b4c11d4db3edd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php @@ -90,7 +90,7 @@ public function testCommandDeleteFailed() public function testComplete(array $input, array $expectedSuggestions) { $application = new Application($this->getKernel()); - $application->add(new CachePoolDeleteCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo'])); + $application->addCommand(new CachePoolDeleteCommand(new Psr6CacheClearer(['foo' => $this->cachePool]), ['foo'])); $tester = new CommandCompletionTester($application->get('cache:pool:delete')); $suggestions = $tester->complete($input); @@ -125,7 +125,7 @@ private function getKernel(): MockObject&KernelInterface private function getCommandTester(KernelInterface $kernel): CommandTester { $application = new Application($kernel); - $application->add(new CachePoolDeleteCommand(new Psr6CacheClearer(['foo' => $this->cachePool]))); + $application->addCommand(new CachePoolDeleteCommand(new Psr6CacheClearer(['foo' => $this->cachePool]))); return new CommandTester($application->find('cache:pool:delete')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php index a2d0ad7fef8f6..18a3622f21455 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php @@ -77,7 +77,7 @@ private function getPruneableInterfaceMock(): MockObject&PruneableInterface private function getCommandTester(KernelInterface $kernel, RewindableGenerator $generator): CommandTester { $application = new Application($kernel); - $application->add(new CachePoolPruneCommand($generator)); + $application->addCommand(new CachePoolPruneCommand($generator)); return new CommandTester($application->find('cache:pool:prune')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php index b6b6771f928ab..97b1859bea321 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php @@ -46,8 +46,8 @@ public function testWithNotMatchPath() private function createCommandTester(): CommandTester { $application = new Application($this->getKernel()); - $application->add(new RouterMatchCommand($this->getRouter())); - $application->add(new RouterDebugCommand($this->getRouter())); + $application->addCommand(new RouterMatchCommand($this->getRouter())); + $application->addCommand(new RouterDebugCommand($this->getRouter())); return new CommandTester($application->find('router:match')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php index c6c91a8574298..1b114ad491b61 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php @@ -223,7 +223,7 @@ function ($path, $catalogue) use ($loadedMessages) { $command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $codePaths, $enabledLocales); $application = new Application($kernel); - $application->add($command); + $application->addCommand($command); return $application->find('debug:translation'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandCompletionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandCompletionTest.php index 6d2f22d96a183..a47b0913f2355 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandCompletionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandCompletionTest.php @@ -132,7 +132,7 @@ function ($path, $catalogue) use ($loadedMessages) { $command = new TranslationExtractCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $codePaths, ['en', 'fr']); $application = new Application($kernel); - $application->add($command); + $application->addCommand($command); return new CommandCompletionTester($application->find('translation:extract')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandTest.php index c5e78de12a3f6..22927d210c32e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationExtractCommandTest.php @@ -304,7 +304,7 @@ function (MessageCatalogue $catalogue) use ($writerMessages) { $command = new TranslationExtractCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates', $transPaths, $codePaths); $application = new Application($kernel); - $application->add($command); + $application->addCommand($command); return new CommandTester($application->find('translation:extract')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/WorkflowDumpCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/WorkflowDumpCommandTest.php index 284e97623ad15..34009756a81e1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/WorkflowDumpCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/WorkflowDumpCommandTest.php @@ -25,7 +25,12 @@ class WorkflowDumpCommandTest extends TestCase public function testComplete(array $input, array $expectedSuggestions) { $application = new Application(); - $application->add(new WorkflowDumpCommand(new ServiceLocator([]))); + $command = new WorkflowDumpCommand(new ServiceLocator([])); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->find('workflow:dump')); $suggestions = $tester->complete($input, 2); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php index d5495ada92e00..ed96fbb00f85f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/XliffLintCommandTest.php @@ -59,7 +59,12 @@ private function createCommandTester($application = null): CommandTester { if (!$application) { $application = new BaseApplication(); - $application->add(new XliffLintCommand()); + $command = new XliffLintCommand(); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } } $command = $application->find('lint:xliff'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php index ec2093119511c..30a73015b66d2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php @@ -107,7 +107,12 @@ private function createCommandTester($application = null): CommandTester { if (!$application) { $application = new BaseApplication(); - $application->add(new YamlLintCommand()); + $command = new YamlLintCommand(); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } } $command = $application->find('lint:yaml'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php index 0b92a813c2d27..7f712107c22b8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php @@ -119,7 +119,7 @@ public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName( $application = new Application($kernel); $newCommand = new Command('example'); - $application->add($newCommand); + $application->addCommand($newCommand); $this->assertSame($newCommand, $application->get('example')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php index a068034344782..45663f0bfeb05 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php @@ -28,7 +28,7 @@ public function testBundlePublicDir() $fs = new Filesystem(); $fs->remove($projectDir); $fs->mkdir($projectDir.'/public'); - $command = (new Application($kernel))->add(new AssetsInstallCommand($fs, $projectDir)); + $command = (new Application($kernel))->addCommand(new AssetsInstallCommand($fs, $projectDir)); $exitCode = (new CommandTester($command))->execute(['target' => $projectDir.'/public']); $this->assertSame(0, $exitCode); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php index dbd78645d881c..a2966b5a244b6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolClearCommandTest.php @@ -146,7 +146,7 @@ public function testExcludedPool() private function createCommandTester(?array $poolNames = null) { $application = new Application(static::$kernel); - $application->add(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer'), $poolNames)); + $application->addCommand(new CachePoolClearCommand(static::getContainer()->get('cache.global_clearer'), $poolNames)); return new CommandTester($application->find('cache:pool:clear')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php index 8e9061845a45e..eec48402628b4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php @@ -46,7 +46,7 @@ public function testEmptyList() private function createCommandTester(array $poolNames) { $application = new Application(static::$kernel); - $application->add(new CachePoolListCommand($poolNames)); + $application->addCommand(new CachePoolListCommand($poolNames)); return new CommandTester($application->find('cache:pool:list')); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php index bd153963632e2..1819e7f4eae4f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php @@ -241,7 +241,7 @@ public function testComplete(bool $debug, array $input, array $expectedSuggestio { $application = $this->createApplication($debug); - $application->add(new ConfigDebugCommand()); + $application->addCommand(new ConfigDebugCommand()); $tester = new CommandCompletionTester($application->get('debug:config')); $suggestions = $tester->complete($input); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php index 8f5930faac2eb..a16d8e0463fb8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDumpReferenceCommandTest.php @@ -132,7 +132,7 @@ public function testComplete(bool $debug, array $input, array $expectedSuggestio { $application = $this->createApplication($debug); - $application->add(new ConfigDumpReferenceCommand()); + $application->addCommand(new ConfigDumpReferenceCommand()); $tester = new CommandCompletionTester($application->get('config:dump-reference')); $suggestions = $tester->complete($input); $this->assertSame($expectedSuggestions, $suggestions); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php index ca11e3faea143..b43a12ed6c9d5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php @@ -122,7 +122,7 @@ public function testNotConfusedByClassAliases() public function testComplete(array $input, array $expectedSuggestions) { $kernel = static::bootKernel(['test_case' => 'ContainerDebug', 'root_config' => 'config.yml']); - $command = (new Application($kernel))->add(new DebugAutowiringCommand()); + $command = (new Application($kernel))->addCommand(new DebugAutowiringCommand()); $tester = new CommandCompletionTester($command); diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index b4539fa1eeb50..f77d57299f4fe 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\CompleteCommand; use Symfony\Component\Console\Command\DumpCompletionCommand; @@ -28,6 +29,7 @@ use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\Console\Exception\ExceptionInterface; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Exception\NamespaceNotFoundException; use Symfony\Component\Console\Exception\RuntimeException; @@ -512,7 +514,7 @@ public function getLongVersion(): string */ public function register(string $name): Command { - return $this->add(new Command($name)); + return $this->addCommand(new Command($name)); } /** @@ -520,25 +522,50 @@ public function register(string $name): Command * * If a Command is not enabled it will not be added. * - * @param Command[] $commands An array of commands + * @param callable[]|Command[] $commands An array of commands */ public function addCommands(array $commands): void { foreach ($commands as $command) { - $this->add($command); + $this->addCommand($command); } } + /** + * @deprecated since Symfony 7.4, use Application::addCommand() instead + */ + public function add(Command $command): ?Command + { + trigger_deprecation('symfony/console', '7.4', 'The "%s()" method is deprecated and will be removed in Symfony 8.0, use "%s::addCommand()" instead.', __METHOD__, self::class); + + return $this->addCommand($command); + } + /** * Adds a command object. * * If a command with the same name already exists, it will be overridden. * If the command is not enabled it will not be added. */ - public function add(Command $command): ?Command + public function addCommand(callable|Command $command): ?Command { $this->init(); + if (!$command instanceof Command) { + if (!\is_object($command) || $command instanceof \Closure) { + throw new InvalidArgumentException(\sprintf('The command must be an instance of "%s" or an invokable object.', Command::class)); + } + + /** @var AsCommand $attribute */ + $attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance() + ?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class)); + + $command = (new Command($attribute->name)) + ->setDescription($attribute->description ?? '') + ->setHelp($attribute->help ?? '') + ->setCode($command); + } + $command->setApplication($this); if (!$command->isEnabled()) { @@ -604,7 +631,7 @@ public function has(string $name): bool { $this->init(); - return isset($this->commands[$name]) || ($this->commandLoader?->has($name) && $this->add($this->commandLoader->get($name))); + return isset($this->commands[$name]) || ($this->commandLoader?->has($name) && $this->addCommand($this->commandLoader->get($name))); } /** @@ -1322,7 +1349,7 @@ private function init(): void $this->initialized = true; foreach ($this->getDefaultCommands() as $command) { - $this->add($command); + $this->addCommand($command); } } } diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 509a9d03c5707..f481d55aa7b36 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -5,6 +5,8 @@ CHANGELOG --- * Allow setting aliases and the hidden flag via the command name passed to the constructor + * Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone + * Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()` 7.3 --- diff --git a/src/Symfony/Component/Console/SingleCommandApplication.php b/src/Symfony/Component/Console/SingleCommandApplication.php index 2b54fb870d244..837948d1287b1 100644 --- a/src/Symfony/Component/Console/SingleCommandApplication.php +++ b/src/Symfony/Component/Console/SingleCommandApplication.php @@ -57,7 +57,7 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu $application->setAutoExit($this->autoExit); // Fix the usage of the command displayed with "--help" $this->setName($_SERVER['argv'][0]); - $application->add($this); + $application->addCommand($this); $application->setDefaultCommand($this->getName(), true); $this->running = true; diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 268f8ba501a9e..e5d16d7fe3b99 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\HelpCommand; +use Symfony\Component\Console\Command\InvokableCommand; use Symfony\Component\Console\Command\LazyCommand; use Symfony\Component\Console\Command\SignalableCommandInterface; use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; @@ -28,6 +29,8 @@ use Symfony\Component\Console\Event\ConsoleSignalEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Component\Console\Exception\CommandNotFoundException; +use Symfony\Component\Console\Exception\InvalidArgumentException; +use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Console\Exception\NamespaceNotFoundException; use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Helper\HelperSet; @@ -163,7 +166,7 @@ public function testAll() $commands = $application->all(); $this->assertInstanceOf(HelpCommand::class, $commands['help'], '->all() returns the registered commands'); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $commands = $application->all('foo'); $this->assertCount(1, $commands, '->all() takes a namespace as its first argument'); } @@ -174,7 +177,7 @@ public function testAllWithCommandLoader() $commands = $application->all(); $this->assertInstanceOf(HelpCommand::class, $commands['help'], '->all() returns the registered commands'); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $commands = $application->all('foo'); $this->assertCount(1, $commands, '->all() takes a namespace as its first argument'); @@ -221,7 +224,7 @@ public function testRegisterAmbiguous() public function testAdd() { $application = new Application(); - $application->add($foo = new \FooCommand()); + $application->addCommand($foo = new \FooCommand()); $commands = $application->all(); $this->assertEquals($foo, $commands['foo:bar'], '->add() registers a command'); @@ -236,7 +239,60 @@ public function testAddCommandWithEmptyConstructor() $this->expectException(\LogicException::class); $this->expectExceptionMessage('Command class "Foo5Command" is not correctly initialized. You probably forgot to call the parent constructor.'); - (new Application())->add(new \Foo5Command()); + (new Application())->addCommand(new \Foo5Command()); + } + + public function testAddCommandWithExtendedCommand() + { + $application = new Application(); + $application->addCommand($foo = new \FooCommand()); + $commands = $application->all(); + + $this->assertEquals($foo, $commands['foo:bar']); + } + + public function testAddCommandWithInvokableCommand() + { + $application = new Application(); + $application->addCommand($foo = new InvokableTestCommand()); + $commands = $application->all(); + + $this->assertInstanceOf(Command::class, $command = $commands['invokable']); + $this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command)); + } + + public function testAddCommandWithInvokableExtendedCommand() + { + $application = new Application(); + $application->addCommand($foo = new InvokableExtendedTestCommand()); + $commands = $application->all(); + + $this->assertEquals($foo, $commands['invokable-extended']); + } + + /** + * @dataProvider provideInvalidInvokableCommands + */ + public function testAddCommandThrowsExceptionOnInvalidCommand(callable $command, string $expectedException, string $expectedExceptionMessage) + { + $application = new Application(); + + $this->expectException($expectedException); + $this->expectExceptionMessage($expectedExceptionMessage); + + $application->addCommand($command); + } + + public static function provideInvalidInvokableCommands(): iterable + { + yield 'a function' => ['strlen', InvalidArgumentException::class, \sprintf('The command must be an instance of "%s" or an invokable object.', Command::class)]; + yield 'a closure' => [function () { + }, InvalidArgumentException::class, \sprintf('The command must be an instance of "%s" or an invokable object.', Command::class)]; + yield 'without the #[AsCommand] attribute' => [new class { + public function __invoke() + { + } + }, LogicException::class, \sprintf('The command must use the "%s" attribute.', AsCommand::class)]; } public function testHasGet() @@ -245,13 +301,13 @@ public function testHasGet() $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered'); $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered'); - $application->add($foo = new \FooCommand()); + $application->addCommand($foo = new \FooCommand()); $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered'); $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name'); $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias'); $application = new Application(); - $application->add($foo = new \FooCommand()); + $application->addCommand($foo = new \FooCommand()); // simulate --help $r = new \ReflectionObject($application); $p = $r->getProperty('wantHelps'); @@ -266,7 +322,7 @@ public function testHasGetWithCommandLoader() $this->assertTrue($application->has('list'), '->has() returns true if a named command is registered'); $this->assertFalse($application->has('afoobar'), '->has() returns false if a named command is not registered'); - $application->add($foo = new \FooCommand()); + $application->addCommand($foo = new \FooCommand()); $this->assertTrue($application->has('afoobar'), '->has() returns true if an alias is registered'); $this->assertEquals($foo, $application->get('foo:bar'), '->get() returns a command by name'); $this->assertEquals($foo, $application->get('afoobar'), '->get() returns a command by alias'); @@ -307,35 +363,35 @@ public function testGetInvalidCommand() public function testGetNamespaces() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); $this->assertEquals(['foo'], $application->getNamespaces(), '->getNamespaces() returns an array of unique used namespaces'); } public function testFindNamespace() { $application = new Application(); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists'); $this->assertEquals('foo', $application->findNamespace('f'), '->findNamespace() finds a namespace given an abbreviation'); - $application->add(new \Foo2Command()); + $application->addCommand(new \Foo2Command()); $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists'); } public function testFindNamespaceWithSubnamespaces() { $application = new Application(); - $application->add(new \FooSubnamespaced1Command()); - $application->add(new \FooSubnamespaced2Command()); + $application->addCommand(new \FooSubnamespaced1Command()); + $application->addCommand(new \FooSubnamespaced2Command()); $this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns commands even if the commands are only contained in subnamespaces'); } public function testFindAmbiguousNamespace() { $application = new Application(); - $application->add(new \BarBucCommand()); - $application->add(new \FooCommand()); - $application->add(new \Foo2Command()); + $application->addCommand(new \BarBucCommand()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo2Command()); $expectedMsg = "The namespace \"f\" is ambiguous.\nDid you mean one of these?\n foo\n foo1"; @@ -348,8 +404,8 @@ public function testFindAmbiguousNamespace() public function testFindNonAmbiguous() { $application = new Application(); - $application->add(new \TestAmbiguousCommandRegistering()); - $application->add(new \TestAmbiguousCommandRegistering2()); + $application->addCommand(new \TestAmbiguousCommandRegistering()); + $application->addCommand(new \TestAmbiguousCommandRegistering2()); $this->assertEquals('test-ambiguous', $application->find('test')->getName()); } @@ -364,9 +420,9 @@ public function testFindInvalidNamespace() public function testFindUniqueNameButNamespaceName() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); $this->expectException(CommandNotFoundException::class); $this->expectExceptionMessage('Command "foo1" is not defined'); @@ -377,7 +433,7 @@ public function testFindUniqueNameButNamespaceName() public function testFind() { $application = new Application(); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $this->assertInstanceOf(\FooCommand::class, $application->find('foo:bar'), '->find() returns a command if its name exists'); $this->assertInstanceOf(HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists'); @@ -389,8 +445,8 @@ public function testFind() public function testFindCaseSensitiveFirst() { $application = new Application(); - $application->add(new \FooSameCaseUppercaseCommand()); - $application->add(new \FooSameCaseLowercaseCommand()); + $application->addCommand(new \FooSameCaseUppercaseCommand()); + $application->addCommand(new \FooSameCaseLowercaseCommand()); $this->assertInstanceOf(\FooSameCaseUppercaseCommand::class, $application->find('f:B'), '->find() returns a command if the abbreviation is the correct case'); $this->assertInstanceOf(\FooSameCaseUppercaseCommand::class, $application->find('f:BAR'), '->find() returns a command if the abbreviation is the correct case'); @@ -401,7 +457,7 @@ public function testFindCaseSensitiveFirst() public function testFindCaseInsensitiveAsFallback() { $application = new Application(); - $application->add(new \FooSameCaseLowercaseCommand()); + $application->addCommand(new \FooSameCaseLowercaseCommand()); $this->assertInstanceOf(\FooSameCaseLowercaseCommand::class, $application->find('f:b'), '->find() returns a command if the abbreviation is the correct case'); $this->assertInstanceOf(\FooSameCaseLowercaseCommand::class, $application->find('f:B'), '->find() will fallback to case insensitivity'); @@ -411,8 +467,8 @@ public function testFindCaseInsensitiveAsFallback() public function testFindCaseInsensitiveSuggestions() { $application = new Application(); - $application->add(new \FooSameCaseLowercaseCommand()); - $application->add(new \FooSameCaseUppercaseCommand()); + $application->addCommand(new \FooSameCaseLowercaseCommand()); + $application->addCommand(new \FooSameCaseUppercaseCommand()); $this->expectException(CommandNotFoundException::class); $this->expectExceptionMessage('Command "FoO:BaR" is ambiguous'); @@ -444,9 +500,9 @@ public function testFindWithAmbiguousAbbreviations($abbreviation, $expectedExcep $this->expectExceptionMessage($expectedExceptionMessage); $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); $application->find($abbreviation); } @@ -476,8 +532,8 @@ public function testFindWithAmbiguousAbbreviationsFindsCommandIfAlternativesAreH { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \FooHiddenCommand()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \FooHiddenCommand()); $this->assertInstanceOf(\FooCommand::class, $application->find('foo:')); } @@ -485,8 +541,8 @@ public function testFindWithAmbiguousAbbreviationsFindsCommandIfAlternativesAreH public function testFindCommandEqualNamespace() { $application = new Application(); - $application->add(new \Foo3Command()); - $application->add(new \Foo4Command()); + $application->addCommand(new \Foo3Command()); + $application->addCommand(new \Foo4Command()); $this->assertInstanceOf(\Foo3Command::class, $application->find('foo3:bar'), '->find() returns the good command even if a namespace has same name'); $this->assertInstanceOf(\Foo4Command::class, $application->find('foo3:bar:toh'), '->find() returns a command even if its namespace equals another command name'); @@ -495,8 +551,8 @@ public function testFindCommandEqualNamespace() public function testFindCommandWithAmbiguousNamespacesButUniqueName() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \FoobarCommand()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \FoobarCommand()); $this->assertInstanceOf(\FoobarCommand::class, $application->find('f:f')); } @@ -504,7 +560,7 @@ public function testFindCommandWithAmbiguousNamespacesButUniqueName() public function testFindCommandWithMissingNamespace() { $application = new Application(); - $application->add(new \Foo4Command()); + $application->addCommand(new \Foo4Command()); $this->assertInstanceOf(\Foo4Command::class, $application->find('f::t')); } @@ -515,7 +571,7 @@ public function testFindCommandWithMissingNamespace() public function testFindAlternativeExceptionMessageSingle($name) { $application = new Application(); - $application->add(new \Foo3Command()); + $application->addCommand(new \Foo3Command()); $this->expectException(CommandNotFoundException::class); $this->expectExceptionMessage('Did you mean this'); @@ -526,7 +582,7 @@ public function testFindAlternativeExceptionMessageSingle($name) public function testDontRunAlternativeNamespaceName() { $application = new Application(); - $application->add(new \Foo1Command()); + $application->addCommand(new \Foo1Command()); $application->setAutoExit(false); $tester = new ApplicationTester($application); $tester->run(['command' => 'foos:bar1'], ['decorated' => false]); @@ -536,7 +592,7 @@ public function testDontRunAlternativeNamespaceName() public function testCanRunAlternativeCommandName() { $application = new Application(); - $application->add(new \FooWithoutAliasCommand()); + $application->addCommand(new \FooWithoutAliasCommand()); $application->setAutoExit(false); $tester = new ApplicationTester($application); $tester->setInputs(['y']); @@ -550,7 +606,7 @@ public function testCanRunAlternativeCommandName() public function testDontRunAlternativeCommandName() { $application = new Application(); - $application->add(new \FooWithoutAliasCommand()); + $application->addCommand(new \FooWithoutAliasCommand()); $application->setAutoExit(false); $tester = new ApplicationTester($application); $tester->setInputs(['n']); @@ -574,9 +630,9 @@ public function testRunNamespace() putenv('COLUMNS=120'); $application = new Application(); $application->setAutoExit(false); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); $tester = new ApplicationTester($application); $tester->run(['command' => 'foo'], ['decorated' => false]); $display = trim($tester->getDisplay(true)); @@ -589,9 +645,9 @@ public function testFindAlternativeExceptionMessageMultiple() { putenv('COLUMNS=120'); $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); // Command + plural try { @@ -614,8 +670,8 @@ public function testFindAlternativeExceptionMessageMultiple() $this->assertMatchesRegularExpression('/foo1/', $e->getMessage()); } - $application->add(new \Foo3Command()); - $application->add(new \Foo4Command()); + $application->addCommand(new \Foo3Command()); + $application->addCommand(new \Foo4Command()); // Subnamespace + plural try { @@ -632,9 +688,9 @@ public function testFindAlternativeCommands() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); try { $application->find($commandName = 'Unknown command'); @@ -669,7 +725,7 @@ public function testFindAlternativeCommandsWithAnAlias() $application->setCommandLoader(new FactoryCommandLoader([ 'foo3' => static fn () => $fooCommand, ])); - $application->add($fooCommand); + $application->addCommand($fooCommand); $result = $application->find('foo'); @@ -680,10 +736,10 @@ public function testFindAlternativeNamespace() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); - $application->add(new \Foo3Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); + $application->addCommand(new \Foo3Command()); try { $application->find('Unknown-namespace:Unknown-command'); @@ -715,11 +771,11 @@ public function testFindAlternativesOutput() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo1Command()); - $application->add(new \Foo2Command()); - $application->add(new \Foo3Command()); - $application->add(new \FooHiddenCommand()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); + $application->addCommand(new \Foo3Command()); + $application->addCommand(new \FooHiddenCommand()); $expectedAlternatives = [ 'afoobar', @@ -755,8 +811,8 @@ public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces() public function testFindWithDoubleColonInNameThrowsException() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \Foo4Command()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo4Command()); $this->expectException(CommandNotFoundException::class); $this->expectExceptionMessage('Command "foo::bar" is not defined.'); @@ -767,7 +823,7 @@ public function testFindWithDoubleColonInNameThrowsException() public function testFindHiddenWithExactName() { $application = new Application(); - $application->add(new \FooHiddenCommand()); + $application->addCommand(new \FooHiddenCommand()); $this->assertInstanceOf(\FooHiddenCommand::class, $application->find('foo:hidden')); $this->assertInstanceOf(\FooHiddenCommand::class, $application->find('afoohidden')); @@ -777,8 +833,8 @@ public function testFindAmbiguousCommandsIfAllAlternativesAreHidden() { $application = new Application(); - $application->add(new \FooCommand()); - $application->add(new \FooHiddenCommand()); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \FooHiddenCommand()); $this->assertInstanceOf(\FooCommand::class, $application->find('foo:')); } @@ -824,7 +880,7 @@ public function testSetCatchErrors(bool $catchExceptions) $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions($catchExceptions); - $application->add((new Command('boom'))->setCode(fn () => throw new \Error('This is an error.'))); + $application->addCommand((new Command('boom'))->setCode(fn () => throw new \Error('This is an error.'))); putenv('COLUMNS=120'); $tester = new ApplicationTester($application); @@ -870,7 +926,7 @@ public function testRenderException() $tester->run(['command' => 'list', '--foo' => true], ['decorated' => false, 'capture_stderr_separately' => true]); $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception2.txt', $tester->getErrorOutput(true), '->renderException() renders the command synopsis when an exception occurs in the context of a command'); - $application->add(new \Foo3Command()); + $application->addCommand(new \Foo3Command()); $tester = new ApplicationTester($application); $tester->run(['command' => 'foo3:bar'], ['decorated' => false, 'capture_stderr_separately' => true]); $this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception3.txt', $tester->getErrorOutput(true), '->renderException() renders a pretty exceptions with previous exceptions'); @@ -1031,7 +1087,7 @@ public function testRun() $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); - $application->add($command = new \Foo1Command()); + $application->addCommand($command = new \Foo1Command()); $_SERVER['argv'] = ['cli.php', 'foo:bar1']; ob_start(); @@ -1116,7 +1172,7 @@ public function testRun() $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $tester = new ApplicationTester($application); $tester->run(['command' => 'foo:bar', '--no-interaction' => true], ['decorated' => false]); @@ -1151,7 +1207,7 @@ public function testVerboseValueNotBreakArguments() $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $output = new StreamOutput(fopen('php://memory', 'w', false)); @@ -1762,7 +1818,7 @@ public function testSetRunCustomDefaultCommand() $application = new Application(); $application->setAutoExit(false); - $application->add($command); + $application->addCommand($command); $application->setDefaultCommand($command->getName()); $tester = new ApplicationTester($application); @@ -1784,7 +1840,7 @@ public function testSetRunCustomDefaultCommandWithOption() $application = new Application(); $application->setAutoExit(false); - $application->add($command); + $application->addCommand($command); $application->setDefaultCommand($command->getName()); $tester = new ApplicationTester($application); @@ -1799,7 +1855,7 @@ public function testSetRunCustomSingleCommand() $application = new Application(); $application->setAutoExit(false); - $application->add($command); + $application->addCommand($command); $application->setDefaultCommand($command->getName(), true); $tester = new ApplicationTester($application); @@ -2150,7 +2206,7 @@ public function testSignalableCommandInterfaceWithoutSignals() $application = new Application(); $application->setAutoExit(false); $application->setDispatcher($dispatcher); - $application->add($command); + $application->addCommand($command); $this->assertSame(0, $application->run(new ArrayInput(['signal']))); } @@ -2186,7 +2242,7 @@ public function testSignalableCommandDoesNotInterruptedOnTermSignals() $application = new Application(); $application->setAutoExit(false); $application->setDispatcher($dispatcher); - $application->add($command); + $application->addCommand($command); $this->assertSame(129, $application->run(new ArrayInput(['signal']))); } @@ -2208,7 +2264,7 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals() $application = new Application(); $application->setAutoExit(false); $application->setDispatcher($dispatcher); - $application->add($command); + $application->addCommand($command); $tester = new ApplicationTester($application); $this->assertSame(51, $tester->run(['signal'])); $expected = <<setAutoExit(false); $application->setDispatcher($dispatcher); - $application->add($command); + $application->addCommand($command); $this->assertSame(0, $application->run(new ArrayInput(['alarm']))); $this->assertFalse($command->signaled); @@ -2459,7 +2515,7 @@ private function createSignalableApplication(Command $command, ?EventDispatcherI if ($dispatcher) { $application->setDispatcher($dispatcher); } - $application->add(new LazyCommand($command->getName(), [], '', false, fn () => $command, true)); + $application->addCommand(new LazyCommand($command->getName(), [], '', false, fn () => $command, true)); return $application; } @@ -2491,7 +2547,7 @@ public function __construct() parent::__construct(); $command = new \FooCommand(); - $this->add($command); + $this->addCommand($command); $this->setDefaultCommand($command->getName()); } } @@ -2514,6 +2570,22 @@ public function isEnabled(): bool } } +#[AsCommand(name: 'invokable')] +class InvokableTestCommand +{ + public function __invoke(): int + { + } +} + +#[AsCommand(name: 'invokable-extended')] +class InvokableExtendedTestCommand extends Command +{ + public function __invoke(): int + { + } +} + #[AsCommand(name: 'signal')] class BaseSignableCommand extends Command { diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 85442c7b9243e..a3ecee43eea6c 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -50,7 +50,7 @@ public function testCommandNameCannotBeEmpty() { $this->expectException(\LogicException::class); $this->expectExceptionMessage('The command defined in "Symfony\Component\Console\Command\Command" cannot have an empty name.'); - (new Application())->add(new Command()); + (new Application())->addCommand(new Command()); } public function testSetApplication() @@ -190,7 +190,7 @@ public function testGetProcessedHelp() $command = new \TestCommand(); $command->setHelp('The %command.name% command does... Example: %command.full_name%.'); $application = new Application(); - $application->add($command); + $application->addCommand($command); $application->setDefaultCommand('namespace:name', true); $this->assertStringContainsString('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly in single command applications'); $this->assertStringNotContainsString('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name% in single command applications'); diff --git a/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php b/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php index 75519eb49e5e3..08f6b046ff7e4 100644 --- a/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CompleteCommandTest.php @@ -33,7 +33,7 @@ protected function setUp(): void $this->command = new CompleteCommand(); $this->application = new Application(); - $this->application->add(new CompleteCommandTest_HelloCommand()); + $this->application->addCommand(new CompleteCommandTest_HelloCommand()); $this->command->setApplication($this->application); $this->tester = new CommandTester($this->command); diff --git a/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php b/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php index c36ab62df02c1..f1979c0dc8475 100644 --- a/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/HelpCommandTest.php @@ -77,7 +77,7 @@ public function testComplete(array $input, array $expectedSuggestions) { require_once realpath(__DIR__.'/../Fixtures/FooCommand.php'); $application = new Application(); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $tester = new CommandCompletionTester($application->get('help')); $suggestions = $tester->complete($input, 2); $this->assertSame($expectedSuggestions, $suggestions); diff --git a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php index a6ffc8ab5bbc9..37496c6b33bb2 100644 --- a/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/ListCommandTest.php @@ -54,7 +54,7 @@ public function testExecuteListsCommandsWithNamespaceArgument() { require_once realpath(__DIR__.'/../Fixtures/FooCommand.php'); $application = new Application(); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $commandTester = new CommandTester($command = $application->get('list')); $commandTester->execute(['command' => $command->getName(), 'namespace' => 'foo', '--raw' => true]); $output = <<<'EOF' @@ -69,7 +69,7 @@ public function testExecuteListsCommandsOrder() { require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php'); $application = new Application(); - $application->add(new \Foo6Command()); + $application->addCommand(new \Foo6Command()); $commandTester = new CommandTester($command = $application->get('list')); $commandTester->execute(['command' => $command->getName()], ['decorated' => false]); $output = <<<'EOF' @@ -102,7 +102,7 @@ public function testExecuteListsCommandsOrderRaw() { require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php'); $application = new Application(); - $application->add(new \Foo6Command()); + $application->addCommand(new \Foo6Command()); $commandTester = new CommandTester($command = $application->get('list')); $commandTester->execute(['command' => $command->getName(), '--raw' => true]); $output = <<<'EOF' @@ -122,7 +122,7 @@ public function testComplete(array $input, array $expectedSuggestions) { require_once realpath(__DIR__.'/../Fixtures/FooCommand.php'); $application = new Application(); - $application->add(new \FooCommand()); + $application->addCommand(new \FooCommand()); $tester = new CommandCompletionTester($application->get('list')); $suggestions = $tester->complete($input, 2); $this->assertSame($expectedSuggestions, $suggestions); diff --git a/src/Symfony/Component/Console/Tests/ConsoleEventsTest.php b/src/Symfony/Component/Console/Tests/ConsoleEventsTest.php index 408f8c0d35c58..3421eda805251 100644 --- a/src/Symfony/Component/Console/Tests/ConsoleEventsTest.php +++ b/src/Symfony/Component/Console/Tests/ConsoleEventsTest.php @@ -58,7 +58,7 @@ public function testEventAliases() ->setPublic(true) ->addMethodCall('setAutoExit', [false]) ->addMethodCall('setDispatcher', [new Reference('event_dispatcher')]) - ->addMethodCall('add', [new Reference('failing_command')]) + ->addMethodCall('addCommand', [new Reference('failing_command')]) ; $container->compile(); diff --git a/src/Symfony/Component/Console/Tests/Descriptor/ApplicationDescriptionTest.php b/src/Symfony/Component/Console/Tests/Descriptor/ApplicationDescriptionTest.php index 1933c985cbad7..ab90320cd6846 100644 --- a/src/Symfony/Component/Console/Tests/Descriptor/ApplicationDescriptionTest.php +++ b/src/Symfony/Component/Console/Tests/Descriptor/ApplicationDescriptionTest.php @@ -25,7 +25,7 @@ public function testGetNamespaces(array $expected, array $names) { $application = new TestApplication(); foreach ($names as $name) { - $application->add(new Command($name)); + $application->addCommand(new Command($name)); } $this->assertSame($expected, array_keys((new ApplicationDescription($application))->getNamespaces())); diff --git a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php index 7bb02fa54c1ff..c755bab383efe 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php +++ b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php @@ -18,9 +18,9 @@ class DescriptorApplication2 extends Application public function __construct() { parent::__construct('My Symfony application', 'v1.0'); - $this->add(new DescriptorCommand1()); - $this->add(new DescriptorCommand2()); - $this->add(new DescriptorCommand3()); - $this->add(new DescriptorCommand4()); + $this->addCommand(new DescriptorCommand1()); + $this->addCommand(new DescriptorCommand2()); + $this->addCommand(new DescriptorCommand3()); + $this->addCommand(new DescriptorCommand4()); } } diff --git a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplicationMbString.php b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplicationMbString.php index bf170c449f51e..a76e0e181047f 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplicationMbString.php +++ b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplicationMbString.php @@ -19,6 +19,6 @@ public function __construct() { parent::__construct('MbString åpplicätion'); - $this->add(new DescriptorCommandMbString()); + $this->addCommand(new DescriptorCommandMbString()); } } diff --git a/src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php b/src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php index cfdebe4d88da8..d1fb20ac5f046 100644 --- a/src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php +++ b/src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php @@ -104,7 +104,7 @@ public function testCommandFromApplication() return 0; }); - $application->add($command); + $application->addCommand($command); $tester = new CommandTester($application->find('foo')); diff --git a/src/Symfony/Component/Console/Tests/phpt/alarm/command_exit.phpt b/src/Symfony/Component/Console/Tests/phpt/alarm/command_exit.phpt index c2cf3edc7d1c0..a53af85672709 100644 --- a/src/Symfony/Component/Console/Tests/phpt/alarm/command_exit.phpt +++ b/src/Symfony/Component/Console/Tests/phpt/alarm/command_exit.phpt @@ -53,7 +53,7 @@ class MyCommand extends Command $app = new Application(); $app->setDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher()); -$app->add(new MyCommand('foo')); +$app->addCommand(new MyCommand('foo')); $app ->setDefaultCommand('foo', true) diff --git a/src/Symfony/Component/Console/Tests/phpt/signal/command_exit.phpt b/src/Symfony/Component/Console/Tests/phpt/signal/command_exit.phpt index e14f80c47afee..e653d65c1a0d6 100644 --- a/src/Symfony/Component/Console/Tests/phpt/signal/command_exit.phpt +++ b/src/Symfony/Component/Console/Tests/phpt/signal/command_exit.phpt @@ -45,7 +45,7 @@ class MyCommand extends Command $app = new Application(); $app->setDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher()); -$app->add(new MyCommand('foo')); +$app->addCommand(new MyCommand('foo')); $app ->setDefaultCommand('foo', true) diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index 28c0b48ca46fa..57828291ae86d 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -288,7 +288,11 @@ public function testCompletion() $command = new DebugCommand($env, $projectDirectory); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('debug:dotenv')); $this->assertSame(['FOO', 'TEST'], $tester->complete([''])); } diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php index 44fc304c5ef8f..d2f2dfecb4dc7 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DotenvDumpCommandTest.php @@ -95,7 +95,12 @@ public function testExecuteTestEnvs() private function createCommand(): CommandTester { $application = new Application(); - $application->add(new DotenvDumpCommand(__DIR__)); + $command = new DotenvDumpCommand(__DIR__); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return new CommandTester($application->find('dotenv:dump')); } diff --git a/src/Symfony/Component/ErrorHandler/Tests/Command/ErrorDumpCommandTest.php b/src/Symfony/Component/ErrorHandler/Tests/Command/ErrorDumpCommandTest.php index 670adbdb12907..0a0ae20b9c91c 100644 --- a/src/Symfony/Component/ErrorHandler/Tests/Command/ErrorDumpCommandTest.php +++ b/src/Symfony/Component/ErrorHandler/Tests/Command/ErrorDumpCommandTest.php @@ -102,11 +102,16 @@ private function getCommandTester(KernelInterface $kernel): CommandTester $entrypointLookup = $this->createMock(EntrypointLookupInterface::class); $application = new Application($kernel); - $application->add(new ErrorDumpCommand( + $command = new ErrorDumpCommand( new Filesystem(), $errorRenderer, $entrypointLookup, - )); + ); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return new CommandTester($application->find('error:dump')); } diff --git a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php index cac92addbf790..c20c72d8d2aa2 100644 --- a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php @@ -194,7 +194,11 @@ public function testComplete(array $input, array $expectedSuggestions) $formRegistry = new FormRegistry([], new ResolvedFormTypeFactory()); $command = new DebugCommand($formRegistry); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('debug:form')); $this->assertSame($expectedSuggestions, $tester->complete($input)); } @@ -278,7 +282,11 @@ private function createCommandTester(array $namespaces = ['Symfony\Component\For $formRegistry = new FormRegistry([], new ResolvedFormTypeFactory()); $command = new DebugCommand($formRegistry, $namespaces, $types); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return new CommandTester($application->find('debug:form')); } diff --git a/src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php b/src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php index 7790e074ad609..48d4a2f5a1b8d 100644 --- a/src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php +++ b/src/Symfony/Component/Messenger/Tests/Command/ConsumeMessagesCommandTest.php @@ -59,7 +59,11 @@ public function testBasicRun() $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher()); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $tester->execute([ 'receivers' => ['dummy-receiver'], @@ -89,7 +93,11 @@ public function testRunWithBusOption() $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher()); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $tester->execute([ 'receivers' => ['dummy-receiver'], @@ -132,7 +140,11 @@ public function testRunWithResetServicesOption(bool $shouldReset) $command = new ConsumeMessagesCommand($bus, $receiverLocator, new EventDispatcher(), null, [], new ResetServicesListener($servicesResetter)); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $tester->execute(array_merge([ 'receivers' => ['dummy-receiver'], @@ -156,7 +168,11 @@ public function testRunWithInvalidOption(string $option, string $value, string $ $command = new ConsumeMessagesCommand(new RoutableMessageBus(new Container()), $receiverLocator, new EventDispatcher()); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $this->expectException(InvalidOptionException::class); @@ -194,7 +210,11 @@ public function testRunWithTimeLimit() $command = new ConsumeMessagesCommand(new RoutableMessageBus($busLocator), $receiverLocator, new EventDispatcher()); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $tester->execute([ 'receivers' => ['dummy-receiver'], @@ -232,7 +252,11 @@ public function testRunWithAllOption() ); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->get('messenger:consume')); $tester->execute([ '--all' => true, diff --git a/src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php index f74661dc5ad1b..55e430c04497f 100644 --- a/src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Messenger/Tests/Command/DebugCommandTest.php @@ -176,7 +176,11 @@ public function testComplete(array $input, array $expectedSuggestions) { $command = new DebugCommand(['command_bus' => [], 'query_bus' => []]); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('debug:messenger')); $this->assertSame($expectedSuggestions, $tester->complete($input)); } diff --git a/src/Symfony/Component/Runtime/SymfonyRuntime.php b/src/Symfony/Component/Runtime/SymfonyRuntime.php index 4035f28c806cd..4667bbdfba24f 100644 --- a/src/Symfony/Component/Runtime/SymfonyRuntime.php +++ b/src/Symfony/Component/Runtime/SymfonyRuntime.php @@ -162,7 +162,11 @@ public function getRunner(?object $application): RunnerInterface if (!$application->getName() || !$console->has($application->getName())) { $application->setName($_SERVER['argv'][0]); - $console->add($application); + if (method_exists($console, 'addCommand')) { + $console->addCommand($application); + } else { + $console->add($application); + } } $console->setDefaultCommand($application->getName(), true); diff --git a/src/Symfony/Component/Runtime/Tests/phpt/application.php b/src/Symfony/Component/Runtime/Tests/phpt/application.php index ca2de555edfb7..b51947c2afaf1 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/application.php +++ b/src/Symfony/Component/Runtime/Tests/phpt/application.php @@ -25,7 +25,11 @@ }); $app = new Application(); - $app->add($command); + if (method_exists($app, 'addCommand')) { + $app->addCommand($command); + } else { + $app->add($command); + } $app->setDefaultCommand('go', true); return $app; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/command_list.php b/src/Symfony/Component/Runtime/Tests/phpt/command_list.php index 929b4401e86b9..aa40eda627151 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/command_list.php +++ b/src/Symfony/Component/Runtime/Tests/phpt/command_list.php @@ -23,7 +23,11 @@ $command->setName('my_command'); [$cmd, $args] = $runtime->getResolver(require __DIR__.'/command.php')->resolve(); - $app->add($cmd(...$args)); + if (method_exists($app, 'addCommand')) { + $app->addCommand($cmd(...$args)); + } else { + $app->add($cmd(...$args)); + } return $app; }; diff --git a/src/Symfony/Component/Translation/Tests/Command/TranslationLintCommandTest.php b/src/Symfony/Component/Translation/Tests/Command/TranslationLintCommandTest.php index 26d46d90d5415..5dad11d02d035 100644 --- a/src/Symfony/Component/Translation/Tests/Command/TranslationLintCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/TranslationLintCommandTest.php @@ -138,7 +138,11 @@ private function createCommand(Translator $translator, array $enabledLocales): C $command = new TranslationLintCommand($translator, $enabledLocales); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return $command; } diff --git a/src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php b/src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php index c8ecf1cf9ae86..223703804a510 100644 --- a/src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php @@ -695,7 +695,12 @@ public function testPullMessagesMultipleDomains() public function testComplete(array $input, array $expectedSuggestions) { $application = new Application(); - $application->add($this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], 'en', ['loco', 'crowdin', 'lokalise'])); + $command = $this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], 'en', ['loco', 'crowdin', 'lokalise']); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('translation:pull')); $suggestions = $tester->complete($input); @@ -724,7 +729,11 @@ private function createCommandTester(ProviderInterface $provider, array $locales { $command = $this->createCommand($provider, $locales, $domains, $defaultLocale); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return new CommandTester($application->find('translation:pull')); } diff --git a/src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php b/src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php index 44cc569cfa276..5e113e1b116c0 100644 --- a/src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php @@ -361,7 +361,11 @@ public function testPushWithProviderDomains() ); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandTester($application->find('translation:push')); $tester->execute(['--locales' => ['en', 'fr']]); @@ -375,7 +379,12 @@ public function testPushWithProviderDomains() public function testComplete(array $input, array $expectedSuggestions) { $application = new Application(); - $application->add($this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], ['loco', 'crowdin', 'lokalise'])); + $command = $this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], ['loco', 'crowdin', 'lokalise']); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('translation:push')); $suggestions = $tester->complete($input); @@ -404,7 +413,11 @@ private function createCommandTester(ProviderInterface $provider, array $locales { $command = $this->createCommand($provider, $locales, $domains); $application = new Application(); - $application->add($command); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return new CommandTester($application->find('translation:push')); } diff --git a/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php b/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php index 7b9fd1ae35b9d..b78ade960be7b 100644 --- a/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php +++ b/src/Symfony/Component/Translation/Tests/Command/XliffLintCommandTest.php @@ -210,7 +210,12 @@ private function createCommand($requireStrictFileNames = true, $application = nu { if (!$application) { $application = new Application(); - $application->add(new XliffLintCommand(null, null, null, $requireStrictFileNames)); + $command = new XliffLintCommand(null, null, null, $requireStrictFileNames); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } } $command = $application->find('lint:xliff'); diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php index 7976b9e064fc1..f077e8e9e284a 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUlidCommandTest.php @@ -109,7 +109,12 @@ public function testUlidsAreDifferentWhenGeneratingSeveralNow() public function testComplete(array $input, array $expectedSuggestions) { $application = new Application(); - $application->add(new GenerateUlidCommand()); + $command = new GenerateUlidCommand(); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('ulid:generate')); $suggestions = $tester->complete($input, 2); $this->assertSame($expectedSuggestions, $suggestions); diff --git a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php index afea7873f8f0e..72d38febe643a 100644 --- a/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php +++ b/src/Symfony/Component/Uid/Tests/Command/GenerateUuidCommandTest.php @@ -238,7 +238,12 @@ public function testNamespacePredefinedKeyword() public function testComplete(array $input, array $expectedSuggestions) { $application = new Application(); - $application->add(new GenerateUuidCommand()); + $command = new GenerateUuidCommand(); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } $tester = new CommandCompletionTester($application->get('uuid:generate')); $suggestions = $tester->complete($input, 2); $this->assertSame($expectedSuggestions, $suggestions); diff --git a/src/Symfony/Component/VarDumper/Resources/bin/var-dump-server b/src/Symfony/Component/VarDumper/Resources/bin/var-dump-server index f398fcef72d39..3e04aeb2d5b84 100755 --- a/src/Symfony/Component/VarDumper/Resources/bin/var-dump-server +++ b/src/Symfony/Component/VarDumper/Resources/bin/var-dump-server @@ -60,8 +60,13 @@ $app->getDefinition()->addOption( new InputOption('--host', null, InputOption::VALUE_REQUIRED, 'The address the server should listen to', $defaultHost) ); -$app->add($command = new ServerDumpCommand(new DumpServer($host, $logger))) - ->getApplication() +$command = new ServerDumpCommand(new DumpServer($host, $logger)); +if (method_exists($app, 'addCommand')) { + $app->addCommand($command); +} else { + $app->add($command); +} +$app ->setDefaultCommand($command->getName(), true) ->run($input, $output) ; diff --git a/src/Symfony/Component/Yaml/Resources/bin/yaml-lint b/src/Symfony/Component/Yaml/Resources/bin/yaml-lint index 143869e018148..eca04976f36b6 100755 --- a/src/Symfony/Component/Yaml/Resources/bin/yaml-lint +++ b/src/Symfony/Component/Yaml/Resources/bin/yaml-lint @@ -42,8 +42,13 @@ if (!class_exists(Application::class)) { exit(1); } -(new Application())->add($command = new LintCommand()) - ->getApplication() +$command = new LintCommand(); +if (method_exists($app = new Application(), 'addCommand')) { + $app->addCommand($command); +} else { + $app->add($command); +} +$app ->setDefaultCommand($command->getName(), true) ->run() ; diff --git a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php index a501f48d09e37..856f82cae8105 100644 --- a/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Component/Yaml/Tests/Command/LintCommandTest.php @@ -180,7 +180,12 @@ private function createFile($content): string protected function createCommand(): Command { $application = new Application(); - $application->add(new LintCommand()); + $command = new LintCommand(); + if (method_exists($application, 'addCommand')) { + $application->addCommand($command); + } else { + $application->add($command); + } return $application->find('lint:yaml'); }