‪TYPO3CMS  9.5
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 
18 
25 {
29  protected ‪$objectManager;
30 
34  protected ‪$controllerClassName;
35 
39  protected ‪$controllerCommandName;
40 
44  protected ‪$commandIdentifier;
45 
51  protected ‪$extensionName;
52 
56  protected ‪$reflectionService;
57 
61  protected ‪$classSchema;
62 
67 
71  public function ‪injectObjectManager(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface ‪$objectManager)
72  {
73  $this->objectManager = ‪$objectManager;
74  }
75 
79  public function ‪injectReflectionService(\‪TYPO3\CMS\‪Extbase\Reflection\ReflectionService ‪$reflectionService)
80  {
81  $this->reflectionService = ‪$reflectionService;
82  }
83 
92  {
93  $this->controllerClassName = ‪$controllerClassName;
94  $this->controllerCommandName = ‪$controllerCommandName;
95  $this->controllerCommandMethod = $this->controllerCommandName . 'Command';
96  $classNameParts = explode('\\', ‪$controllerClassName);
97  if (isset($classNameParts[0]) && $classNameParts[0] === 'TYPO3' && isset($classNameParts[1]) && $classNameParts[1] === 'CMS') {
98  $classNameParts[0] .= '\\' . $classNameParts[1];
99  unset($classNameParts[1]);
100  $classNameParts = array_values($classNameParts);
101  }
102  $numberOfClassNameParts = count($classNameParts);
103  if ($numberOfClassNameParts < 3) {
104  throw new \InvalidArgumentException(
105  'Controller class names must at least consist of three parts: vendor, extension name and path.',
106  1438782187
107  );
108  }
109  if (strpos($classNameParts[$numberOfClassNameParts - 1], 'CommandController') === false) {
110  throw new \InvalidArgumentException(
111  'Invalid controller class name "' . ‪$controllerClassName . '". Class name must end with "CommandController".',
112  1305100019
113  );
114  }
115 
116  $this->extensionName = $classNameParts[1];
117  $extensionKey = \TYPO3\CMS\Core\Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($this->extensionName);
118  $this->commandIdentifier = strtolower($extensionKey . ':' . substr($classNameParts[$numberOfClassNameParts - 1], 0, -17) . ':' . ‪$controllerCommandName);
119  }
120 
121  public function ‪initializeObject()
122  {
123  $this->classSchema = $this->reflectionService->getClassSchema($this->controllerClassName);
124  }
125 
129  public function ‪getControllerClassName()
130  {
132  }
133 
137  public function ‪getControllerCommandName()
138  {
140  }
141 
147  public function ‪getCommandIdentifier()
148  {
150  }
151 
157  public function ‪getExtensionName()
158  {
160  }
161 
167  public function ‪getShortDescription()
168  {
169  $lines = explode(LF, $this->classSchema->getMethod($this->controllerCommandMethod)['description']);
170  return !empty($lines) ? trim($lines[0]) : '<no description available>';
171  }
172 
180  public function ‪getDescription()
181  {
182  $lines = explode(LF, $this->classSchema->getMethod($this->controllerCommandMethod)['description']);
183  array_shift($lines);
184  $descriptionLines = [];
185  foreach ($lines as $line) {
186  $trimmedLine = trim($line);
187  if ($descriptionLines !== [] || $trimmedLine !== '') {
188  $descriptionLines[] = $trimmedLine;
189  }
190  }
191  return implode(LF, $descriptionLines);
192  }
193 
199  public function ‪hasArguments()
200  {
201  return !empty($this->classSchema->getMethod($this->controllerCommandMethod)['params']);
202  }
203 
211  public function ‪getArgumentDefinitions()
212  {
213  if (!$this->‪hasArguments()) {
214  return [];
215  }
216  $commandArgumentDefinitions = [];
217  $commandParameters = $this->classSchema->getMethod($this->controllerCommandMethod)['params'];
218  $commandParameterTags = $this->classSchema->getMethod($this->controllerCommandMethod)['tags']['param'];
219  $i = 0;
220  foreach ($commandParameters as $commandParameterName => $commandParameterDefinition) {
221  $explodedAnnotation = preg_split('/\s+/', $commandParameterTags[$i], 3);
222  $description = !empty($explodedAnnotation[2]) ? $explodedAnnotation[2] : '';
223  $required = $commandParameterDefinition['optional'] !== true;
224  $commandArgumentDefinitions[] = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\Cli\CommandArgumentDefinition::class, $commandParameterName, $required, $description);
225  $i++;
226  }
227  return $commandArgumentDefinitions;
228  }
229 
237  public function ‪isInternal()
238  {
239  return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['internal']);
240  }
241 
247  public function ‪isCliOnly()
248  {
249  return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['cli']);
250  }
251 
261  public function ‪isFlushingCaches()
262  {
263  trigger_error(
264  'Method isFlushingCaches() will be removed in TYPO3 v10.0. Do not call from other extension.',
265  E_USER_DEPRECATED
266  );
267  return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['flushesCaches']);
268  }
269 
276  public function ‪getRelatedCommandIdentifiers()
277  {
278  if (!isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['see'])) {
279  return [];
280  }
281  $relatedCommandIdentifiers = [];
282  foreach ($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['see'] as $tagValue) {
283  if (preg_match('/^[\\w\\d\\.]+:[\\w\\d]+:[\\w\\d]+$/', $tagValue) === 1) {
284  $relatedCommandIdentifiers[] = $tagValue;
285  }
286  }
287  return $relatedCommandIdentifiers;
288  }
289 }
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\isInternal
‪bool isInternal()
Definition: Command.php:229
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\isCliOnly
‪bool isCliOnly()
Definition: Command.php:239
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$controllerCommandMethod
‪string $controllerCommandMethod
Definition: Command.php:58
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\initializeObject
‪initializeObject()
Definition: Command.php:113
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\hasArguments
‪bool hasArguments()
Definition: Command.php:191
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getDescription
‪string getDescription()
Definition: Command.php:172
‪TYPO3
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getCommandIdentifier
‪string getCommandIdentifier()
Definition: Command.php:139
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getExtensionName
‪string getExtensionName()
Definition: Command.php:149
‪TYPO3\CMS\Extbase\Mvc\Cli
Definition: Command.php:2
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$reflectionService
‪TYPO3 CMS Extbase Reflection ReflectionService $reflectionService
Definition: Command.php:50
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\__construct
‪__construct($controllerClassName, $controllerCommandName)
Definition: Command.php:83
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getRelatedCommandIdentifiers
‪array getRelatedCommandIdentifiers()
Definition: Command.php:268
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$classSchema
‪ClassSchema $classSchema
Definition: Command.php:54
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\injectObjectManager
‪injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
Definition: Command.php:63
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$commandIdentifier
‪string $commandIdentifier
Definition: Command.php:40
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getControllerCommandName
‪string getControllerCommandName()
Definition: Command.php:129
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\isFlushingCaches
‪bool isFlushingCaches()
Definition: Command.php:253
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$controllerCommandName
‪string $controllerCommandName
Definition: Command.php:36
‪TYPO3\CMS\Extbase\Mvc\Cli\Command
Definition: Command.php:25
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getControllerClassName
‪string getControllerClassName()
Definition: Command.php:121
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getShortDescription
‪string getShortDescription()
Definition: Command.php:159
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$controllerClassName
‪string $controllerClassName
Definition: Command.php:32
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\injectReflectionService
‪injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
Definition: Command.php:71
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$extensionName
‪string $extensionName
Definition: Command.php:46
‪TYPO3\CMS\Extbase\Reflection\ClassSchema
Definition: ClassSchema.php:41
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Command.php:28
‪TYPO3\CMS\Extbase\Mvc\Cli\Command\getArgumentDefinitions
‪array<\TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition > getArgumentDefinitions()
Definition: Command.php:203