[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / CommandManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
3
4 /***************************************************************
5 * Copyright notice
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of TYPO3 Flow.
9 * All credits go to the TYPO3 Flow team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * A helper for CLI commands
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class CommandManager implements \TYPO3\CMS\Core\SingletonInterface {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 * @inject
37 */
38 protected $objectManager;
39
40 /**
41 * @var array<\TYPO3\CMS\Extbase\Mvc\Cli\Command>
42 */
43 protected $availableCommands = NULL;
44
45 /**
46 * @var array
47 */
48 protected $shortCommandIdentifiers = NULL;
49
50 /**
51 * Returns an array of all commands
52 *
53 * @return array<\TYPO3\CMS\Extbase\Mvc\Cli\Command>
54 * @api
55 */
56 public function getAvailableCommands() {
57 if ($this->availableCommands === NULL) {
58 $this->availableCommands = array();
59 $commandControllerClassNames = is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers']) ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] : array();
60 foreach ($commandControllerClassNames as $className) {
61 if (!class_exists($className)) {
62 continue;
63 }
64 foreach (get_class_methods($className) as $methodName) {
65 if (substr($methodName, -7, 7) === 'Command') {
66 $this->availableCommands[] = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', $className, substr($methodName, 0, -7));
67 }
68 }
69 }
70 }
71 return $this->availableCommands;
72 }
73
74 /**
75 * Returns a Command that matches the given identifier.
76 * If no Command could be found a CommandNotFoundException is thrown
77 * If more than one Command matches an AmbiguousCommandIdentifierException is thrown that contains the matched Commands
78 *
79 * @param string $commandIdentifier command identifier in the format foo:bar:baz
80 * @return \TYPO3\CMS\Extbase\Mvc\Cli\Command
81 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException if no matching command is available
82 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException if more than one Command matches the identifier (the exception contains the matched commands)
83 * @api
84 */
85 public function getCommandByIdentifier($commandIdentifier) {
86 $commandIdentifier = strtolower(trim($commandIdentifier));
87 if ($commandIdentifier === 'help') {
88 $commandIdentifier = 'extbase:help:help';
89 }
90 $matchedCommands = array();
91 $availableCommands = $this->getAvailableCommands();
92 foreach ($availableCommands as $command) {
93 if ($this->commandMatchesIdentifier($command, $commandIdentifier)) {
94 $matchedCommands[] = $command;
95 }
96 }
97 if (count($matchedCommands) === 0) {
98 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('No command could be found that matches the command identifier "' . $commandIdentifier . '".', 1310556663);
99 }
100 if (count($matchedCommands) > 1) {
101 throw new \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException('More than one command matches the command identifier "' . $commandIdentifier . '"', 1310557169, NULL, $matchedCommands);
102 }
103 return current($matchedCommands);
104 }
105
106 /**
107 * Returns the shortest, non-ambiguous command identifier for the given command
108 *
109 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command The command
110 * @return string The shortest possible command identifier
111 * @api
112 */
113 public function getShortestIdentifierForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command) {
114 if ($command->getCommandIdentifier() === 'extbase:help:help') {
115 return 'help';
116 }
117 $shortCommandIdentifiers = $this->getShortCommandIdentifiers();
118 if (!isset($shortCommandIdentifiers[$command->getCommandIdentifier()])) {
119 $command->getCommandIdentifier();
120 }
121 return $shortCommandIdentifiers[$command->getCommandIdentifier()];
122 }
123
124 /**
125 * Returns an array that contains all available command identifiers and their shortest non-ambiguous alias
126 *
127 * @return array in the format array('full.command:identifier1' => 'alias1', 'full.command:identifier2' => 'alias2')
128 */
129 protected function getShortCommandIdentifiers() {
130 if ($this->shortCommandIdentifiers === NULL) {
131 $commandsByCommandName = array();
132 foreach ($this->getAvailableCommands() as $availableCommand) {
133 list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
134 if (!isset($commandsByCommandName[$commandName])) {
135 $commandsByCommandName[$commandName] = array();
136 }
137 if (!isset($commandsByCommandName[$commandName][$controllerName])) {
138 $commandsByCommandName[$commandName][$controllerName] = array();
139 }
140 $commandsByCommandName[$commandName][$controllerName][] = $extensionKey;
141 }
142 foreach ($this->getAvailableCommands() as $availableCommand) {
143 list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
144 if (count($commandsByCommandName[$commandName][$controllerName]) > 1) {
145 $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s:%s', $extensionKey, $controllerName, $commandName);
146 } else {
147 $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s', $controllerName, $commandName);
148 }
149 }
150 }
151 return $this->shortCommandIdentifiers;
152 }
153
154 /**
155 * Returns TRUE if the specified command identifier matches the identifier of the specified command.
156 * This is the case, if the identifiers are the same or if at least the last two command parts match (case sensitive).
157 *
158 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command
159 * @param string $commandIdentifier command identifier in the format foo:bar:baz (all lower case)
160 * @return boolean TRUE if the specified command identifier matches this commands identifier
161 */
162 protected function commandMatchesIdentifier(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command, $commandIdentifier) {
163 $commandIdentifierParts = explode(':', $command->getCommandIdentifier());
164 $searchedCommandIdentifierParts = explode(':', $commandIdentifier);
165 $extensionKey = array_shift($commandIdentifierParts);
166 if (count($searchedCommandIdentifierParts) === 3) {
167 $searchedExtensionKey = array_shift($searchedCommandIdentifierParts);
168 if ($searchedExtensionKey !== $extensionKey) {
169 return FALSE;
170 }
171 }
172 if (count($searchedCommandIdentifierParts) !== 2) {
173 return FALSE;
174 }
175 return $searchedCommandIdentifierParts === $commandIdentifierParts;
176 }
177 }
178
179 ?>