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