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
35 abstract class Tx_Extbase_MVC_Controller_AbstractController
implements Tx_Extbase_MVC_Controller_ControllerInterface
{
38 * @var Tx_Extbase_Object_ManagerInterface
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 * The flash messages. Use $this->flashMessages->add(...) to add a new Flash message.
109 * @var Tx_Extbase_MVC_Controller_FlashMessages
112 protected $flashMessages;
115 * Constructs the controller.
117 public function __construct() {
118 $this->initializeObjects();
119 list(, $this->extensionName
) = explode('_', get_class($this));
123 * Initializes objects this class depends on
127 protected function initializeObjects() {
128 $this->objectManager
= t3lib_div
::makeInstance('Tx_Extbase_Object_Manager');
129 $this->arguments
= t3lib_div
::makeInstance('Tx_Extbase_MVC_Controller_Arguments');
130 $this->arguments
->injectPersistenceManager(Tx_Extbase_Dispatcher
::getPersistenceManager());
131 $this->arguments
->injectQueryFactory(t3lib_div
::makeInstance('Tx_Extbase_Persistence_QueryFactory'));
135 * Injects the property mapper
137 * @param Tx_Extbase_Property_Mapper $propertyMapper The property mapper
140 public function injectPropertyMapper(Tx_Extbase_Property_Mapper
$propertyMapper) {
141 $this->propertyMapper
= $propertyMapper;
145 * Injects the settings of the extension.
147 * @param array $settings Settings container of the current extension
150 public function injectSettings(array $settings) {
151 $this->settings
= $settings;
155 * Injects the object manager
157 * @param Tx_Extbase_Object_ManagerInterface $objectManager
160 public function injectObjectManager(Tx_Extbase_Object_ManagerInterface
$objectManager) {
161 $this->objectManager
= $objectManager;
165 * Injects the validator resolver
167 * @param Tx_Extbase_Validation_ValidatorResolver $validatorResolver
170 public function injectValidatorResolver(Tx_Extbase_Validation_ValidatorResolver
$validatorResolver) {
171 $this->validatorResolver
= $validatorResolver;
175 * Injects the flash messages container
177 * @param Tx_Extbase_MVC_Controller_FlashMessages $flashMessages
180 public function injectFlashMessages(Tx_Extbase_MVC_Controller_FlashMessages
$flashMessages) {
181 $this->flashMessages
= $flashMessages;
185 * Checks if the current request type is supported by the controller.
187 * If your controller only supports certain request types, either
188 * replace / modify the supporteRequestTypes property or override this
191 * @param Tx_Extbase_MVC_Request $request The current request
192 * @return boolean TRUE if this request type is supported, otherwise FALSE
195 public function canProcessRequest(Tx_Extbase_MVC_Request
$request) {
196 foreach ($this->supportedRequestTypes
as $supportedRequestType) {
197 if ($request instanceof $supportedRequestType) return TRUE;
203 * Processes a general request. The result can be returned by altering the given response.
205 * @param Tx_Extbase_MVC_Request $request The request object
206 * @param Tx_Extbase_MVC_Response $response The response, modified by this handler
208 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType if the controller doesn't support the current request type
211 public function processRequest(Tx_Extbase_MVC_Request
$request, Tx_Extbase_MVC_Response
$response) {
212 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);
214 $this->request
= $request;
215 $this->request
->setDispatched(TRUE);
216 $this->response
= $response;
218 $this->uriBuilder
= t3lib_div
::makeInstance('Tx_Extbase_MVC_Web_Routing_UriBuilder');
219 $this->uriBuilder
->setRequest($request);
221 $this->initializeControllerArgumentsBaseValidators();
222 $this->mapRequestArgumentsToControllerArguments();
226 * Initialize the controller context
228 * @return Tx_Extbase_MVC_Controller_ControllerContext ControllerContext to be passed to the view
231 protected function buildControllerContext() {
232 $controllerContext = t3lib_div
::makeInstance('Tx_Extbase_MVC_Controller_ControllerContext');
233 $controllerContext->setRequest($this->request
);
234 $controllerContext->setResponse($this->response
);
235 if ($this->arguments
!== NULL) {
236 $controllerContext->setArguments($this->arguments
);
238 if ($this->argumentsMappingResults
!== NULL) {
239 $controllerContext->setArgumentsMappingResults($this->argumentsMappingResults
);
241 $controllerContext->setUriBuilder($this->uriBuilder
);
242 $controllerContext->setFlashMessages($this->flashMessages
);
243 return $controllerContext;
247 * Forwards the request to another controller.
249 * @param string $actionName Name of the action to forward to
250 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
251 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
252 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
254 * @throws Tx_Extbase_MVC_Exception_StopAction
257 public function forward($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL) {
258 $this->request
->setDispatched(FALSE);
259 $this->request
->setControllerActionName($actionName);
260 if ($controllerName !== NULL) $this->request
->setControllerName($controllerName);
261 if ($extensionName !== NULL) $this->request
->setControllerExtensionName($extensionName);
262 if ($arguments !== NULL) $this->request
->setArguments($arguments);
263 throw new Tx_Extbase_MVC_Exception_StopAction();
267 * Forwards the request to another action and / or controller.
269 * NOTE: This method only supports web requests and will thrown an exception
270 * if used with other request types.
272 * @param string $actionName Name of the action to forward to
273 * @param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
274 * @param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
275 * @param Tx_Extbase_MVC_Controller_Arguments $arguments Arguments to pass to the target action
276 * @param integer $pageUid Target page uid. If NULL, the current page uid is used
277 * @param integer $delay (optional) The delay in seconds. Default is no delay.
278 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
280 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
281 * @throws Tx_Extbase_MVC_Exception_StopAction
284 protected function redirect($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL, $pageUid = NULL, $delay = 0, $statusCode = 303) {
285 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
287 if ($controllerName === NULL) {
288 $controllerName = $this->request
->getControllerName();
290 if ($pageUid === NULL && isset($GLOBALS['TSFE'])) {
291 $pageUid = $GLOBALS['TSFE']->id
;
294 $uri = $this->uriBuilder
296 ->setTargetPageUid($pageUid)
297 ->uriFor($actionName, $arguments, $controllerName, $extensionName);
298 $this->redirectToURI($uri, $delay, $statusCode);
302 * Redirects the web request to another uri.
304 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
306 * @param mixed $uri A string representation of a URI
307 * @param integer $delay (optional) The delay in seconds. Default is no delay.
308 * @param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
309 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
310 * @throws Tx_Extbase_MVC_Exception_StopAction
313 protected function redirectToURI($uri, $delay = 0, $statusCode = 303) {
314 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('redirect() only supports web requests.', 1220539734);
316 $baseUri = $this->request
->getBaseURI();
318 $uri = $baseUri . (string)$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 * Sends the specified HTTP status immediately.
329 * NOTE: This method only supports web requests and will thrown an exception if used with other request types.
331 * @param integer $statusCode The HTTP status code
332 * @param string $statusMessage A custom HTTP status message
333 * @param string $content Body content which further explains the status
334 * @throws Tx_Extbase_MVC_Exception_UnsupportedRequestType If the request is not a web request
335 * @throws Tx_Extbase_MVC_Exception_StopAction
338 public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
339 if (!$this->request
instanceof Tx_Extbase_MVC_Web_Request
) throw new Tx_Extbase_MVC_Exception_UnsupportedRequestType('throwStatus() only supports web requests.', 1220539739);
341 $this->response
->setStatus($statusCode, $statusMessage);
342 if ($content === NULL) $content = $this->response
->getStatus();
343 $this->response
->setContent($content);
344 throw new Tx_Extbase_MVC_Exception_StopAction();
348 * Collects the base validators which were defined for the data type of each
349 * controller argument and adds them to the argument's validator chain.
353 public function initializeControllerArgumentsBaseValidators() {
354 foreach ($this->arguments
as $argument) {
355 $validator = $this->validatorResolver
->getBaseValidatorConjunction($argument->getDataType());
356 if ($validator !== NULL) $argument->setValidator($validator);
361 * Maps arguments delivered by the request object to the local controller arguments.
365 protected function mapRequestArgumentsToControllerArguments() {
366 $optionalPropertyNames = array();
367 $allPropertyNames = $this->arguments
->getArgumentNames();
368 foreach ($allPropertyNames as $propertyName) {
369 if ($this->arguments
[$propertyName]->isRequired() === FALSE) $optionalPropertyNames[] = $propertyName;
372 $validator = t3lib_div
::makeInstance('Tx_Extbase_MVC_Controller_ArgumentsValidator');
373 $this->propertyMapper
->mapAndValidate($allPropertyNames, $this->request
->getArguments(), $this->arguments
, $optionalPropertyNames, $validator);
375 $this->argumentsMappingResults
= $this->propertyMapper
->getMappingResults();