[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Http / Application.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Http;
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;
21 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
22 use TYPO3\CMS\Core\Context\Context;
23 use TYPO3\CMS\Core\Context\DateTimeAspect;
24 use TYPO3\CMS\Core\Context\UserAspect;
25 use TYPO3\CMS\Core\Context\VisibilityAspect;
26 use TYPO3\CMS\Core\Context\WorkspaceAspect;
27 use TYPO3\CMS\Core\Core\Environment;
28 use TYPO3\CMS\Core\Http\AbstractApplication;
29 use TYPO3\CMS\Core\Http\RedirectResponse;
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Entry point for the TYPO3 Frontend
34 */
35 class Application extends AbstractApplication
36 {
37 /**
38 * @var ConfigurationManager
39 */
40 protected $configurationManager;
41
42 /**
43 * @param RequestHandlerInterface $requestHandler
44 * @param ConfigurationManager $configurationManager
45 */
46 public function __construct(RequestHandlerInterface $requestHandler, ConfigurationManager $configurationManager)
47 {
48 $this->requestHandler = $requestHandler;
49 $this->configurationManager = $configurationManager;
50 }
51
52 /**
53 * @param ServerRequestInterface $request
54 * @return ResponseInterface
55 */
56 protected function handle(ServerRequestInterface $request): ResponseInterface
57 {
58 if (!$this->checkIfEssentialConfigurationExists()) {
59 return $this->installToolRedirect();
60 }
61 $this->initializeContext();
62 return parent::handle($request);
63 }
64
65 /**
66 * Check if LocalConfiguration.php and PackageStates.php exist
67 *
68 * @return bool TRUE when the essential configuration is available, otherwise FALSE
69 */
70 protected function checkIfEssentialConfigurationExists(): bool
71 {
72 return file_exists($this->configurationManager->getLocalConfigurationFileLocation())
73 && file_exists(Environment::getLegacyConfigPath() . '/PackageStates.php');
74 }
75
76 /**
77 * Create a PSR-7 Response that redirects to the install tool
78 *
79 * @return ResponseInterface
80 */
81 protected function installToolRedirect(): ResponseInterface
82 {
83 $path = TYPO3_mainDir . 'install.php';
84 return new RedirectResponse($path, 302);
85 }
86
87 /**
88 * Initializes the Context used for accessing data and finding out the current state of the application
89 * Will be moved to a DI-like concept once introduced, for now, this is a singleton
90 */
91 protected function initializeContext()
92 {
93 GeneralUtility::makeInstance(Context::class, [
94 'date' => new DateTimeAspect(new \DateTimeImmutable('@' . $GLOBALS['EXEC_TIME'])),
95 'visibility' => new VisibilityAspect(),
96 'workspace' => new WorkspaceAspect(0),
97 'backend.user' => new UserAspect(null),
98 'frontend.user' => new UserAspect(null, [0, -1]),
99 ]);
100 }
101 }