[TASK] Deprecate Extbase CommandControllers and @cli annotation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Command / CoreCommand.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Command;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Symfony\Component\Console\Application;
18 use Symfony\Component\Console\Command\Command;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Core\Bootstrap;
21 use TYPO3\CMS\Extbase\Mvc\Cli\CommandManager;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
23
24 /**
25 * Main call to register any Extbase command from Extbase command controllers
26 *
27 * Fetches all registered Extbase commands and adds them to the application as custom Extbase commands
28 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0. Use symfony/console commands instead.
29 */
30 class CoreCommand extends Command
31 {
32 /**
33 * @var Bootstrap
34 */
35 protected $extbaseBootstrap;
36
37 /**
38 * Configure the command, since this is a command
39 */
40 protected function configure()
41 {
42 $this->setHidden(true);
43 }
44
45 /**
46 * Sets the application instance for this command.
47 * This is done in setApplication() because configure() is called too early to do it in that place.
48 * The method 'setApplication()' is done right afterwards but has the application object to call.
49 * Then registers additional commands that act as wrappers to the actual Extbase commands.
50 *
51 * @param Application $application An Application instance
52 */
53 public function setApplication(Application $application = null)
54 {
55 parent::setApplication($application);
56
57 // Find any registered Extbase commands
58 $this->extbaseBootstrap = GeneralUtility::makeInstance(Bootstrap::class);
59 $this->extbaseBootstrap->initialize([]);
60
61 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
62
63 /** @var CommandManager $commandManager */
64 $commandManager = $objectManager->get(CommandManager::class);
65 $commands = $commandManager->getAvailableCommands();
66 foreach ($commands as $command) {
67 $commandName = $commandManager->getShortestIdentifierForCommand($command);
68 $fullCommandName = $command->getCommandIdentifier();
69 if ($fullCommandName === 'extbase:help:error' || $fullCommandName === 'extbase:help:helpstub') {
70 continue;
71 }
72 if ($commandName === 'help') {
73 $commandName = 'extbase:help';
74 }
75 $extbaseCommand = GeneralUtility::makeInstance(ExtbaseCommand::class, $fullCommandName);
76
77 if ($commandName !== $fullCommandName) {
78 $extbaseCommand->setAliases([$commandName]);
79 }
80
81 $extbaseCommand->setExtbaseCommand($command);
82 $this->getApplication()->add($extbaseCommand);
83 }
84 }
85 }