TYPO3 CMS  TYPO3_7-6
Command.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 class Command
23 {
27  protected $objectManager;
28 
33 
38 
42  protected $commandIdentifier;
43 
48 
54  protected $extensionName;
55 
59  protected $reflectionService;
60 
64  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
65  {
66  $this->objectManager = $objectManager;
67  }
68 
72  public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
73  {
74  $this->reflectionService = $reflectionService;
75  }
76 
85  {
86  $this->controllerClassName = $controllerClassName;
87  $this->controllerCommandName = $controllerCommandName;
88  $delimiter = strpos($controllerClassName, '\\') !== false ? '\\' : '_';
89  $classNameParts = explode($delimiter, $controllerClassName);
90  if (isset($classNameParts[0]) && $classNameParts[0] === 'TYPO3' && isset($classNameParts[1]) && $classNameParts[1] === 'CMS') {
91  $classNameParts[0] .= '\\' . $classNameParts[1];
92  unset($classNameParts[1]);
93  $classNameParts = array_values($classNameParts);
94  }
95  $numberOfClassNameParts = count($classNameParts);
96  if ($numberOfClassNameParts < 3) {
97  throw new \InvalidArgumentException(
98  'Controller class names must at least consist of three parts: vendor, extension name and path.',
99  1438782187
100  );
101  }
102  if (strpos($classNameParts[$numberOfClassNameParts - 1], 'CommandController') === false) {
103  throw new \InvalidArgumentException(
104  'Invalid controller class name "' . $controllerClassName . '". Class name must end with "CommandController".',
105  1305100019
106  );
107  }
108 
109  $this->extensionName = $classNameParts[1];
111  $this->commandIdentifier = strtolower($extensionKey . ':' . substr($classNameParts[$numberOfClassNameParts - 1], 0, -17) . ':' . $controllerCommandName);
112  }
113 
117  public function getControllerClassName()
118  {
120  }
121 
125  public function getControllerCommandName()
126  {
128  }
129 
135  public function getCommandIdentifier()
136  {
138  }
139 
145  public function getExtensionName()
146  {
147  return $this->extensionName;
148  }
149 
155  public function getShortDescription()
156  {
157  $lines = explode(LF, $this->getCommandMethodReflection()->getDescription());
158  return !empty($lines) ? trim($lines[0]) : '<no description available>';
159  }
160 
168  public function getDescription()
169  {
170  $lines = explode(LF, $this->getCommandMethodReflection()->getDescription());
171  array_shift($lines);
172  $descriptionLines = [];
173  foreach ($lines as $line) {
174  $trimmedLine = trim($line);
175  if ($descriptionLines !== [] || $trimmedLine !== '') {
176  $descriptionLines[] = $trimmedLine;
177  }
178  }
179  return implode(LF, $descriptionLines);
180  }
181 
187  public function hasArguments()
188  {
189  return !empty($this->getCommandMethodReflection()->getParameters());
190  }
191 
199  public function getArgumentDefinitions()
200  {
201  if (!$this->hasArguments()) {
202  return [];
203  }
204  $commandArgumentDefinitions = [];
206  $annotations = $commandMethodReflection->getTagsValues();
207  $commandParameters = $this->reflectionService->getMethodParameters($this->controllerClassName, $this->controllerCommandName . 'Command');
208  $i = 0;
209  foreach ($commandParameters as $commandParameterName => $commandParameterDefinition) {
210  $explodedAnnotation = preg_split('/\s+/', $annotations['param'][$i], 3);
211  $description = !empty($explodedAnnotation[2]) ? $explodedAnnotation[2] : '';
212  $required = $commandParameterDefinition['optional'] !== true;
213  $commandArgumentDefinitions[] = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition::class, $commandParameterName, $required, $description);
214  $i++;
215  }
216  return $commandArgumentDefinitions;
217  }
218 
226  public function isInternal()
227  {
228  return $this->getCommandMethodReflection()->isTaggedWith('internal');
229  }
230 
236  public function isCliOnly()
237  {
238  return $this->getCommandMethodReflection()->isTaggedWith('cli');
239  }
240 
248  public function isFlushingCaches()
249  {
250  return $this->getCommandMethodReflection()->isTaggedWith('flushesCaches');
251  }
252 
260  {
262  if (!$commandMethodReflection->isTaggedWith('see')) {
263  return [];
264  }
265  $relatedCommandIdentifiers = [];
266  foreach ($commandMethodReflection->getTagValues('see') as $tagValue) {
267  if (preg_match('/^[\\w\\d\\.]+:[\\w\\d]+:[\\w\\d]+$/', $tagValue) === 1) {
268  $relatedCommandIdentifiers[] = $tagValue;
269  }
270  }
271  return $relatedCommandIdentifiers;
272  }
273 
277  protected function getCommandMethodReflection()
278  {
279  if ($this->commandMethodReflection === null) {
280  $this->commandMethodReflection = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\MethodReflection::class, $this->controllerClassName, $this->controllerCommandName . 'Command');
281  }
283  }
284 }
__construct($controllerClassName, $controllerCommandName)
Definition: Command.php:84
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Command.php:64
injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
Definition: Command.php:72