TYPO3 CMS  TYPO3_8-7
AbstractController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
27 abstract class AbstractController implements ControllerInterface
28 {
33 
37  protected $objectManager;
38 
42  protected $uriBuilder;
43 
47  protected $extensionName;
48 
55  protected $settings;
56 
63  protected $request;
64 
71  protected $response;
72 
76  protected $validatorResolver;
77 
81  protected $arguments;
82 
86  public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
87  {
88  $this->signalSlotDispatcher = $signalSlotDispatcher;
89  }
90 
94  public function injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
95  {
96  $this->validatorResolver = $validatorResolver;
97  }
98 
106  protected $supportedRequestTypes = [\TYPO3\CMS\Extbase\Mvc\Request::class];
107 
113 
118  public function getControllerContext()
119  {
121  }
122 
127 
131  public function __construct()
132  {
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 
151  {
152  $this->configurationManager = $configurationManager;
153  $this->settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
154  }
155 
161  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
162  {
163  $this->objectManager = $objectManager;
164  $this->arguments = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class);
165  }
166 
178  public function addFlashMessage($messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = true)
179  {
180  if (!is_string($messageBody)) {
181  throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1243258395);
182  }
183  /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
185  \TYPO3\CMS\Core\Messaging\FlashMessage::class,
186  (string)$messageBody,
187  (string)$messageTitle,
188  $severity,
189  $storeInSession
190  );
191  $this->controllerContext->getFlashMessageQueue()->enqueue($flashMessage);
192  }
193 
205  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
206  {
207  foreach ($this->supportedRequestTypes as $supportedRequestType) {
208  if ($request instanceof $supportedRequestType) {
209  return true;
210  }
211  }
212  return false;
213  }
214 
223  public function processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
224  {
225  if (!$this->canProcessRequest($request)) {
226  throw new UnsupportedRequestTypeException(get_class($this) . ' does not support requests of type "' . get_class($request) . '". Supported types are: ' . implode(' ', $this->supportedRequestTypes), 1187701132);
227  }
228  if ($response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response && $request instanceof WebRequest) {
229  $response->setRequest($request);
230  }
231  $this->request = $request;
232  $this->request->setDispatched(true);
233  $this->response = $response;
234  $this->uriBuilder = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);
235  $this->uriBuilder->setRequest($request);
236  $this->initializeControllerArgumentsBaseValidators();
237  $this->mapRequestArgumentsToControllerArguments();
238  $this->controllerContext = $this->buildControllerContext();
239  }
240 
247  protected function buildControllerContext()
248  {
250  $controllerContext = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class);
251  $controllerContext->setRequest($this->request);
252  $controllerContext->setResponse($this->response);
253  if ($this->arguments !== null) {
254  $controllerContext->setArguments($this->arguments);
255  }
256  $controllerContext->setUriBuilder($this->uriBuilder);
257 
258  return $controllerContext;
259  }
260 
275  public function forward($actionName, $controllerName = null, $extensionName = null, array $arguments = null)
276  {
277  $this->request->setDispatched(false);
278  if ($this->request instanceof WebRequest) {
279  $this->request->setControllerActionName($actionName);
280  if ($controllerName !== null) {
281  $this->request->setControllerName($controllerName);
282  }
283  if ($extensionName !== null) {
284  $this->request->setControllerExtensionName($extensionName);
285  }
286  }
287  if ($arguments !== null) {
288  $this->request->setArguments($arguments);
289  }
290  throw new StopActionException('forward', 1476045801);
291  }
292 
313  protected function redirect($actionName, $controllerName = null, $extensionName = null, array $arguments = null, $pageUid = null, $delay = 0, $statusCode = 303)
314  {
315  if (!$this->request instanceof WebRequest) {
316  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539734);
317  }
318  if ($controllerName === null) {
319  $controllerName = $this->request->getControllerName();
320  }
321  $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(true);
322  if (\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL')) {
323  $this->uriBuilder->setAbsoluteUriScheme('https');
324  }
325  $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName);
326  $this->redirectToUri($uri, $delay, $statusCode);
327  }
328 
341  protected function redirectToUri($uri, $delay = 0, $statusCode = 303)
342  {
343  if (!$this->request instanceof WebRequest) {
344  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539735);
345  }
346 
347  $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
348 
349  $uri = $this->addBaseUriIfNecessary($uri);
350  $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
351  $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
352  if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
353  $this->response->setStatus($statusCode);
354  $this->response->setHeader('Location', (string)$uri);
355  }
356  // Avoid caching the plugin when we issue a redirect response
357  // This means that even when an action is configured as cachable
358  // we avoid the plugin to be cached, but keep the page cache untouched
359  $contentObject = $this->configurationManager->getContentObject();
360  if ($contentObject->getUserObjectType() === \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::OBJECTTYPE_USER) {
361  $contentObject->convertToUserIntObject();
362  }
363 
364  throw new StopActionException('redirectToUri', 1476045828);
365  }
366 
373  protected function addBaseUriIfNecessary($uri)
374  {
375  return \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl((string)$uri);
376  }
377 
390  public function throwStatus($statusCode, $statusMessage = null, $content = null)
391  {
392  if (!$this->request instanceof WebRequest) {
393  throw new UnsupportedRequestTypeException('throwStatus() only supports web requests.', 1220539739);
394  }
395  if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
396  $this->response->setStatus($statusCode, $statusMessage);
397  if ($content === null) {
398  $content = $this->response->getStatus();
399  }
400  }
401  $this->response->setContent($content);
402  throw new StopActionException('throwStatus', 1476045871);
403  }
404 
409  public function initializeControllerArgumentsBaseValidators()
410  {
412  foreach ($this->arguments as $argument) {
413  $validator = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
414  if ($validator !== null) {
415  $argument->setValidator($validator);
416  }
417  }
418  }
419 
425  protected function mapRequestArgumentsToControllerArguments()
426  {
428  foreach ($this->arguments as $argument) {
429  $argumentName = $argument->getName();
430  if ($this->request->hasArgument($argumentName)) {
431  $argument->setValue($this->request->getArgument($argumentName));
432  } elseif ($argument->isRequired()) {
433  throw new \TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException('Required argument "' . $argumentName . '" is not set for ' . $this->request->getControllerObjectName() . '->' . $this->request->getControllerActionName() . '.', 1298012500);
434  }
435  }
436  }
437 }
throwStatus($statusCode, $statusMessage=null, $content=null)
injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
static makeInstance($className,... $constructorArguments)
injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
processRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request, \TYPO3\CMS\Extbase\Mvc\ResponseInterface $response)
forward($actionName, $controllerName=null, $extensionName=null, array $arguments=null)
injectValidatorResolver(\TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver)
addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)