[+FEATURE] Backport CommandController Implementation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Command / HelpCommandController.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 * All rights reserved
5 *
6 * This script is part of the TYPO3 project. The TYPO3 project is
7 * free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * The GNU General Public License can be found at
13 * http://www.gnu.org/copyleft/gpl.html.
14 *
15 * This script is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * This copyright notice MUST APPEAR in all copies of the script!
21 ***************************************************************/
22
23 /**
24 * A Command Controller which provides help for available commands
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 */
28 class Tx_Extbase_Command_HelpCommandController extends Tx_Extbase_MVC_Controller_CommandController {
29
30 /**
31 * @var Tx_Extbase_MVC_CLI_CommandManager
32 */
33 protected $commandManager;
34
35 /**
36 * @var array
37 */
38 protected $commandsByExtensionsAndControllers = array();
39
40 /**
41 * @param Tx_Extbase_MVC_CLI_CommandManager $commandManager
42 * @return void
43 * @author Bastian Waidelich <bastian@typo3.org>
44 */
45 public function injectCommandManager(Tx_Extbase_MVC_CLI_CommandManager $commandManager) {
46 $this->commandManager = $commandManager;
47 }
48
49 /**
50 * Displays a short, general help message
51 *
52 * This only outputs the Extbase version number, context and some hint about how to
53 * get more help about commands.
54 *
55 * @return void
56 * @internal
57 */
58 public function helpStubCommand() {
59 $this->outputLine('Extbase %s', array(t3lib_extMgm::getExtensionVersion('extbase')));
60 $this->outputLine('usage: ./cli_dispatch.phpsh extbase <command identifier>');
61 $this->outputLine();
62 $this->outputLine('See \'./cli_dispatch.phpsh extbase help\' for a list of all available commands.');
63 $this->outputLine();
64 }
65
66 /**
67 * Display help for a command
68 *
69 * The help command displays help for a given command:
70 * ./cli_dispatch.phpsh extbase help <command identifier>
71 *
72 * @param string $commandIdentifier Identifier of a command for more details
73 * @return void
74 */
75 public function helpCommand($commandIdentifier = NULL) {
76 if ($commandIdentifier === NULL) {
77 $this->displayHelpIndex();
78 } else {
79 try {
80 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
81 } catch (Tx_Extbase_MVC_Exception_Command $exception) {
82 $this->outputLine($exception->getMessage());
83 return;
84 }
85 $this->displayHelpForCommand($command);
86 }
87 }
88
89 /**
90 * @return void
91 */
92 protected function displayHelpIndex() {
93 $this->buildCommandsIndex();
94
95 $this->outputLine('Extbase %s', array(t3lib_extMgm::getExtensionVersion('extbase')));
96 $this->outputLine('usage: ./cli_dispatch.phpsh extbase <command identifier>');
97 $this->outputLine();
98 $this->outputLine('The following commands are currently available:');
99
100 foreach ($this->commandsByExtensionsAndControllers as $extensionKey => $commandControllers) {
101 $this->outputLine('');
102 $this->outputLine('EXTENSION "%s":', array(strtoupper($extensionKey)));
103 $this->outputLine(str_repeat('-', self::MAXIMUM_LINE_LENGTH));
104 foreach ($commandControllers as $commands) {
105 foreach ($commands as $command) {
106 $description = wordwrap($command->getShortDescription(), self::MAXIMUM_LINE_LENGTH - 43, PHP_EOL . str_repeat(' ', 43), TRUE);
107 $shortCommandIdentifier = $this->commandManager->getShortestIdentifierForCommand($command);
108 $this->outputLine('%-2s%-40s %s', array(' ', $shortCommandIdentifier , $description));
109 }
110 $this->outputLine();
111 }
112 }
113 $this->outputLine('See \'./cli_dispatch.phpsh extbase help <command identifier>\' for more information about a specific command.');
114 $this->outputLine();
115 }
116
117 /**
118 * Render help text for a single command
119 *
120 * @param Tx_Extbase_MVC_CLI_Command $command
121 * @return void
122 */
123 protected function displayHelpForCommand(Tx_Extbase_MVC_CLI_Command $command) {
124 $this->outputLine();
125 $this->outputLine($command->getShortDescription());
126 $this->outputLine();
127
128 $this->outputLine('COMMAND:');
129 $this->outputLine('%-2s%s', array(' ', $command->getCommandIdentifier()));
130
131 $commandArgumentDefinitions = $command->getArgumentDefinitions();
132 $usage = '';
133 $hasOptions = FALSE;
134 foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
135 if (!$commandArgumentDefinition->isRequired()) {
136 $hasOptions = TRUE;
137 } else {
138 $usage .= sprintf(' <%s>', strtolower(preg_replace('/([A-Z])/', ' $1', $commandArgumentDefinition->getName())));
139 }
140 }
141
142 $usage = './cli_dispatch.phpsh extbase ' . $this->commandManager->getShortestIdentifierForCommand($command) . ($hasOptions ? ' [<options>]' : '') . $usage;
143
144 $this->outputLine();
145 $this->outputLine('USAGE:');
146 $this->outputLine(' ' . $usage);
147
148 $argumentDescriptions = array();
149 $optionDescriptions = array();
150
151 if ($command->hasArguments()) {
152 foreach ($commandArgumentDefinitions as $commandArgumentDefinition) {
153 $argumentDescription = $commandArgumentDefinition->getDescription();
154 $argumentDescription = wordwrap($argumentDescription, self::MAXIMUM_LINE_LENGTH - 23, PHP_EOL . str_repeat(' ', 23), TRUE);
155 if ($commandArgumentDefinition->isRequired()) {
156 $argumentDescriptions[] = vsprintf(' %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
157 } else {
158 $optionDescriptions[] = vsprintf(' %-20s %s', array($commandArgumentDefinition->getDashedName(), $argumentDescription));
159 }
160 }
161 }
162
163 if (count($argumentDescriptions) > 0) {
164 $this->outputLine();
165 $this->outputLine('ARGUMENTS:');
166 foreach ($argumentDescriptions as $argumentDescription) {
167 $this->outputLine($argumentDescription);
168 }
169 }
170
171 if (count($optionDescriptions) > 0) {
172 $this->outputLine();
173 $this->outputLine('OPTIONS:');
174 foreach ($optionDescriptions as $optionDescription) {
175 $this->outputLine($optionDescription);
176 }
177 }
178
179 if ($command->getDescription() !== '') {
180 $this->outputLine();
181 $this->outputLine('DESCRIPTION:');
182 $descriptionLines = explode(chr(10), $command->getDescription());
183 foreach ($descriptionLines as $descriptionLine) {
184 $this->outputLine('%-2s%s', array(' ', $descriptionLine));
185 }
186 }
187
188 $relatedCommandIdentifiers = $command->getRelatedCommandIdentifiers();
189 if ($relatedCommandIdentifiers !== array()) {
190 $this->outputLine();
191 $this->outputLine('SEE ALSO:');
192 foreach ($relatedCommandIdentifiers as $commandIdentifier) {
193 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
194 $this->outputLine('%-2s%s (%s)', array(' ', $commandIdentifier, $command->getShortDescription()));
195 }
196 }
197
198 $this->outputLine();
199 }
200
201 /**
202 * Displays an error message
203 *
204 * @internal
205 * @param Tx_Extbase_MVC_Exception_Command $exception
206 * @return void
207 * @author Bastian Waidelich <bastian@typo3.org>
208 */
209 public function errorCommand(Tx_Extbase_MVC_Exception_Command $exception) {
210 $this->outputLine($exception->getMessage());
211 if ($exception instanceof Tx_Extbase_MVC_Exception_AmbiguousCommandIdentifier) {
212 $this->outputLine('Please specify the complete command identifier. Matched commands:');
213 foreach ($exception->getMatchingCommands() as $matchingCommand) {
214 $this->outputLine(' %s', array($matchingCommand->getCommandIdentifier()));
215 }
216 }
217 $this->outputLine('');
218 $this->outputLine('Enter "./cli_dispatch.phpsh extbase help" for an overview of all available commands');
219 $this->outputLine('or "./cli_dispatch.phpsh extbase help <command identifier>" for a detailed description of the corresponding command.');
220 }
221
222 /**
223 * Builds an index of available commands. For each of them a Command object is
224 * added to the commands array of this class.
225 *
226 * @return void
227 * @author Robert Lemke <robert@typo3.org>
228 * @author Bastian Waidelich <bastian@typo3.org>
229 */
230 protected function buildCommandsIndex() {
231 $availableCommands = $this->commandManager->getAvailableCommands();
232 foreach ($availableCommands as $command) {
233 if ($command->isInternal()) {
234 continue;
235 }
236 $commandIdentifier = $command->getCommandIdentifier();
237 $extensionKey = strstr($commandIdentifier, ':', TRUE);
238 $commandControllerClassName = $command->getControllerClassName();
239 $commandName = $command->getControllerCommandName();
240 $this->commandsByExtensionsAndControllers[$extensionKey][$commandControllerClassName][$commandName] = $command;
241 }
242 }
243 }
244 ?>