[FEATURE] Introduce "Application" classes for all entry points
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / Application.php
1 <?php
2 namespace TYPO3\CMS\Backend\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 use TYPO3\CMS\Core\Core\ApplicationInterface;
17 use TYPO3\CMS\Core\Core\Bootstrap;
18
19
20 /**
21 * Entry point for the TYPO3 Backend (HTTP requests)
22 */
23 class Application implements ApplicationInterface {
24
25 /**
26 * @var Bootstrap
27 */
28 protected $bootstrap;
29
30 /**
31 * @var string
32 */
33 protected $entryPointPath = 'typo3/';
34
35 /**
36 * All available request handlers that can handle backend requests (non-CLI)
37 * @var array
38 */
39 protected $availableRequestHandlers = array(
40 \TYPO3\CMS\Backend\Http\RequestHandler::class,
41 \TYPO3\CMS\Backend\Http\BackendModuleRequestHandler::class,
42 \TYPO3\CMS\Backend\Http\AjaxRequestHandler::class
43 );
44
45 /**
46 * Constructor setting up legacy constant and register available Request Handlers
47 *
48 * @param \Composer\Autoload\ClassLoader|\Helhum\ClassAliasLoader\Composer\ClassAliasLoader $classLoader an instance of the class loader
49 */
50 public function __construct($classLoader) {
51 $this->defineLegacyConstants();
52
53 $this->bootstrap = Bootstrap::getInstance()
54 ->initializeClassLoader($classLoader)
55 ->baseSetup($this->entryPointPath);
56
57 // can be done here after the base setup is done
58 $this->defineAdditionalEntryPointRelatedConstants();
59
60 // Redirect to install tool if base configuration is not found
61 if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
62 $this->bootstrap->redirectToInstallTool($this->entryPointPath);
63 }
64
65 foreach ($this->availableRequestHandlers as $requestHandler) {
66 $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
67 }
68 }
69
70 /**
71 * Set up the application and shut it down afterwards
72 *
73 * @param callable $execute
74 * @return void
75 */
76 public function run(callable $execute = NULL) {
77 $this->bootstrap->run();
78
79 if ($execute !== NULL) {
80 if ($execute instanceof \Closure) {
81 $execute->bindTo($this);
82 }
83 call_user_func($execute);
84 }
85
86 $this->bootstrap->shutdown();
87 }
88
89 /**
90 * Define constants and variables
91 */
92 protected function defineLegacyConstants() {
93 define('TYPO3_MODE', 'BE');
94 }
95
96 /**
97 * Define values that are based on the current script
98 */
99 protected function defineAdditionalEntryPointRelatedConstants() {
100 $currentScript = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('SCRIPT_NAME');
101
102 // activate "AJAX" handler when called via ajax.php
103 if (substr($currentScript, -15) === '/typo3/ajax.php') {
104 $GLOBALS['TYPO3_AJAX'] = TRUE;
105 }
106 // allow backend login to work
107 if (substr($currentScript, -16) === '/typo3/index.php') {
108 define('TYPO3_PROCEED_IF_NO_USER', 1);
109 }
110 }
111 }