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