[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / Command.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 * Represents a Command
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class Command {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 */
37 protected $objectManager;
38
39 /**
40 * @var string
41 */
42 protected $controllerClassName;
43
44 /**
45 * @var string
46 */
47 protected $controllerCommandName;
48
49 /**
50 * @var string
51 */
52 protected $commandIdentifier;
53
54 /**
55 * @var \TYPO3\CMS\Extbase\Reflection\MethodReflection
56 */
57 protected $commandMethodReflection;
58
59 /**
60 * Reflection service
61 *
62 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
63 */
64 private $reflectionService;
65
66 /**
67 * Constructor
68 *
69 * @param string $controllerClassName Class name of the controller providing the command
70 * @param string $controllerCommandName Command name, i.e. the method name of the command, without the "Command" suffix
71 * @throws \InvalidArgumentException
72 * @author Robert Lemke <robert@typo3.org>
73 */
74 public function __construct($controllerClassName, $controllerCommandName) {
75 $this->controllerClassName = $controllerClassName;
76 $this->controllerCommandName = $controllerCommandName;
77 $delimiter = strpos($controllerClassName, '\\') !== FALSE ? '\\' : '_';
78 $classNameParts = explode($delimiter, $controllerClassName);
79 if (isset($classNameParts[0]) && $classNameParts[0] === 'TYPO3' && isset($classNameParts[1]) && $classNameParts[1] === 'CMS') {
80 $classNameParts[0] .= '\\' . $classNameParts[1];
81 unset($classNameParts[1]);
82 $classNameParts = array_values($classNameParts);
83 }
84 if (count($classNameParts) !== 4 || strpos($classNameParts[3], 'CommandController') === FALSE) {
85 throw new \InvalidArgumentException('Invalid controller class name "' . $controllerClassName . '"', 1305100019);
86 }
87 $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($classNameParts[1]);
88 $this->commandIdentifier = strtolower($extensionKey . ':' . substr($classNameParts[3], 0, -17) . ':' . $controllerCommandName);
89 }
90
91 /**
92 * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService Reflection service
93 */
94 public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService) {
95 $this->reflectionService = $reflectionService;
96 }
97
98 /**
99 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager A reference to the object manager
100 * @return void
101 */
102 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
103 $this->objectManager = $objectManager;
104 }
105
106 /**
107 * @return string
108 */
109 public function getControllerClassName() {
110 return $this->controllerClassName;
111 }
112
113 /**
114 * @return string
115 */
116 public function getControllerCommandName() {
117 return $this->controllerCommandName;
118 }
119
120 /**
121 * Returns the command identifier for this command
122 *
123 * @return string The command identifier for this command, following the pattern extensionname:controllername:commandname
124 * @author Robert Lemke <robert@typo3.org>
125 */
126 public function getCommandIdentifier() {
127 return $this->commandIdentifier;
128 }
129
130 /**
131 * Returns a short description of this command
132 *
133 * @return string A short description
134 * @author Robert Lemke <robert@typo3.org>
135 */
136 public function getShortDescription() {
137 $lines = explode(chr(10), $this->getCommandMethodReflection()->getDescription());
138 return count($lines) > 0 ? trim($lines[0]) : '<no description available>';
139 }
140
141 /**
142 * Returns a longer description of this command
143 * This is the complete method description except for the first line which can be retrieved via getShortDescription()
144 * If The command description only consists of one line, an empty string is returned
145 *
146 * @return string A longer description of this command
147 * @author Bastian Waidelich <bastian@typo3.org>
148 */
149 public function getDescription() {
150 $lines = explode(chr(10), $this->getCommandMethodReflection()->getDescription());
151 array_shift($lines);
152 $descriptionLines = array();
153 foreach ($lines as $line) {
154 $trimmedLine = trim($line);
155 if ($descriptionLines !== array() || $trimmedLine !== '') {
156 $descriptionLines[] = $trimmedLine;
157 }
158 }
159 return implode(chr(10), $descriptionLines);
160 }
161
162 /**
163 * Returns TRUE if this command expects required and/or optional arguments, otherwise FALSE
164 *
165 * @return boolean
166 */
167 public function hasArguments() {
168 return count($this->getCommandMethodReflection()->getParameters()) > 0;
169 }
170
171 /**
172 * Returns an array of \TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition that contains
173 * information about required/optional arguments of this command.
174 * If the command does not expect any arguments, an empty array is returned
175 *
176 * @return array<\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition>
177 * @author Bastian Waidelich <bastian@typo3.org>
178 */
179 public function getArgumentDefinitions() {
180 if (!$this->hasArguments()) {
181 return array();
182 }
183 $commandArgumentDefinitions = array();
184 $commandMethodReflection = $this->getCommandMethodReflection();
185 $annotations = $commandMethodReflection->getTagsValues();
186 $commandParameters = $this->reflectionService->getMethodParameters($this->controllerClassName, $this->controllerCommandName . 'Command');
187 $i = 0;
188 foreach ($commandParameters as $commandParameterName => $commandParameterDefinition) {
189 $explodedAnnotation = explode(' ', $annotations['param'][$i]);
190 array_shift($explodedAnnotation);
191 array_shift($explodedAnnotation);
192 $description = implode(' ', $explodedAnnotation);
193 $required = $commandParameterDefinition['optional'] !== TRUE;
194 $commandArgumentDefinitions[] = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\CommandArgumentDefinition', $commandParameterName, $required, $description);
195 $i++;
196 }
197 return $commandArgumentDefinitions;
198 }
199
200 /**
201 * Tells if this command is internal and thus should not be exposed through help texts, user documentation etc.
202 * Internall commands are still accessible through the regular command line interface, but should not be used
203 * by users.
204 *
205 * @return boolean
206 * @author Robert Lemke <robert@typo3.org>
207 * @author Bastian Waidelich <bastian@typo3.org>
208 */
209 public function isInternal() {
210 return $this->getCommandMethodReflection()->isTaggedWith('internal');
211 }
212
213 /**
214 * Tells if this command flushes all caches and thus needs special attention in the interactive shell.
215 *
216 * Note that neither this method nor the @flushesCaches annotation is currently part of the official API.
217 *
218 * @return boolean
219 * @author Robert Lemke <robert@typo3.org>
220 */
221 public function isFlushingCaches() {
222 return $this->getCommandMethodReflection()->isTaggedWith('flushesCaches');
223 }
224
225 /**
226 * Returns an array of command identifiers which were specified in the "@see"
227 * annotation of a command method.
228 *
229 * @return array
230 * @author Robert Lemke <robert@typo3.org>
231 */
232 public function getRelatedCommandIdentifiers() {
233 $commandMethodReflection = $this->getCommandMethodReflection();
234 if (!$commandMethodReflection->isTaggedWith('see')) {
235 return array();
236 }
237 $relatedCommandIdentifiers = array();
238 foreach ($commandMethodReflection->getTagValues('see') as $tagValue) {
239 if (preg_match('/^[\\w\\d\\.]+:[\\w\\d]+:[\\w\\d]+$/', $tagValue) === 1) {
240 $relatedCommandIdentifiers[] = $tagValue;
241 }
242 }
243 return $relatedCommandIdentifiers;
244 }
245
246 /**
247 * @return \TYPO3\CMS\Extbase\Reflection\MethodReflection
248 */
249 protected function getCommandMethodReflection() {
250 if ($this->commandMethodReflection === NULL) {
251 $this->commandMethodReflection = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Reflection\\MethodReflection', $this->controllerClassName, $this->controllerCommandName . 'Command');
252 }
253 return $this->commandMethodReflection;
254 }
255 }
256
257 ?>