[TASK] Streamline moduleName handling
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / Application.php
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
18 namespace TYPO3\CMS\Backend\Http;
19
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
24 use TYPO3\CMS\Core\Context\Context;
25 use TYPO3\CMS\Core\Context\DateTimeAspect;
26 use TYPO3\CMS\Core\Context\VisibilityAspect;
27 use TYPO3\CMS\Core\Core\Environment;
28 use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
29 use TYPO3\CMS\Core\Http\AbstractApplication;
30 use TYPO3\CMS\Core\Http\RedirectResponse;
31
32 /**
33 * Entry point for the TYPO3 Backend (HTTP requests)
34 */
35 class Application extends AbstractApplication
36 {
37 /**
38 * @var ConfigurationManager
39 */
40 protected $configurationManager;
41
42 /**
43 * @var Context
44 */
45 protected $context;
46
47 public function __construct(
48 RequestHandlerInterface $requestHandler,
49 ConfigurationManager $configurationManager,
50 Context $context
51 ) {
52 $this->requestHandler = $requestHandler;
53 $this->configurationManager = $configurationManager;
54 $this->context = $context;
55 }
56
57 public function handle(ServerRequestInterface $request): ResponseInterface
58 {
59 if (!$this->checkIfEssentialConfigurationExists()) {
60 return $this->installToolRedirect();
61 }
62
63 // Add applicationType attribute to request: This is backend and maybe backend ajax.
64 $applicationType = SystemEnvironmentBuilder::REQUESTTYPE_BE;
65 if (strpos($request->getUri()->getPath(), '/typo3/ajax/') !== false || strpos($request->getQueryParams()['route'] ?? '', '/ajax/') === 0) {
66 $applicationType |= SystemEnvironmentBuilder::REQUESTTYPE_AJAX;
67 }
68 $request = $request->withAttribute('applicationType', $applicationType);
69
70 // Set up the initial context
71 $this->initializeContext();
72 return parent::handle($request);
73 }
74
75 /**
76 * Check if LocalConfiguration.php and PackageStates.php exist
77 *
78 * @return bool TRUE when the essential configuration is available, otherwise FALSE
79 */
80 protected function checkIfEssentialConfigurationExists(): bool
81 {
82 return file_exists($this->configurationManager->getLocalConfigurationFileLocation())
83 && file_exists(Environment::getLegacyConfigPath() . '/PackageStates.php');
84 }
85
86 /**
87 * Create a PSR-7 Response that redirects to the install tool
88 *
89 * @return ResponseInterface
90 */
91 protected function installToolRedirect(): ResponseInterface
92 {
93 return new RedirectResponse('./install.php', 302);
94 }
95
96 /**
97 * Initializes the Context used for accessing data and finding out the current state of the application
98 */
99 protected function initializeContext(): void
100 {
101 $this->context->setAspect('date', new DateTimeAspect(new \DateTimeImmutable('@' . $GLOBALS['EXEC_TIME'])));
102 $this->context->setAspect('visibility', new VisibilityAspect(true, true));
103 }
104 }