TYPO3 CMS  TYPO3_6-2
AbstractController.php
Go to the documentation of this file.
1 <?php
3 
22 
28 
32  protected $objectManager;
33 
37  protected $uriBuilder;
38 
42  protected $extensionName;
43 
50  protected $settings;
51 
58  protected $request;
59 
66  protected $response;
67 
74 
79  protected $validatorResolver;
80 
84  protected $arguments;
85 
94 
102  protected $supportedRequestTypes = array('TYPO3\\CMS\\Extbase\\Mvc\\Request');
103 
109 
114  public function getControllerContext() {
116  }
117 
123 
128 
132  public function __construct() {
133  $className = get_class($this);
134  if (strpos($className, '\\') !== FALSE) {
135  $classNameParts = explode('\\', $className, 4);
136  // Skip vendor and product name for core classes
137  if (strpos($className, 'TYPO3\\CMS\\') === 0) {
138  $this->extensionName = $classNameParts[2];
139  } else {
140  $this->extensionName = $classNameParts[1];
141  }
142  } else {
143  list(, $this->extensionName) = explode('_', $className);
144  }
145  }
146 
152  $this->configurationManager = $configurationManager;
153  $this->settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
154  }
155 
162  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) {
163  $this->objectManager = $objectManager;
164  $this->arguments = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments');
165  }
166 
176  public function injectFlashMessageContainer(\TYPO3\CMS\Extbase\Mvc\Controller\FlashMessageContainer $flashMessageContainer) {
177  $this->flashMessageContainer = $flashMessageContainer;
178  }
179 
192  public function addFlashMessage($messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = TRUE) {
193  if (!is_string($messageBody)) {
194  throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1243258395);
195  }
196  /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
198  'TYPO3\\CMS\\Core\\Messaging\\FlashMessage', $messageBody, $messageTitle, $severity, $storeInSession
199  );
200  $this->controllerContext->getFlashMessageQueue()->enqueue($flashMessage);
201  }
202 
214  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request) {
215  foreach ($this->supportedRequestTypes as $supportedRequestType) {
216  if ($request instanceof $supportedRequestType) {
217  return TRUE;
218  }
219  }
220  return FALSE;
221  }
222 
232  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response) {
233  if (!$this->canProcessRequest($request)) {
234  throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException(get_class($this) . ' does not support requests of type "' . get_class($request) . '". Supported types are: ' . implode(' ', $this->supportedRequestTypes), 1187701132);
235  }
236  $response->setRequest($request);
237  $this->request = $request;
238  $this->request->setDispatched(TRUE);
239  $this->response = $response;
240  $this->uriBuilder = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
241  $this->uriBuilder->setRequest($request);
243  $this->mapRequestArgumentsToControllerArguments();
244  $this->controllerContext = $this->buildControllerContext();
245  }
246 
253  protected function buildControllerContext() {
255  $controllerContext = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext');
256  $controllerContext->setRequest($this->request);
257  $controllerContext->setResponse($this->response);
258  if ($this->arguments !== NULL) {
259  $controllerContext->setArguments($this->arguments);
260  }
261  if ($this->argumentsMappingResults !== NULL) {
262  $controllerContext->setArgumentsMappingResults($this->argumentsMappingResults);
263  }
264  $controllerContext->setUriBuilder($this->uriBuilder);
265 
266  $controllerContext->setFlashMessageContainer($this->flashMessageContainer);
267  return $controllerContext;
268  }
269 
285  public function forward($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL) {
286  $this->request->setDispatched(FALSE);
287  $this->request->setControllerActionName($actionName);
288  if ($controllerName !== NULL) {
289  $this->request->setControllerName($controllerName);
290  }
291  if ($extensionName !== NULL) {
292  $this->request->setControllerExtensionName($extensionName);
293  }
294  if ($arguments !== NULL) {
295  $this->request->setArguments($arguments);
296  }
297  throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
298  }
299 
321  protected function redirect($actionName, $controllerName = NULL, $extensionName = NULL, array $arguments = NULL, $pageUid = NULL, $delay = 0, $statusCode = 303) {
322  if (!$this->request instanceof \TYPO3\CMS\Extbase\Mvc\Web\Request) {
323  throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539734);
324  }
325  if ($controllerName === NULL) {
326  $controllerName = $this->request->getControllerName();
327  }
328  $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(TRUE);
329  if (\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL')) {
330  $this->uriBuilder->setAbsoluteUriScheme('https');
331  }
332  $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName);
333  $this->redirectToUri($uri, $delay, $statusCode);
334  }
335 
348  protected function redirectToUri($uri, $delay = 0, $statusCode = 303) {
349  if (!$this->request instanceof \TYPO3\CMS\Extbase\Mvc\Web\Request) {
350  throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539735);
351  }
352 
353  $this->objectManager->get('TYPO3\CMS\Extbase\Service\CacheService')->clearCachesOfRegisteredPageIds();
354 
355  $uri = $this->addBaseUriIfNecessary($uri);
356  $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
357  $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
358  $this->response->setStatus($statusCode);
359  $this->response->setHeader('Location', (string) $uri);
360  throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
361  }
362 
369  protected function addBaseUriIfNecessary($uri) {
370  return \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl((string) $uri);
371  }
372 
385  public function throwStatus($statusCode, $statusMessage = NULL, $content = NULL) {
386  if (!$this->request instanceof \TYPO3\CMS\Extbase\Mvc\Web\Request) {
387  throw new \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException('throwStatus() only supports web requests.', 1220539739);
388  }
389  $this->response->setStatus($statusCode, $statusMessage);
390  if ($content === NULL) {
391  $content = $this->response->getStatus();
392  }
393  $this->response->setContent($content);
394  throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException();
395  }
396 
404  foreach ($this->arguments as $argument) {
405  $validator = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
406  if ($validator !== NULL) {
407  $argument->setValidator($validator);
408  }
409  }
410  }
411 
418  protected function mapRequestArgumentsToControllerArguments() {
419  if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
420  foreach ($this->arguments as $argument) {
421  $argumentName = $argument->getName();
422  if ($this->request->hasArgument($argumentName)) {
423  $argument->setValue($this->request->getArgument($argumentName));
424  } elseif ($argument->isRequired()) {
425  throw new \TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException('Required argument "' . $argumentName . '" is not set for ' . $this->request->getControllerObjectName() . '->' . $this->request->getControllerActionName() . '.', 1298012500);
426  }
427  }
428  } else {
429  // @deprecated since Extbase 1.4, will be removed two versions after Extbase 6.1
430  $optionalPropertyNames = array();
431  $allPropertyNames = $this->arguments->getArgumentNames();
432  foreach ($allPropertyNames as $propertyName) {
433  if ($this->arguments[$propertyName]->isRequired() === FALSE) {
434  $optionalPropertyNames[] = $propertyName;
435  }
436  }
438  $validator = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ArgumentsValidator');
439  $this->deprecatedPropertyMapper->mapAndValidate($allPropertyNames, $this->request->getArguments(), $this->arguments, $optionalPropertyNames, $validator);
440  $this->argumentsMappingResults = $this->deprecatedPropertyMapper->getMappingResults();
441  }
442  }
443 }
forward($actionName, $controllerName=NULL, $extensionName=NULL, array $arguments=NULL)
redirect($actionName, $controllerName=NULL, $extensionName=NULL, array $arguments=NULL, $pageUid=NULL, $delay=0, $statusCode=303)
addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=TRUE)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
injectFlashMessageContainer(\TYPO3\CMS\Extbase\Mvc\Controller\FlashMessageContainer $flashMessageContainer)
throwStatus($statusCode, $statusMessage=NULL, $content=NULL)