[TASK] Make TYPO3 Core PSR-2 standard compliant
[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 = array();
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', array(\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
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 * @return void
86 */
87 protected function displayHelpIndex()
88 {
89 $this->buildCommandsIndex();
90 $this->outputLine('Extbase %s', array(\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":', array(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', array(' ', $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
111 /**
112 * Render help text for a single command
113 *
114 * @param \TYPO3\CMS\Extbase\Mvc\Cli\Command $command
115 * @return void
116 */
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', array(' ', $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 = array();
139 $optionDescriptions = array();
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', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
146 } else {
147 $optionDescriptions[] = vsprintf(' %-20s %s', array($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', array(' ', $descriptionLine));
171 }
172 }
173 $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
174 if ($relatedCommandIdentifiers !== array()) {
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)', array(' ', $commandIdentifier, $command->getShortDescription()));
180 }
181 }
182 $this->outputLine();
183 }
184
185 /**
186 * Displays an error message
187 *
188 * @internal
189 * @param \TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception
190 * @return void
191 */
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', array($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
206 /**
207 * Builds an index of available commands. For each of them a Command object is
208 * added to the commands array of this class.
209 *
210 * @return void
211 */
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 }