[TASK] Replace inject methods with @inject
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / CommandController.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
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 * A controller which processes requests from the command line
29 *
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 class CommandController implements \TYPO3\CMS\Extbase\Mvc\Controller\CommandControllerInterface {
33
34 const MAXIMUM_LINE_LENGTH = 79;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Request
38 */
39 protected $request;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Mvc\Cli\Response
43 */
44 protected $response;
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Mvc\Controller\Arguments
48 */
49 protected $arguments;
50
51 /**
52 * Name of the command method
53 *
54 * @var string
55 */
56 protected $commandMethodName = '';
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
60 * @inject
61 */
62 protected $reflectionService;
63
64 /**
65 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
66 */
67 protected $objectManager;
68
69 /**
70 * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
71 * @return void
72 */
73 public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
74 $this->objectManager = $objectManager;
75 $this->arguments = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments');
76 }
77
78 /**
79 * Checks if the current request type is supported by the controller.
80 *
81 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The current request
82 * @return boolean TRUE if this request type is supported, otherwise FALSE
83 */
84 public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request) {
85 return $request instanceof \TYPO3\CMS\Extbase\Mvc\Cli\Request;
86 }
87
88 /**
89 * Processes a command line request.
90 *
91 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface $request The request object
92 * @param \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response The response, modified by this controller
93 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
94 * @return void
95 * @api
96 */
97 public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
98 if (!$this->canProcessRequest($request)) {
99 throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException(get_class($this) . ' does not support requests of type "' . get_class($request) . '".', 1300787096);
100 }
101 $this->request = $request;
102 $this->request->setDispatched(TRUE);
103 $this->response = $response;
104 $this->commandMethodName = $this->resolveCommandMethodName();
105 $this->initializeCommandMethodArguments();
106 $this->mapRequestArgumentsToControllerArguments();
107 $this->callCommandMethod();
108 }
109
110 /**
111 * Resolves and checks the current command method name
112 *
113 * Note: The resulting command method name might not have the correct case, which isn't a problem because PHP is
114 * case insensitive regarding method names.
115 *
116 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException
117 * @return string Method name of the current command
118 */
119 protected function resolveCommandMethodName() {
120 $commandMethodName = $this->request->getControllerCommandName() . 'Command';
121 if (!is_callable(array($this, $commandMethodName))) {
122 throw new \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException('A command method "' . $commandMethodName . '()" does not exist in controller "' . get_class($this) . '".', 1300902143);
123 }
124 return $commandMethodName;
125 }
126
127 /**
128 * Initializes the arguments array of this controller by creating an empty argument object for each of the
129 * method arguments found in the designated command method.
130 *
131 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException
132 * @return void
133 */
134 protected function initializeCommandMethodArguments() {
135 $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
136 foreach ($methodParameters as $parameterName => $parameterInfo) {
137 $dataType = NULL;
138 if (isset($parameterInfo['type'])) {
139 $dataType = $parameterInfo['type'];
140 } elseif ($parameterInfo['array']) {
141 $dataType = 'array';
142 }
143 if ($dataType === NULL) {
144 throw new \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException('The argument type for parameter $' . $parameterName . ' of method ' . get_class($this) . '->' . $this->commandMethodName . '() could not be detected.', 1306755296);
145 }
146 $defaultValue = isset($parameterInfo['defaultValue']) ? $parameterInfo['defaultValue'] : NULL;
147 $this->arguments->addNewArgument($parameterName, $dataType, $parameterInfo['optional'] === FALSE, $defaultValue);
148 }
149 }
150
151 /**
152 * Maps arguments delivered by the request object to the local controller arguments.
153 *
154 * @return void
155 */
156 protected function mapRequestArgumentsToControllerArguments() {
157 foreach ($this->arguments as $argument) {
158 $argumentName = $argument->getName();
159 if ($this->request->hasArgument($argumentName)) {
160 $argument->setValue($this->request->getArgument($argumentName));
161 } elseif ($argument->isRequired()) {
162 $exception = new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('Required argument "' . $argumentName . '" is not set.', 1306755520);
163 $this->forward('error', 'TYPO3\\CMS\\Extbase\\Command\\HelpCommandController', array('exception' => $exception));
164 }
165 }
166 }
167
168 /**
169 * Forwards the request to another command and / or CommandController.
170 *
171 * Request is directly transferred to the other command / controller
172 * without the need for a new request.
173 *
174 * @param string $commandName
175 * @param string $controllerObjectName
176 * @param array $arguments
177 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
178 * @return void
179 */
180 protected function forward($commandName, $controllerObjectName = NULL, array $arguments = array()) {
181 $this->request->setDispatched(FALSE);
182 $this->request->setControllerCommandName($commandName);
183 if ($controllerObjectName !== NULL) {
184 $this->request->setControllerObjectName($controllerObjectName);
185 }
186 $this->request->setArguments($arguments);
187 $this->arguments->removeAll();
188 throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
189 }
190
191 /**
192 * Calls the specified command method and passes the arguments.
193 *
194 * If the command returns a string, it is appended to the content in the
195 * response object. If the command doesn't return anything and a valid
196 * view exists, the view is rendered automatically.
197 *
198 * @return void
199 */
200 protected function callCommandMethod() {
201 $preparedArguments = array();
202 foreach ($this->arguments as $argument) {
203 $preparedArguments[] = $argument->getValue();
204 }
205 $commandResult = call_user_func_array(array($this, $this->commandMethodName), $preparedArguments);
206 if (is_string($commandResult) && strlen($commandResult) > 0) {
207 $this->response->appendContent($commandResult);
208 } elseif (is_object($commandResult) && method_exists($commandResult, '__toString')) {
209 $this->response->appendContent((string) $commandResult);
210 }
211 }
212
213 /**
214 * Outputs specified text to the console window
215 * You can specify arguments that will be passed to the text via sprintf
216 *
217 * @see http://www.php.net/sprintf
218 * @param string $text Text to output
219 * @param array $arguments Optional arguments to use for sprintf
220 * @return void
221 */
222 protected function output($text, array $arguments = array()) {
223 if ($arguments !== array()) {
224 $text = vsprintf($text, $arguments);
225 }
226 $this->response->appendContent($text);
227 }
228
229 /**
230 * Outputs specified text to the console window and appends a line break
231 *
232 * @param string $text Text to output
233 * @param array $arguments Optional arguments to use for sprintf
234 * @return string
235 * @see output()
236 */
237 protected function outputLine($text = '', array $arguments = array()) {
238 return $this->output($text . PHP_EOL, $arguments);
239 }
240
241 /**
242 * Exits the CLI through the dispatcher
243 * An exit status code can be specified @see http://www.php.net/exit
244 *
245 * @param integer $exitCode Exit code to return on exit
246 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
247 * @return void
248 */
249 protected function quit($exitCode = 0) {
250 $this->response->setExitCode($exitCode);
251 throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
252 }
253
254 /**
255 * Sends the response and exits the CLI without any further code execution
256 * Should be used for commands that flush code caches.
257 *
258 * @param integer $exitCode Exit code to return on exit
259 * @return void
260 */
261 protected function sendAndExit($exitCode = 0) {
262 $this->response->send();
263 die($exitCode);
264 }
265 }
266
267 ?>