[FEATURE] Add support for PSR-15 HTTP middlewares
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / Application.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Http;
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
17 use TYPO3\CMS\Core\Core\ApplicationInterface;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19
20 /**
21 * Entry point for the TYPO3 Frontend
22 */
23 class Application implements ApplicationInterface
24 {
25 /**
26 * @var Bootstrap
27 */
28 protected $bootstrap;
29
30 /**
31 * Number of subdirectories where the entry script is located, relative to PATH_site
32 * Usually this is equal to PATH_site = 0
33 * @var int
34 */
35 protected $entryPointLevel = 0;
36
37 /**
38 * All available request handlers that can deal with a Frontend Request
39 * @var array
40 */
41 protected $availableRequestHandlers = [
42 \TYPO3\CMS\Frontend\Http\RequestHandler::class,
43 \TYPO3\CMS\Frontend\Http\EidRequestHandler::class
44 ];
45
46 /**
47 * Constructor setting up legacy constant and register available Request Handlers
48 *
49 * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
50 */
51 public function __construct($classLoader)
52 {
53 $this->defineLegacyConstants();
54
55 $this->bootstrap = Bootstrap::getInstance()
56 ->initializeClassLoader($classLoader)
57 ->setRequestType(TYPO3_REQUESTTYPE_FE)
58 ->baseSetup($this->entryPointLevel);
59
60 // Redirect to install tool if base configuration is not found
61 if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
62 $this->bootstrap->redirectToInstallTool($this->entryPointLevel);
63 }
64
65 foreach ($this->availableRequestHandlers as $requestHandler) {
66 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
67 }
68
69 $this->bootstrap->configure();
70 }
71
72 /**
73 * Starting point
74 *
75 * @param callable $execute
76 */
77 public function run(callable $execute = null)
78 {
79 $this->bootstrap->handleRequest(\TYPO3\CMS\Core\Http\ServerRequestFactory::fromGlobals(), 'frontend');
80
81 if ($execute !== null) {
82 call_user_func($execute);
83 }
84
85 $this->bootstrap->shutdown();
86 }
87
88 /**
89 * Define constants and variables
90 */
91 protected function defineLegacyConstants()
92 {
93 define('TYPO3_MODE', 'FE');
94 }
95 }