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