TYPO3 CMS  TYPO3_7-6
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 = null;
33 
37  protected $shortCommandIdentifiers = null;
38 
42  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
43  {
44  $this->objectManager = $objectManager;
45  }
46 
53  public function getAvailableCommands()
54  {
55  if ($this->availableCommands === null) {
56  $this->availableCommands = [];
57  $commandControllerClassNames = is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers']) ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] : [];
58  foreach ($commandControllerClassNames as $className) {
59  if (!class_exists($className)) {
60  continue;
61  }
62  foreach (get_class_methods($className) as $methodName) {
63  if (substr($methodName, -7, 7) === 'Command') {
64  $this->availableCommands[] = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\Command::class, $className, substr($methodName, 0, -7));
65  }
66  }
67  }
68  }
70  }
71 
83  public function getCommandByIdentifier($commandIdentifier)
84  {
85  $commandIdentifier = strtolower(trim($commandIdentifier));
86  if ($commandIdentifier === 'help') {
87  $commandIdentifier = 'extbase:help:help';
88  }
89  $matchedCommands = [];
91  foreach ($availableCommands as $command) {
92  if ($this->commandMatchesIdentifier($command, $commandIdentifier)) {
93  $matchedCommands[] = $command;
94  }
95  }
96  if (empty($matchedCommands)) {
97  throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('No command could be found that matches the command identifier "' . $commandIdentifier . '".', 1310556663);
98  }
99  if (count($matchedCommands) > 1) {
100  throw new \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException('More than one command matches the command identifier "' . $commandIdentifier . '"', 1310557169, null, $matchedCommands);
101  }
102  return current($matchedCommands);
103  }
104 
112  public function getShortestIdentifierForCommand(Command $command)
113  {
114  if ($command->getCommandIdentifier() === 'extbase:help:help') {
115  return 'help';
116  }
118  if (!isset($shortCommandIdentifiers[$command->getCommandIdentifier()])) {
119  $command->getCommandIdentifier();
120  }
122  }
123 
129  protected function getShortCommandIdentifiers()
130  {
131  if ($this->shortCommandIdentifiers === null) {
132  $commandsByCommandName = [];
133  foreach ($this->getAvailableCommands() as $availableCommand) {
134  list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
135  if (!isset($commandsByCommandName[$commandName])) {
136  $commandsByCommandName[$commandName] = [];
137  }
138  if (!isset($commandsByCommandName[$commandName][$controllerName])) {
139  $commandsByCommandName[$commandName][$controllerName] = [];
140  }
141  $commandsByCommandName[$commandName][$controllerName][] = $extensionKey;
142  }
143  foreach ($this->getAvailableCommands() as $availableCommand) {
144  list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
145  if (count($commandsByCommandName[$commandName][$controllerName]) > 1) {
146  $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s:%s', $extensionKey, $controllerName, $commandName);
147  } else {
148  $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s', $controllerName, $commandName);
149  }
150  }
151  }
153  }
154 
163  protected function commandMatchesIdentifier(Command $command, $commandIdentifier)
164  {
165  $commandIdentifierParts = explode(':', $command->getCommandIdentifier());
166  $searchedCommandIdentifierParts = explode(':', $commandIdentifier);
167  $extensionKey = array_shift($commandIdentifierParts);
168  if (count($searchedCommandIdentifierParts) === 3) {
169  $searchedExtensionKey = array_shift($searchedCommandIdentifierParts);
170  if ($searchedExtensionKey !== $extensionKey) {
171  return false;
172  }
173  }
174  if (count($searchedCommandIdentifierParts) !== 2) {
175  return false;
176  }
177  return $searchedCommandIdentifierParts === $commandIdentifierParts;
178  }
179 }
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
commandMatchesIdentifier(Command $command, $commandIdentifier)