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