TYPO3 CMS  TYPO3_7-6
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 
152  {
153  $this->configurationManager = $configurationManager;
154  $this->settings = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
155  }
156 
163  public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
164  {
165  $this->objectManager = $objectManager;
166  $this->arguments = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class);
167  }
168 
181  public function addFlashMessage($messageBody, $messageTitle = '', $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession = true)
182  {
183  if (!is_string($messageBody)) {
184  throw new \InvalidArgumentException('The message body must be of type string, "' . gettype($messageBody) . '" given.', 1243258395);
185  }
186  /* @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
188  \TYPO3\CMS\Core\Messaging\FlashMessage::class, $messageBody, $messageTitle, $severity, $storeInSession
189  );
190  $this->controllerContext->getFlashMessageQueue()->enqueue($flashMessage);
191  }
192 
204  public function canProcessRequest(\TYPO3\CMS\Extbase\Mvc\RequestInterface $request)
205  {
206  foreach ($this->supportedRequestTypes as $supportedRequestType) {
207  if ($request instanceof $supportedRequestType) {
208  return true;
209  }
210  }
211  return false;
212  }
213 
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 
276  public function forward($actionName, $controllerName = null, $extensionName = null, array $arguments = null)
277  {
278  $this->request->setDispatched(false);
279  if ($this->request instanceof WebRequest) {
280  $this->request->setControllerActionName($actionName);
281  if ($controllerName !== null) {
282  $this->request->setControllerName($controllerName);
283  }
284  if ($extensionName !== null) {
285  $this->request->setControllerExtensionName($extensionName);
286  }
287  }
288  if ($arguments !== null) {
289  $this->request->setArguments($arguments);
290  }
291  throw new StopActionException();
292  }
293 
315  protected function redirect($actionName, $controllerName = null, $extensionName = null, array $arguments = null, $pageUid = null, $delay = 0, $statusCode = 303)
316  {
317  if (!$this->request instanceof WebRequest) {
318  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539734);
319  }
320  if ($controllerName === null) {
321  $controllerName = $this->request->getControllerName();
322  }
323  $this->uriBuilder->reset()->setTargetPageUid($pageUid)->setCreateAbsoluteUri(true);
324  if (\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL')) {
325  $this->uriBuilder->setAbsoluteUriScheme('https');
326  }
327  $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName);
328  $this->redirectToUri($uri, $delay, $statusCode);
329  }
330 
343  protected function redirectToUri($uri, $delay = 0, $statusCode = 303)
344  {
345  if (!$this->request instanceof WebRequest) {
346  throw new UnsupportedRequestTypeException('redirect() only supports web requests.', 1220539735);
347  }
348 
349  $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
350 
351  $uri = $this->addBaseUriIfNecessary($uri);
352  $escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
353  $this->response->setContent('<html><head><meta http-equiv="refresh" content="' . (int)$delay . ';url=' . $escapedUri . '"/></head></html>');
354  if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
355  $this->response->setStatus($statusCode);
356  $this->response->setHeader('Location', (string)$uri);
357  }
358  throw new StopActionException();
359  }
360 
367  protected function addBaseUriIfNecessary($uri)
368  {
369  return \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl((string)$uri);
370  }
371 
384  public function throwStatus($statusCode, $statusMessage = null, $content = null)
385  {
386  if (!$this->request instanceof WebRequest) {
387  throw new UnsupportedRequestTypeException('throwStatus() only supports web requests.', 1220539739);
388  }
389  if ($this->response instanceof \TYPO3\CMS\Extbase\Mvc\Web\Response) {
390  $this->response->setStatus($statusCode, $statusMessage);
391  if ($content === null) {
392  $content = $this->response->getStatus();
393  }
394  }
395  $this->response->setContent($content);
396  throw new StopActionException();
397  }
398 
405  public function initializeControllerArgumentsBaseValidators()
406  {
408  foreach ($this->arguments as $argument) {
409  $validator = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
410  if ($validator !== null) {
411  $argument->setValidator($validator);
412  }
413  }
414  }
415 
422  protected function mapRequestArgumentsToControllerArguments()
423  {
425  foreach ($this->arguments as $argument) {
426  $argumentName = $argument->getName();
427  if ($this->request->hasArgument($argumentName)) {
428  $argument->setValue($this->request->getArgument($argumentName));
429  } elseif ($argument->isRequired()) {
430  throw new \TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException('Required argument "' . $argumentName . '" is not set for ' . $this->request->getControllerObjectName() . '->' . $this->request->getControllerActionName() . '.', 1298012500);
431  }
432  }
433  }
434 }
throwStatus($statusCode, $statusMessage=null, $content=null)
injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
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)