‪TYPO3CMS  9.5
AbstractApplication.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\RequestHandlerInterface;
23 
28 {
29  private const ‪MULTI_LINE_HEADERS = [
30  'set-cookie',
31  ];
32 
36  protected ‪$requestHandler = '';
37 
41  protected ‪$middlewareStack = '';
42 
48  {
49  $resolver = new ‪MiddlewareStackResolver(
50  GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Package\PackageManager::class),
51  GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Service\DependencyOrderingService::class),
52  GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core')
53  );
54  $middlewares = $resolver->resolve($this->middlewareStack);
55 
56  return new ‪MiddlewareDispatcher(‪$requestHandler, $middlewares);
57  }
58 
64  protected function ‪sendResponse(ResponseInterface $response)
65  {
66  if ($response instanceof ‪NullResponse) {
67  return;
68  }
69 
70  // @todo This requires some merge strategy or header callback handling
71  if (!headers_sent()) {
72  // If the response code was not changed by legacy code (still is 200)
73  // then allow the PSR-7 response object to explicitly set it.
74  // Otherwise let legacy code take precedence.
75  // This code path can be deprecated once we expose the response object to third party code
76  if (http_response_code() === 200) {
77  header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
78  }
79 
80  foreach ($response->getHeaders() as $name => $values) {
81  if (in_array(strtolower($name), self::MULTI_LINE_HEADERS, true)) {
82  foreach ($values as $value) {
83  header($name . ': ' . $value, false);
84  }
85  } else {
86  header($name . ': ' . implode(', ', $values));
87  }
88  }
89  }
90  $body = $response->getBody();
91  if ($body instanceof SelfEmittableStreamInterface) {
92  // Optimization for streams that use php functions like readfile() as fastpath for serving files.
93  $body->emit();
94  } else {
95  echo $body->__toString();
96  }
97  }
98 
103  protected function ‪handle(ServerRequestInterface $request): ResponseInterface
104  {
105  ‪$requestHandler = GeneralUtility::makeInstance($this->requestHandler);
107 
108  return $dispatcher->handle($request);
109  }
110 
116  final public function ‪run(callable $execute = null)
117  {
118  try {
119  $response = $this->‪handle(
121  );
122  if ($execute !== null) {
123  call_user_func($execute);
124  }
125  } catch (ImmediateResponseException $exception) {
126  $response = $exception->getResponse();
127  }
128 
129  $this->‪sendResponse($response);
130  }
131 }
‪TYPO3\CMS\Core\Http\AbstractApplication\$requestHandler
‪string $requestHandler
Definition: AbstractApplication.php:35
‪TYPO3
‪TYPO3\CMS\Core\Http\MiddlewareStackResolver
Definition: MiddlewareStackResolver.php:29
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:24
‪TYPO3\CMS\Core\Http\AbstractApplication\sendResponse
‪sendResponse(ResponseInterface $response)
Definition: AbstractApplication.php:62
‪TYPO3\CMS\Core\Http\AbstractApplication\MULTI_LINE_HEADERS
‪const MULTI_LINE_HEADERS
Definition: AbstractApplication.php:29
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:32
‪TYPO3\CMS\Core\Http\AbstractApplication
Definition: AbstractApplication.php:28
‪TYPO3\CMS\Core\Http\RequestHandlerInterface
Definition: RequestHandlerInterface.php:28
‪TYPO3\CMS\Core\Core\ApplicationInterface
Definition: ApplicationInterface.php:23
‪TYPO3\CMS\Core\Http\ImmediateResponseException\getResponse
‪Response getResponse()
Definition: ImmediateResponseException.php:47
‪TYPO3\CMS\Core\Http\AbstractApplication\$middlewareStack
‪string $middlewareStack
Definition: AbstractApplication.php:39
‪TYPO3\CMS\Core\Http\AbstractApplication\handle
‪ResponseInterface handle(ServerRequestInterface $request)
Definition: AbstractApplication.php:101
‪TYPO3\CMS\Core\Http\SelfEmittableStreamInterface
Definition: SelfEmittableStreamInterface.php:27
‪TYPO3\CMS\Core\Http\AbstractApplication\createMiddlewareDispatcher
‪MiddlewareDispatcher createMiddlewareDispatcher(RequestHandlerInterface $requestHandler)
Definition: AbstractApplication.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Http\AbstractApplication\run
‪run(callable $execute=null)
Definition: AbstractApplication.php:114
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:29
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:36
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3