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