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