[TASK] Add proper comments to PHP code
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Command / HelpCommandController.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Command;
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
17 /**
18 * A Command Controller which provides help for available commands
19 *
20 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
21 */
22 class HelpCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
26 */
27 protected $commandManager;
28
29 /**
30 * @var array
31 */
32 protected $commandsByExtensionsAndControllers = [];
33
34 /**
35 * @param \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager
36 */
37 public function injectCommandManager(\TYPO3\CMS\Extbase\Mvc\Cli\CommandManager $commandManager)
38 {
39 $this->commandManager = $commandManager;
40 }
41
42 /**
43 * Displays a short, general help message
44 *
45 * This only outputs the Extbase version number, context and some hint about how to
46 * get more help about commands.
47 *
48 * @return void
49 * @internal
50 */
51 public function helpStubCommand()
52 {
53 $this->outputLine('Extbase %s', [TYPO3_version]);
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
60 /**
61 * Display help for a command
62 *
63 * The help command displays help for a given command:
64 * ./cli_dispatch.phpsh extbase help <command identifier>
65 *
66 * @param string $commandIdentifier Identifier of a command for more details
67 * @cli
68 */
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
84 /**
85 * Builds an index of all commands that are available
86 *
87 * @return void
88 */
89 protected function displayHelpIndex()
90 {
91 $this->buildCommandsIndex();
92 $this->outputLine('Extbase %s', [TYPO3_version]);
93 $this->outputLine('usage: ' . $this->request->getCallingScript() . ' <command identifier>');
94 $this->outputLine();
95 $this->outputLine('The following commands are currently available:');
96 foreach ($this->commandsByExtensionsAndControllers as $extensionKey => $commandControllers) {
97 $this->outputLine('');
98 $this->outputLine('EXTENSION "%s":', [strtoupper($extensionKey)]);
99 $this->outputLine(str_repeat('-', $this->output->getMaximumLineLength()));
100 foreach ($commandControllers as $commands) {
101 foreach ($commands as $command) {
102 $description = wordwrap($command->getShortDescription(), $this->output->getMaximumLineLength() - 43, PHP_EOL . str_repeat(' ', 43), true);
103 $shortCommandIdentifier = $this->commandManager->getShortestIdentifierForCommand($command);
104 $this->outputLine('%-2s%-40s %s', [' ', $shortCommandIdentifier, $description]);
105 }
106 $this->outputLine();
107 }
108 }
109 $this->outputLine('See \'' . $this->request->getCallingScript() . ' help <command identifier>\' for more information about a specific command.');
110 $this->outputLine();
111 }
112
113 /**
114 * Render help text for a single command
115 *
116 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command
117 * @return void
118 */
119 protected function displayHelpForCommand(\TYPO3\CMS\Extbase\Mvc\Cli\Command $command)
120 {
121 $this->outputLine();
122 $this->outputLine($command->getShortDescription());
123 $this->outputLine();
124 $this->outputLine('COMMAND:');
125 $this->outputLine('%-2s%s', [' ', $command->getCommandIdentifier()]);
126 $commandArgumentDefinitions = $command->getArgumentDefinitions();
127 $usage = '';
128 $hasOptions = false;
129 foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
130 if (!$commandArgumentDefinition->isRequired()) {
131 $hasOptions = true;
132 } else {
133 $usage .= sprintf(' <%s>', strtolower(preg_replace('/([A-Z])/', ' $1', $commandArgumentDefinition->getName())));
134 }
135 }
136 $usage = $this->request->getCallingScript() . ' ' . $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage;
137 $this->outputLine();
138 $this->outputLine('USAGE:');
139 $this->outputLine(' ' . $usage);
140 $argumentDescriptions = [];
141 $optionDescriptions = [];
142 if ($command->hasArguments()) {
143 foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
144 $argumentDescription = $commandArgumentDefinition->getDescription();
145 $argumentDescription = wordwrap($argumentDescription, $this->output->getMaximumLineLength() - 23, PHP_EOL . str_repeat(' ', 23), true);
146 if ($commandArgumentDefinition->isRequired()) {
147 $argumentDescriptions[] = vsprintf(' %-20s %s', [$commandArgumentDefinition->getDashedName(), $argumentDescription]);
148 } else {
149 $optionDescriptions[] = vsprintf(' %-20s %s', [$commandArgumentDefinition->getDashedName(), $argumentDescription]);
150 }
151 }
152 }
153 if (!empty($argumentDescriptions)) {
154 $this->outputLine();
155 $this->outputLine('ARGUMENTS:');
156 foreach ($argumentDescriptions as $argumentDescription) {
157 $this->outputLine($argumentDescription);
158 }
159 }
160 if (!empty($optionDescriptions)) {
161 $this->outputLine();
162 $this->outputLine('OPTIONS:');
163 foreach ($optionDescriptions as $optionDescription) {
164 $this->outputLine($optionDescription);
165 }
166 }
167 if ($command->getDescription() !== '') {
168 $this->outputLine();
169 $this->outputLine('DESCRIPTION:');
170 $descriptionLines = explode(LF, $command->getDescription());
171 foreach ($descriptionLines as $descriptionLine) {
172 $this->outputLine('%-2s%s', [' ', $descriptionLine]);
173 }
174 }
175 $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
176 if ($relatedCommandIdentifiers !== []) {
177 $this->outputLine();
178 $this->outputLine('SEE ALSO:');
179 foreach ($relatedCommandIdentifiers as $commandIdentifier) {
180 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
181 $this->outputLine('%-2s%s (%s)', [' ', $commandIdentifier, $command->getShortDescription()]);
182 }
183 }
184 $this->outputLine();
185 }
186
187 /**
188 * Displays an error message
189 *
190 * @internal
191 * @param \TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception
192 * @return void
193 */
194 public function errorCommand(\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception)
195 {
196 $this->outputLine($exception->getMessage());
197 if ($exception instanceof \TYPO3\CMS\Extbase\Mvc\Exception\AmbiguousCommandIdentifierException) {
198 $this->outputLine('Please specify the complete command identifier. Matched commands:');
199 foreach ($exception->getMatchingCommands() as $matchingCommand) {
200 $this->outputLine(' %s', [$matchingCommand->getCommandIdentifier()]);
201 }
202 }
203 $this->outputLine('');
204 $this->outputLine('Enter "' . $this->request->getCallingScript() . ' help" for an overview of all available commands');
205 $this->outputLine('or "' . $this->request->getCallingScript() . ' help <command identifier>" for a detailed description of the corresponding command.');
206 }
207
208 /**
209 * Builds an index of available commands. For each of them a Command object is
210 * added to the commands array of this class.
211 *
212 * @return void
213 */
214 protected function buildCommandsIndex()
215 {
216 $availableCommands = $this->commandManager->getAvailableCommands();
217 foreach ($availableCommands as $command) {
218 if ($command->isInternal()) {
219 continue;
220 }
221 $commandIdentifier = $command->getCommandIdentifier();
222 $extensionKey = strstr($commandIdentifier, ':', true);
223 $commandControllerClassName = $command->getControllerClassName();
224 $commandName = $command->getControllerCommandName();
225 $this->commandsByExtensionsAndControllers[$extensionKey][$commandControllerClassName][$commandName] = $command;
226 }
227 }
228 }