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