2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * An abstract base class for Controllers
32 * @subpackage MVC\Controller
36 abstract class Tx_Extbase_MVC_Controller_AbstractController
implements Tx_Extbase_MVC_Controller_ControllerInterface
{
38 * @var Tx_Extbase_Object_ObjectManagerInterface
40 protected $objectManager;
43 * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
45 protected $uriBuilder;
48 * @var string Key of the extension this controller belongs to
50 protected $extensionName;
53 * Contains the settings of the current extension
61 * The current request.
63 * @var Tx_Extbase_MVC_Request
69 * The response which will be returned by this action controller
71 * @var Tx_Extbase_MVC_Response
77 * @var Tx_Extbase_Property_Mapper
79 protected $propertyMapper;
82 * @var Tx_Extbase_Validation_ValidatorResolver
84 protected $validatorResolver;
87 * @var Tx_Extbase_MVC_Controller_Arguments Arguments passed to the controller
92 * The results of the mapping of request arguments to controller arguments
93 * @var Tx_Extbase_Property_MappingResults
96 protected $argumentsMappingResults;
99 * An array of supported request types. By default only web requests are supported.
100 * Modify or replace this array if your specific controller supports certain
101 * (additional) request types.
104 protected $supportedRequestTypes = array('Tx_Extbase_MVC_Request');
107 * @var Tx_Extbase_MVC_Controller_ControllerContext
110 protected $controllerContext;
113 * The flash messages. DEPRECATED. Use $this->flashMessageContainer instead.
115 * @var Tx_Extbase_MVC_Controller_FlashMessages
118 protected $flashMessages;
121 * The flash messages. Use $this->flashMessageContainer->add(...) to add a new Flash message.
123 * @var Tx_Extbase_MVC_Controller_FlashMessages
126 protected $flashMessageContainer;
129 * Constructs the controller.
131 public function __construct() {
132 list(, $this->extensionName
) = explode('_', get_class($this));
137 * Injects the property mapper
139 * @param Tx_Extbase_Property_Mapper $propertyMapper The property mapper
142 public function injectPropertyMapper(Tx_Extbase_Property_Mapper
$propertyMapper) {
143 $this->propertyMapper
= $propertyMapper;
147 * Injects the settings of the extension.
149 * @param array $settings Settings container of the current extension
152 public function setSettings(array $settings) {
153 $this->settings
= $settings;
157 * Injects the object manager
159 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
162 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface
$objectManager) {
163 $this->objectManager
= $objectManager;
164 $this->arguments
= $this->objectManager
->create('Tx_Extbase_MVC_Controller_Arguments');
168 * Injects the validator resolver
170 * @param Tx_Extbase_Validation_ValidatorResolver $validatorResolver
173 public function injectValidatorResolver(Tx_Extbase_Validation_ValidatorResolver
$validatorResolver) {
174 $this->validatorResolver
= $validatorResolver;
178 * Injects the flash messages container
180 * @param Tx_Extbase_MVC_Controller_FlashMessages $flashMessages
183 public function injectFlashMessageContainer(Tx_Extbase_MVC_Controller_FlashMessages
$flashMessageContainer) {
184 $this->flashMessageContainer
= $flashMessageContainer;
185 $this->flashMessages
= $flashMessageContainer; // deprecated, but should still work.
189 * Checks if the current request type is supported by the controller.
191 * If your controller only supports certain request types, either
192 * replace / modify the supporteRequestTypes property or override this
195 * @param Tx_Extbase_MVC_Request $request The current request
196 * @return boolean TRUE if this request type is supported, otherwise FALSE
199 public function canProcessRequest(Tx_Extbase_MVC_Request
$request) {
200 foreach ($this->supportedRequestTypes
as $supportedRequestType) {
201 if ($request instanceof $supportedRequestType) return TRUE;
207 * Processes a general request. The result can be returned by altering the given response.
209 * @param Tx_Extbase_MVC_Request $request The request object
210 * @param Tx_Extbase_MVC_Response $response The response, modified by this handler
212 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType if the controller doesn't support the current request type
215 public function processRequest(Tx_Extbase_MVC_Request
$request, Tx_Extbase_MVC_Response
$response) {
216 if (!$this->canProcessRequest($request)) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType(get_class($this) . ' does not support requests of type "' . get_class($request) . '". Supported types are: ' . implode(' ', $this->supportedRequestTypes
) , 1187701131);
218 $this->request
= $request;
219 $this->request
->setDispatched(TRUE);
220 $this->response
= $response;
222 $this->uriBuilder
= $this->objectManager
->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
223 $this->uriBuilder
->setRequest($request);
225 $this->initializeControllerArgumentsBaseValidators();
226 $this->mapRequestArgumentsToControllerArguments();
227 $this->controllerContext
= $this->buildControllerContext();
231 * Initialize the controller context
233 * @return Tx_Extbase_MVC_Controller_ControllerContext ControllerContext to be passed to the view
236 protected function buildControllerContext() {
237 $controllerContext = $this->objectManager
->create('Tx_Extbase_MVC_Controller_ControllerContext');
238 $controllerContext->setRequest($this->request
);
239 $controllerContext->setResponse($this->response
);
240 if ($this->arguments
!== NULL) {
241 $controllerContext->setArguments($this->arguments
);
243 if ($this->argumentsMappingResults
!== NULL) {
244 $controllerContext->setArgumentsMappingResults($this->argumentsMappingResults
);
246 $controllerContext->setUriBuilder($this->uriBuilder
);
247 $controllerContext->setFlashMessageContainer($this->flashMessageContainer
);
248 return $controllerContext;
252 * Forwards the request to another controller.
254 * @param string $actionName Name of the action to forward to
255 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
256 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
257 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
259 * @throws Tx_Extbase_MVC_Exception_StopAction
262 public function forward($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL) {
263 $this->request
->setDispatched(FALSE);
264 $this->request
->setControllerActionName($actionName);
265 if ($controllerName !== NULL) $this->request
->setControllerName($controllerName);
266 if ($extensionName !== NULL) $this->request
->setControllerExtensionName($extensionName);
267 if ($arguments !== NULL) $this->request
->setArguments($arguments);
268 throw new Tx_Extbase_MVC_Exception_StopAction();
272 * Forwards the request to another action and / or controller.
274 * NOTE: This method only supports web requests and will thrown an exception
275 * if used with other request types.
277 * @param string $actionName Name of the action to forward to
278 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
279 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
280 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
281 * @param integer $pageUid Target page uid. If NULL, the current page uid is used
282 * @param integer $delay (optional) The delay in seconds. Default is no delay.
283 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
285 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
286 * @throws Tx_Extbase_MVC_Exception_StopAction
289 protected function redirect($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL, $pageUid = NULL, $delay = 0, $statusCode = 303) {
290 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
292 if ($controllerName === NULL) {
293 $controllerName = $this->request
->getControllerName();
296 $uri = $this->uriBuilder
298 ->setTargetPageUid($pageUid)
299 ->uriFor($actionName, $arguments, $controllerName, $extensionName);
300 $this->redirectToURI($uri, $delay, $statusCode);
304 * Redirects the web request to another uri.
306 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
308 * @param mixed $uri A string representation of a URI
309 * @param integer $delay (optional) The delay in seconds. Default is no delay.
310 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
311 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
312 * @throws Tx_Extbase_MVC_Exception_StopAction
315 protected function redirectToURI($uri, $delay = 0, $statusCode = 303) {
316 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
318 $uri = $this->addBaseUriIfNecessary($uri);
319 $escapedUri = htmlentities($uri, ENT_QUOTES
, 'utf-8');
320 $this->response
->setContent('<html><head><meta http-equiv="refresh" content="' . intval($delay) . ';url=' . $escapedUri . '"/></head></html>');
321 $this->response
->setStatus($statusCode);
322 $this->response
->setHeader('Location', (string)$uri);
323 throw new Tx_Extbase_MVC_Exception_StopAction();
327 * Adds the base uri if not already in place.
329 * @param string $uri The URI
332 protected function addBaseUriIfNecessary($uri) {
333 $baseUri = $this->request
->getBaseURI();
334 if(stripos($uri, $baseUri) !== 0) {
335 $uri = $baseUri . (string)$uri;
341 * Sends the specified HTTP status immediately.
343 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
345 * @param integer $statusCode The HTTP status code
346 * @param string $statusMessage A custom HTTP status message
347 * @param string $content Body content which further explains the status
348 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
349 * @throws Tx_Extbase_MVC_Exception_StopAction
352 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
353 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
355 $this->response
->setStatus($statusCode, $statusMessage);
356 if ($content === NULL) $content = $this->response
->getStatus();
357 $this->response
->setContent($content);
358 throw new Tx_Extbase_MVC_Exception_StopAction();
362 * Collects the base validators which were defined for the data type of each
363 * controller argument and adds them to the argument's validator chain.
367 public function initializeControllerArgumentsBaseValidators() {
368 foreach ($this->arguments
as $argument) {
369 $validator = $this->validatorResolver
->getBaseValidatorConjunction($argument->getDataType());
370 if ($validator !== NULL) $argument->setValidator($validator);
375 * Maps arguments delivered by the request object to the local controller arguments.
379 protected function mapRequestArgumentsToControllerArguments() {
380 $optionalPropertyNames = array();
381 $allPropertyNames = $this->arguments
->getArgumentNames();
382 foreach ($allPropertyNames as $propertyName) {
383 if ($this->arguments
[$propertyName]->isRequired() === FALSE) $optionalPropertyNames[] = $propertyName;
386 $validator = $this->objectManager
->create('Tx_Extbase_MVC_Controller_ArgumentsValidator');
387 $this->propertyMapper
->mapAndValidate($allPropertyNames, $this->request
->getArguments(), $this->arguments
, $optionalPropertyNames, $validator);
389 $this->argumentsMappingResults
= $this->propertyMapper
->getMappingResults();