[CLEANUP] Adjust code to coding guidelines
[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 */
37 protected $objectManager;
38
39 /**
40 * @var array<\TYPO3\CMS\Extbase\Mvc\Cli\Command>
41 */
42 protected $availableCommands = NULL;
43
44 /**
45 * @var array
46 */
47 protected $shortCommandIdentifiers = NULL;
48
49 /**
50 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
51 * @return void
52 */
53 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
54 $this->objectManager = $objectManager;
55 }
56
57 /**
58 * Returns an array of all commands
59 *
60 * @return array<\TYPO3\CMS\Extbase\Mvc\Cli\Command>
61 * @author Robert Lemke <robert@typo3.org>
62 * @author Bastian Waidelich <bastian@typo3.org>
63 * @api
64 */
65 public function getAvailableCommands() {
66 if ($this->availableCommands === NULL) {
67 $this->availableCommands = array();
68 $commandControllerClassNames = is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers']) ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] : array();
69 foreach ($commandControllerClassNames as $className) {
70 if (!class_exists($className)) {
71 continue;
72 }
73 foreach (get_class_methods($className) as $methodName) {
74 if (substr($methodName, -7, 7) === 'Command') {
75 $this->availableCommands[] = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Command', $className, substr($methodName, 0, -7));
76 }
77 }
78 }
79 }
80 return $this->availableCommands;
81 }
82
83 /**
84 * Returns a Command that matches the given identifier.
85 * If no Command could be found a CommandNotFoundException is thrown
86 * If more than one Command matches an AmbiguousCommandIdentifierException is thrown that contains the matched Commands
87 *
88 * @param string $commandIdentifier command identifier in the format foo:bar:baz
89 * @return \TYPO3\CMS\Extbase\Mvc\Cli\Command
90 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException if no matching command is available
91 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException if more than one Command matches the identifier (the exception contains the matched commands)
92 * @api
93 */
94 public function getCommandByIdentifier($commandIdentifier) {
95 $commandIdentifier = strtolower(trim($commandIdentifier));
96 if ($commandIdentifier === 'help') {
97 $commandIdentifier = 'extbase:help:help';
98 }
99 $matchedCommands = array();
100 $availableCommands = $this->getAvailableCommands();
101 foreach ($availableCommands as $command) {
102 if ($this->commandMatchesIdentifier($command, $commandIdentifier)) {
103 $matchedCommands[] = $command;
104 }
105 }
106 if (count($matchedCommands) === 0) {
107 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('No command could be found that matches the command identifier "' . $commandIdentifier . '".', 1310556663);
108 }
109 if (count($matchedCommands) > 1) {
110 throw new \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException('More than one command matches the command identifier "' . $commandIdentifier . '"', 1310557169, NULL, $matchedCommands);
111 }
112 return current($matchedCommands);
113 }
114
115 /**
116 * Returns the shortest, non-ambiguous command identifier for the given command
117 *
118 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command The command
119 * @return string The shortest possible command identifier
120 * @author Robert Lemke <robert@typo3.org>
121 * @author Bastian Waidelich <bastian@typo3.org>
122 * @api
123 */
124 public function getShortestIdentifierForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command) {
125 if ($command->getCommandIdentifier() === 'extbase:help:help') {
126 return 'help';
127 }
128 $shortCommandIdentifiers = $this->getShortCommandIdentifiers();
129 if (!isset($shortCommandIdentifiers[$command->getCommandIdentifier()])) {
130 $command->getCommandIdentifier();
131 }
132 return $shortCommandIdentifiers[$command->getCommandIdentifier()];
133 }
134
135 /**
136 * Returns an array that contains all available command identifiers and their shortest non-ambiguous alias
137 *
138 * @return array in the format array('full.command:identifier1' => 'alias1', 'full.command:identifier2' => 'alias2')
139 * @author Bastian Waidelich <bastian@typo3.org>
140 */
141 protected function getShortCommandIdentifiers() {
142 if ($this->shortCommandIdentifiers === NULL) {
143 $commandsByCommandName = array();
144 foreach ($this->getAvailableCommands() as $availableCommand) {
145 list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
146 if (!isset($commandsByCommandName[$commandName])) {
147 $commandsByCommandName[$commandName] = array();
148 }
149 if (!isset($commandsByCommandName[$commandName][$controllerName])) {
150 $commandsByCommandName[$commandName][$controllerName] = array();
151 }
152 $commandsByCommandName[$commandName][$controllerName][] = $extensionKey;
153 }
154 foreach ($this->getAvailableCommands() as $availableCommand) {
155 list($extensionKey, $controllerName, $commandName) = explode(':', $availableCommand->getCommandIdentifier());
156 if (count($commandsByCommandName[$commandName][$controllerName]) > 1) {
157 $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s:%s', $extensionKey, $controllerName, $commandName);
158 } else {
159 $this->shortCommandIdentifiers[$availableCommand->getCommandIdentifier()] = sprintf('%s:%s', $controllerName, $commandName);
160 }
161 }
162 }
163 return $this->shortCommandIdentifiers;
164 }
165
166 /**
167 * Returns TRUE if the specified command identifier matches the identifier of the specified command.
168 * This is the case, if the identifiers are the same or if at least the last two command parts match (case sensitive).
169 *
170 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command
171 * @param string $commandIdentifier command identifier in the format foo:bar:baz (all lower case)
172 * @return boolean TRUE if the specified command identifier matches this commands identifier
173 * @author Bastian Waidelich <bastian@typo3.org>
174 */
175 protected function commandMatchesIdentifier(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command, $commandIdentifier) {
176 $commandIdentifierParts = explode(':', $command->getCommandIdentifier());
177 $searchedCommandIdentifierParts = explode(':', $commandIdentifier);
178 $extensionKey = array_shift($commandIdentifierParts);
179 if (count($searchedCommandIdentifierParts) === 3) {
180 $searchedExtensionKey = array_shift($searchedCommandIdentifierParts);
181 if ($searchedExtensionKey !== $extensionKey) {
182 return FALSE;
183 }
184 }
185 if (count($searchedCommandIdentifierParts) !== 2) {
186 return FALSE;
187 }
188 return $searchedCommandIdentifierParts === $commandIdentifierParts;
189 }
190 }
191
192 ?>