‪TYPO3CMS  9.5
CommandManager.php
Go to the documentation of this file.
1 <?php
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 
23 {
27  protected ‪$objectManager;
28 
32  protected ‪$availableCommands;
33 
38 
42  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
43  {
44  $this->objectManager = ‪$objectManager;
45  }
46 
52  public function ‪getAvailableCommands()
53  {
54  if ($this->availableCommands === null) {
55  $this->availableCommands = [];
56  foreach (‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] ?? [] as $className) {
57  if (!class_exists($className)) {
58  continue;
59  }
60  foreach (get_class_methods($className) as $methodName) {
61  if (substr($methodName, -7, 7) === 'Command') {
62  $this->availableCommands[] = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\Command::class, $className, substr($methodName, 0, -7));
63  }
64  }
65  }
66  }
68  }
69 
80  public function ‪getCommandByIdentifier($commandIdentifier)
81  {
82  $commandIdentifier = strtolower(trim($commandIdentifier));
83  if ($commandIdentifier === 'help') {
84  $commandIdentifier = 'extbase:help:help';
85  }
86  $matchedCommands = [];
88  foreach (‪$availableCommands as $command) {
89  if ($this->‪commandMatchesIdentifier($command, $commandIdentifier)) {
90  $matchedCommands[] = $command;
91  }
92  }
93  if (empty($matchedCommands)) {
94  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('No command could be found that matches the command identifier "' . $commandIdentifier . '".', 1310556663);
95  }
96  if (count($matchedCommands) > 1) {
97  throw new \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException('More than one command matches the command identifier "' . $commandIdentifier . '"', 1310557169, null, $matchedCommands);
98  }
99  return current($matchedCommands);
100  }
101 
108  public function ‪getShortestIdentifierForCommand(‪Command $command)
109  {
110  if ($command->‪getCommandIdentifier() === 'extbase:help:help') {
111  return 'help';
112  }
114  if (!isset(‪$shortCommandIdentifiers[$command->‪getCommandIdentifier()])) {
115  $command->‪getCommandIdentifier();
116  }
118  }
119 
125  protected function ‪getShortCommandIdentifiers()
126  {
127  if ($this->shortCommandIdentifiers === null) {
128  $commandsByCommandName = [];
129  foreach ($this->‪getAvailableCommands() as $availableCommand) {
130  list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
131  if (!isset($commandsByCommandName[$commandName])) {
132  $commandsByCommandName[$commandName] = [];
133  }
134  if (!isset($commandsByCommandName[$commandName][$controllerName])) {
135  $commandsByCommandName[$commandName][$controllerName] = [];
136  }
137  $commandsByCommandName[$commandName][$controllerName][] = $extensionKey;
138  }
139  foreach ($this->‪getAvailableCommands() as $availableCommand) {
140  list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
141  if (count($commandsByCommandName[$commandName][$controllerName]) > 1) {
142  $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s:%s', $extensionKey, $controllerName, $commandName);
143  } else {
144  $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s', $controllerName, $commandName);
145  }
146  }
147  }
149  }
150 
159  protected function ‪commandMatchesIdentifier(‪Command $command, $commandIdentifier)
160  {
161  $commandIdentifierParts = explode(':', $command->‪getCommandIdentifier());
162  $searchedCommandIdentifierParts = explode(':', $commandIdentifier);
163  $extensionKey = array_shift($commandIdentifierParts);
164  if (count($searchedCommandIdentifierParts) === 3) {
165  $searchedExtensionKey = array_shift($searchedCommandIdentifierParts);
166  if ($searchedExtensionKey !== $extensionKey) {
167  return false;
168  }
169  }
170  if (count($searchedCommandIdentifierParts) !== 2) {
171  return false;
172  }
173  return $searchedCommandIdentifierParts === $commandIdentifierParts;
174  }
175 }
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\getAvailableCommands
‪Command[] getAvailableCommands()
Definition: CommandManager.php:49
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getCommandIdentifier
‪string getCommandIdentifier()
Definition: Command.php:139
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\getShortestIdentifierForCommand
‪string getShortestIdentifierForCommand(Command $command)
Definition: CommandManager.php:105
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\$shortCommandIdentifiers
‪array $shortCommandIdentifiers
Definition: CommandManager.php:34
‪TYPO3\CMS\Extbase\Mvc\Cli
Definition: Command.php:2
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\getShortCommandIdentifiers
‪array getShortCommandIdentifiers()
Definition: CommandManager.php:122
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: CommandManager.php:26
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\commandMatchesIdentifier
‪bool commandMatchesIdentifier(Command $command, $commandIdentifier)
Definition: CommandManager.php:156
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\getCommandByIdentifier
‪Command getCommandByIdentifier($commandIdentifier)
Definition: CommandManager.php:77
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
Definition: CommandManager.php:23
‪TYPO3\CMS\Extbase\Mvc\Cli\Command
Definition: Command.php:25
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\$availableCommands
‪array<\TYPO3\CMS\Extbase\Mvc\Cli\Command > $availableCommands
Definition: CommandManager.php:30
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Cli\CommandManager\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: CommandManager.php:39