[BUGFIX] Do not depend on a database connection in CLI authentication
[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\Package\FailsafePackageManager;
20 use TYPO3\CMS\Core\Package\PackageManager;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Core\Bootstrap;
23 use TYPO3\CMS\Extbase\Mvc\Cli\CommandManager;
24 use TYPO3\CMS\Extbase\Object\ObjectManager;
25
26 /**
27 * Main call to register any Extbase command from Extbase command controllers
28 *
29 * Fetches all registered Extbase commands and adds them to the application as custom Extbase commands
30 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0. Use symfony/console commands instead.
31 */
32 class CoreCommand extends Command
33 {
34 /**
35 * @var Bootstrap
36 */
37 protected $extbaseBootstrap;
38
39 /**
40 * Configure the command, since this is a command
41 */
42 protected function configure()
43 {
44 $this->setHidden(true);
45 }
46
47 /**
48 * Sets the application instance for this command.
49 * This is done in setApplication() because configure() is called too early to do it in that place.
50 * The method 'setApplication()' is done right afterwards but has the application object to call.
51 * Then registers additional commands that act as wrappers to the actual Extbase commands.
52 *
53 * @param Application $application An Application instance
54 */
55 public function setApplication(Application $application = null)
56 {
57 parent::setApplication($application);
58
59 // Extbase commands can not be initialized in failsafe mode
60 if (GeneralUtility::makeInstance(PackageManager::class) instanceof FailsafePackageManager) {
61 return;
62 }
63
64 // Find any registered Extbase commands
65 $this->extbaseBootstrap = GeneralUtility::makeInstance(Bootstrap::class);
66 $this->extbaseBootstrap->initialize([]);
67
68 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
69
70 /** @var CommandManager $commandManager */
71 $commandManager = $objectManager->get(CommandManager::class);
72 $commands = $commandManager->getAvailableCommands();
73 foreach ($commands as $command) {
74 $commandName = $commandManager->getShortestIdentifierForCommand($command);
75 $fullCommandName = $command->getCommandIdentifier();
76 if ($fullCommandName === 'extbase:help:error' || $fullCommandName === 'extbase:help:helpstub') {
77 continue;
78 }
79 if ($commandName === 'help') {
80 $commandName = 'extbase:help';
81 }
82 $extbaseCommand = GeneralUtility::makeInstance(ExtbaseCommand::class, $fullCommandName);
83
84 if ($commandName !== $fullCommandName) {
85 $extbaseCommand->setAliases([$commandName]);
86 }
87
88 $extbaseCommand->setExtbaseCommand($command);
89 $this->getApplication()->add($extbaseCommand);
90 }
91 }
92 }