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