‪TYPO3CMS  ‪main
AbstractApplication.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
24 
28 abstract class ‪AbstractApplication implements ‪ApplicationInterface, RequestHandlerInterface
29 {
30  private const ‪MULTI_LINE_HEADERS = [
31  'set-cookie',
32  ];
33 
37  protected ‪$requestHandler;
38 
42  protected function ‪sendResponse(ResponseInterface $response)
43  {
44  if ($response instanceof ‪NullResponse) {
45  return;
46  }
47 
48  // @todo This requires some merge strategy or header callback handling
49  if (!headers_sent()) {
50  // If the response code was not changed by legacy code (still is 200)
51  // then allow the PSR-7 response object to explicitly set it.
52  // Otherwise let legacy code take precedence.
53  // This code path can be deprecated once we expose the response object to third party code
54  if (http_response_code() === 200) {
55  header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
56  }
57 
58  foreach ($response->getHeaders() as $name => $values) {
59  if (in_array(strtolower($name), self::MULTI_LINE_HEADERS, true)) {
60  foreach ($values as $value) {
61  header($name . ': ' . $value, false);
62  }
63  } else {
64  header($name . ': ' . implode(', ', $values));
65  }
66  }
67  }
68  $body = $response->getBody();
69  if ($body instanceof ‪SelfEmittableStreamInterface) {
70  // Optimization for streams that use php functions like readfile() as fastpath for serving files.
71  $body->emit();
72  } else {
73  echo $body->__toString();
74  }
75  }
76 
77  public function ‪handle(ServerRequestInterface $request): ResponseInterface
78  {
79  try {
80  $response = $this->requestHandler->handle($request);
81  } catch (‪ImmediateResponseException $exception) {
82  $response = $exception->‪getResponse();
83  }
84  return $response;
85  }
86 
90  final public function ‪run()
91  {
92  $response = $this->‪handle(‪ServerRequestFactory::fromGlobals());
93  $this->‪sendResponse($response);
94  }
95 }
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:26
‪TYPO3\CMS\Core\Http\AbstractApplication\handle
‪handle(ServerRequestInterface $request)
Definition: AbstractApplication.php:76
‪TYPO3\CMS\Core\Http\AbstractApplication\sendResponse
‪sendResponse(ResponseInterface $response)
Definition: AbstractApplication.php:41
‪TYPO3\CMS\Core\Http\AbstractApplication\MULTI_LINE_HEADERS
‪const MULTI_LINE_HEADERS
Definition: AbstractApplication.php:30
‪TYPO3\CMS\Core\Http\AbstractApplication
Definition: AbstractApplication.php:29
‪TYPO3\CMS\Core\Core\ApplicationInterface
Definition: ApplicationInterface.php:27
‪TYPO3\CMS\Core\Http\ImmediateResponseException\getResponse
‪Response getResponse()
Definition: ImmediateResponseException.php:49
‪TYPO3\CMS\Core\Http\AbstractApplication\run
‪run()
Definition: AbstractApplication.php:89
‪TYPO3\CMS\Core\Http\SelfEmittableStreamInterface
Definition: SelfEmittableStreamInterface.php:28
‪TYPO3\CMS\Core\Http\AbstractApplication\$requestHandler
‪RequestHandlerInterface null $requestHandler
Definition: AbstractApplication.php:36
‪TYPO3\CMS\Core\Http\ImmediateResponseException
Definition: ImmediateResponseException.php:35
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18