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