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