[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Cli / RequestBuilder.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Cli;
3
4 /***************************************************************
5 * Copyright notice
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of TYPO3 Flow.
9 * All credits go to the TYPO3 Flow team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Builds a CLI request object from the raw command call
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class RequestBuilder {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
36 * @inject
37 */
38 protected $objectManager;
39
40 /**
41 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
42 * @inject
43 */
44 protected $reflectionService;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Mvc\Cli\CommandManager
48 * @inject
49 */
50 protected $commandManager;
51
52 /**
53 * Builds a CLI request object from a command line.
54 *
55 * The given command line may be a string (e.g. "myextension:foo do-that-thing --force") or
56 * an array consisting of the individual parts. The array must not include the script
57 * name (like in $argv) but start with command right away.
58 *
59 * @param mixed $commandLine The command line, either as a string or as an array
60 * @return \TYPO3\CMS\Extbase\Mvc\Cli\Request The CLI request as an object
61 */
62 public function build($commandLine = '') {
63 $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Request');
64 $request->setControllerObjectName('TYPO3\\CMS\\Extbase\\Command\\HelpCommandController');
65 $rawCommandLineArguments = is_array($commandLine) ? $commandLine : explode(' ', $commandLine);
66 if (count($rawCommandLineArguments) === 0) {
67 $request->setControllerCommandName('helpStub');
68 return $request;
69 }
70 $commandIdentifier = trim(array_shift($rawCommandLineArguments));
71 try {
72 $command = $this->commandManager->getCommandByIdentifier($commandIdentifier);
73 } catch (\TYPO3\CMS\Extbase\Mvc\Exception\CommandException $exception) {
74 $request->setArgument('exception', $exception);
75 $request->setControllerCommandName('error');
76 return $request;
77 }
78 $controllerObjectName = $command->getControllerClassName();
79 $controllerCommandName = $command->getControllerCommandName();
80 $request->setControllerObjectName($controllerObjectName);
81 $request->setControllerCommandName($controllerCommandName);
82 list($commandLineArguments, $exceedingCommandLineArguments) = $this->parseRawCommandLineArguments($rawCommandLineArguments, $controllerObjectName, $controllerCommandName);
83 $request->setArguments($commandLineArguments);
84 $request->setExceedingArguments($exceedingCommandLineArguments);
85 return $request;
86 }
87
88 /**
89 * Takes an array of unparsed command line arguments and options and converts it separated
90 * by named arguments, options and unnamed arguments.
91 *
92 * @param array $rawCommandLineArguments The unparsed command parts (such as "--foo") as an array
93 * @param string $controllerObjectName Object name of the designated command controller
94 * @param string $controllerCommandName Command name of the recognized command (ie. method name without "Command" suffix)
95 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentMixingException
96 * @return array All and exceeding command line arguments
97 */
98 protected function parseRawCommandLineArguments(array $rawCommandLineArguments, $controllerObjectName, $controllerCommandName) {
99 $commandLineArguments = array();
100 $exceedingArguments = array();
101 $commandMethodName = $controllerCommandName . 'Command';
102 $commandMethodParameters = $this->reflectionService->getMethodParameters($controllerObjectName, $commandMethodName);
103 $requiredArguments = array();
104 $optionalArguments = array();
105 $argumentNames = array();
106 foreach ($commandMethodParameters as $parameterName => $parameterInfo) {
107 $argumentNames[] = $parameterName;
108 if ($parameterInfo['optional'] === FALSE) {
109 $requiredArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']);
110 } else {
111 $optionalArguments[strtolower($parameterName)] = array('parameterName' => $parameterName, 'type' => $parameterInfo['type']);
112 }
113 }
114 $decidedToUseNamedArguments = FALSE;
115 $decidedToUseUnnamedArguments = FALSE;
116 $argumentIndex = 0;
117 while (count($rawCommandLineArguments) > 0) {
118 $rawArgument = array_shift($rawCommandLineArguments);
119 if ($rawArgument[0] === '-') {
120 if ($rawArgument[1] === '-') {
121 $rawArgument = substr($rawArgument, 2);
122 } else {
123 $rawArgument = substr($rawArgument, 1);
124 }
125 $argumentName = $this->extractArgumentNameFromCommandLinePart($rawArgument);
126 if (isset($optionalArguments[$argumentName])) {
127 $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $optionalArguments[$argumentName]['type']);
128 $commandLineArguments[$optionalArguments[$argumentName]['parameterName']] = $argumentValue;
129 } elseif (isset($requiredArguments[$argumentName])) {
130 if ($decidedToUseUnnamedArguments) {
131 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected named argument "%s". If you use unnamed arguments, all required arguments must be passed without a name.', $argumentName), 1309971821);
132 }
133 $decidedToUseNamedArguments = TRUE;
134 $argumentValue = $this->getValueOfCurrentCommandLineOption($rawArgument, $rawCommandLineArguments, $requiredArguments[$argumentName]['type']);
135 $commandLineArguments[$requiredArguments[$argumentName]['parameterName']] = $argumentValue;
136 unset($requiredArguments[$argumentName]);
137 }
138 } else {
139 if (count($requiredArguments) > 0) {
140 if ($decidedToUseNamedArguments) {
141 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentMixingException(sprintf('Unexpected unnamed argument "%s". If you use named arguments, all required arguments must be passed named.', $rawArgument), 1309971820);
142 }
143 $argument = array_shift($requiredArguments);
144 $commandLineArguments[$argument['parameterName']] = $rawArgument;
145 $decidedToUseUnnamedArguments = TRUE;
146 } else {
147 if ($argumentIndex < count($argumentNames)) {
148 $commandLineArguments[$argumentNames[$argumentIndex]] = $rawArgument;
149 } else {
150 $exceedingArguments[] = $rawArgument;
151 }
152 }
153 }
154 $argumentIndex++;
155 }
156 return array($commandLineArguments, $exceedingArguments);
157 }
158
159 /**
160 * Extracts the option or argument name from the name / value pair of a command line.
161 *
162 * @param string $commandLinePart Part of the command line, e.g. "my-important-option=SomeInterestingValue
163 * @return string The lowercased argument name, e.g. "myimportantoption
164 */
165 protected function extractArgumentNameFromCommandLinePart($commandLinePart) {
166 $nameAndValue = explode('=', $commandLinePart, 2);
167 return strtolower(str_replace('-', '', $nameAndValue[0]));
168 }
169
170 /**
171 * Returns the value of the first argument of the given input array. Shifts the parsed argument off the array.
172 *
173 * @param string $currentArgument The current argument
174 * @param array &$rawCommandLineArguments Array of the remaining command line arguments
175 * @param string $expectedArgumentType The expected type of the current argument, because booleans get special attention
176 * @return string The value of the first argument
177 */
178 protected function getValueOfCurrentCommandLineOption($currentArgument, array &$rawCommandLineArguments, $expectedArgumentType) {
179 if (!isset($rawCommandLineArguments[0]) && strpos($currentArgument, '=') === FALSE || isset($rawCommandLineArguments[0]) && $rawCommandLineArguments[0][0] === '-' && strpos($currentArgument, '=') === FALSE) {
180 return TRUE;
181 }
182 if (strpos($currentArgument, '=') === FALSE) {
183 $possibleValue = trim(array_shift($rawCommandLineArguments));
184 if (strpos($possibleValue, '=') === FALSE) {
185 if ($expectedArgumentType !== 'boolean') {
186 return $possibleValue;
187 }
188 if (array_search($possibleValue, array('on', '1', 'y', 'yes', 'true', 'TRUE')) !== FALSE) {
189 return TRUE;
190 }
191 if (array_search($possibleValue, array('off', '0', 'n', 'no', 'false', 'FALSE')) !== FALSE) {
192 return FALSE;
193 }
194 array_unshift($rawCommandLineArguments, $possibleValue);
195 return TRUE;
196 }
197 $currentArgument .= $possibleValue;
198 }
199 $splitArgument = explode('=', $currentArgument, 2);
200 while ((!isset($splitArgument[1]) || trim($splitArgument[1]) === '') && count($rawCommandLineArguments) > 0) {
201 $currentArgument .= array_shift($rawCommandLineArguments);
202 $splitArgument = explode('=', $currentArgument);
203 }
204 $value = isset($splitArgument[1]) ? $splitArgument[1] : '';
205 return $value;
206 }
207 }
208
209 ?>