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