TYPO3 CMS  TYPO3_7-6
HelpCommandController.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 
23 {
27  protected $commandManager;
28 
33 
37  public function injectCommandManager(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager)
38  {
39  $this->commandManager = $commandManager;
40  }
41 
51  public function helpStubCommand()
52  {
53  $this->outputLine('Extbase %s', [\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion('extbase')]);
54  $this->outputLine('usage: ' . $this->request->getCallingScript() . ' <command identifier>');
55  $this->outputLine();
56  $this->outputLine('See \'' . $this->request->getCallingScript() . ' help\' for a list of all available commands.');
57  $this->outputLine();
58  }
59 
69  public function helpCommand($commandIdentifier = null)
70  {
71  if ($commandIdentifier === null) {
72  $this->displayHelpIndex();
73  } else {
74  try {
75  $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
76  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception) {
77  $this->outputLine($exception->getMessage());
78  return;
79  }
80  $this->displayHelpForCommand($command);
81  }
82  }
83 
87  protected function displayHelpIndex()
88  {
89  $this->buildCommandsIndex();
90  $this->outputLine('Extbase %s', [\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getExtensionVersion('extbase')]);
91  $this->outputLine('usage: ' . $this->request->getCallingScript() . ' <command identifier>');
92  $this->outputLine();
93  $this->outputLine('The following commands are currently available:');
94  foreach ($this->commandsByExtensionsAndControllers as $extensionKey => $commandControllers) {
95  $this->outputLine('');
96  $this->outputLine('EXTENSION "%s":', [strtoupper($extensionKey)]);
97  $this->outputLine(str_repeat('-', $this->output->getMaximumLineLength()));
98  foreach ($commandControllers as $commands) {
99  foreach ($commands as $command) {
100  $description = wordwrap($command->getShortDescription(), $this->output->getMaximumLineLength() - 43, PHP_EOL . str_repeat(' ', 43), true);
101  $shortCommandIdentifier = $this->commandManager->getShortestIdentifierForCommand($command);
102  $this->outputLine('%-2s%-40s %s', [' ', $shortCommandIdentifier, $description]);
103  }
104  $this->outputLine();
105  }
106  }
107  $this->outputLine('See \'' . $this->request->getCallingScript() . ' help <command identifier>\' for more information about a specific command.');
108  $this->outputLine();
109  }
110 
117  protected function displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command)
118  {
119  $this->outputLine();
120  $this->outputLine($command->getShortDescription());
121  $this->outputLine();
122  $this->outputLine('COMMAND:');
123  $this->outputLine('%-2s%s', [' ', $command->getCommandIdentifier()]);
124  $commandArgumentDefinitions = $command->getArgumentDefinitions();
125  $usage = '';
126  $hasOptions = false;
127  foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
128  if (!$commandArgumentDefinition->isRequired()) {
129  $hasOptions = true;
130  } else {
131  $usage .= sprintf(' <%s>', strtolower(preg_replace('/([A-Z])/', ' $1', $commandArgumentDefinition->getName())));
132  }
133  }
134  $usage = $this->request->getCallingScript() . ' ' . $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage;
135  $this->outputLine();
136  $this->outputLine('USAGE:');
137  $this->outputLine(' ' . $usage);
138  $argumentDescriptions = [];
139  $optionDescriptions = [];
140  if ($command->hasArguments()) {
141  foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
142  $argumentDescription = $commandArgumentDefinition->getDescription();
143  $argumentDescription = wordwrap($argumentDescription, $this->output->getMaximumLineLength() - 23, PHP_EOL . str_repeat(' ', 23), true);
144  if ($commandArgumentDefinition->isRequired()) {
145  $argumentDescriptions[] = vsprintf(' %-20s %s', [$commandArgumentDefinition->getDashedName(), $argumentDescription]);
146  } else {
147  $optionDescriptions[] = vsprintf(' %-20s %s', [$commandArgumentDefinition->getDashedName(), $argumentDescription]);
148  }
149  }
150  }
151  if (!empty($argumentDescriptions)) {
152  $this->outputLine();
153  $this->outputLine('ARGUMENTS:');
154  foreach ($argumentDescriptions as $argumentDescription) {
155  $this->outputLine($argumentDescription);
156  }
157  }
158  if (!empty($optionDescriptions)) {
159  $this->outputLine();
160  $this->outputLine('OPTIONS:');
161  foreach ($optionDescriptions as $optionDescription) {
162  $this->outputLine($optionDescription);
163  }
164  }
165  if ($command->getDescription() !== '') {
166  $this->outputLine();
167  $this->outputLine('DESCRIPTION:');
168  $descriptionLines = explode(LF, $command->getDescription());
169  foreach ($descriptionLines as $descriptionLine) {
170  $this->outputLine('%-2s%s', [' ', $descriptionLine]);
171  }
172  }
173  $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
174  if ($relatedCommandIdentifiers !== []) {
175  $this->outputLine();
176  $this->outputLine('SEE ALSO:');
177  foreach ($relatedCommandIdentifiers as $commandIdentifier) {
178  $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
179  $this->outputLine('%-2s%s (%s)', [' ', $commandIdentifier, $command->getShortDescription()]);
180  }
181  }
182  $this->outputLine();
183  }
184 
192  public function errorCommand(\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception)
193  {
194  $this->outputLine($exception->getMessage());
195  if ($exception instanceof \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException) {
196  $this->outputLine('Please specify the complete command identifier. Matched commands:');
197  foreach ($exception->getMatchingCommands() as $matchingCommand) {
198  $this->outputLine(' %s', [$matchingCommand->getCommandIdentifier()]);
199  }
200  }
201  $this->outputLine('');
202  $this->outputLine('Enter "' . $this->request->getCallingScript() . ' help" for an overview of all available commands');
203  $this->outputLine('or "' . $this->request->getCallingScript() . ' help <command identifier>" for a detailed description of the corresponding command.');
204  }
205 
212  protected function buildCommandsIndex()
213  {
214  $availableCommands = $this->commandManager->getAvailableCommands();
215  foreach ($availableCommands as $command) {
216  if ($command->isInternal()) {
217  continue;
218  }
219  $commandIdentifier = $command->getCommandIdentifier();
220  $extensionKey = strstr($commandIdentifier, ':', true);
221  $commandControllerClassName = $command->getControllerClassName();
222  $commandName = $command->getControllerCommandName();
223  $this->commandsByExtensionsAndControllers[$extensionKey][$commandControllerClassName][$commandName] = $command;
224  }
225  }
226 }
errorCommand(\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception)
injectCommandManager(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager)
displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command)