‪TYPO3CMS  10.4
Bootstrap.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\Container\ContainerInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
30 use ‪TYPO3\CMS\Extbase\Mvc\Response as ExtbaseResponse;
35 
43 {
47  public static ‪$persistenceClasses = [];
48 
55  public ‪$cObj;
56 
60  protected ‪$container;
61 
66 
70  protected ‪$persistenceManager;
71 
76 
80  protected ‪$cacheService;
81 
82  public function ‪__construct(
83  ContainerInterface ‪$container,
88  ) {
89  $this->container = ‪$container;
90  $this->configurationManager = ‪$configurationManager;
91  $this->persistenceManager = ‪$persistenceManager;
92  $this->requestHandlerResolver = ‪$requestHandlerResolver;
93  $this->cacheService = ‪$cacheService;
94  }
95 
106  public function ‪initialize(array $configuration): void
107  {
108  if (!‪Environment::isCli()) {
109  if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
110  throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
111  }
112  if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
113  throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
114  }
115  }
116  $this->‪initializeConfiguration($configuration);
119  }
120 
128  public function ‪initializeConfiguration(array $configuration): void
129  {
131  $contentObject = $this->cObj ?? $this->container->get(ContentObjectRenderer::class);
132  $this->configurationManager->setContentObject($contentObject);
133  $this->configurationManager->setConfiguration($configuration);
134  // todo: Shouldn't the configuration manager object – which is a singleton – be stateless?
135  // todo: At this point we give the configuration manager a state, while we could directly pass the
136  // todo: configuration (i.e. controllerName, actionName and such), directly to the request
137  // todo: handler, which then creates stateful request objects.
138  // todo: Once this has changed, \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::loadDefaultValues does not need
139  // todo: to fetch this configuration from the configuration manager.
140  }
141 
145  private function ‪initializePersistenceClassesConfiguration(): void
146  {
147  $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
148  GeneralUtility::makeInstance(ClassesConfigurationFactory::class, $cacheManager)
149  ->createClassesConfiguration();
150  }
151 
155  private function ‪initializeRequestHandlersConfiguration(): void
156  {
157  $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
158  GeneralUtility::makeInstance(RequestHandlersConfigurationFactory::class, $cacheManager)
159  ->createRequestHandlersConfiguration();
160  }
161 
170  public function ‪run(string $content, array $configuration): string
171  {
172  $this->‪initialize($configuration);
173  return $this->‪handleRequest();
174  }
175 
179  protected function ‪handleRequest(): string
180  {
181  $requestHandler = $this->requestHandlerResolver->resolveRequestHandler();
182 
183  $response = $requestHandler->handleRequest();
184  // If response is NULL after handling the request we need to stop
185  // This happens for instance, when a USER object was converted to a USER_INT
186  // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
187  if ($response === null) {
188  $content = '';
189  } else {
190  /*
191  * Explicitly cast $content to string here as \TYPO3\CMS\Extbase\Mvc\ResponseInterface::shutdown does not
192  * use strict types yet and response objects possibly return other types than string.
193  *
194  * todo: remove the type cast when \TYPO3\CMS\Extbase\Mvc\ResponseInterface declares strict return types.
195  */
196  $content = (string)$response->shutdown();
197  $this->‪resetSingletons();
198  $this->cacheService->clearCachesOfRegisteredPageIds();
199  }
200 
201  return $content;
202  }
203 
211  public function ‪handleBackendRequest(ServerRequestInterface $request): ResponseInterface
212  {
213  // build the configuration from the Server request / route
215  $route = $request->getAttribute('route');
216  $moduleConfiguration = $route->getOption('moduleConfiguration');
217  $configuration = [
218  'extensionName' => $moduleConfiguration['extensionName'],
219  'pluginName' => $route->getOption('moduleName')
220  ];
221 
222  $this->‪initialize($configuration);
223 
224  $requestHandler = $this->requestHandlerResolver->resolveRequestHandler();
226  $extbaseResponse = $requestHandler->handleRequest();
227 
228  // Convert to PSR-7 response and hand it back to TYPO3 Core
229  $response = $this->‪convertExtbaseResponseToPsr7Response($extbaseResponse);
230  $this->‪resetSingletons();
231  $this->cacheService->clearCachesOfRegisteredPageIds();
232  return $response;
233  }
234 
241  protected function ‪convertExtbaseResponseToPsr7Response(ExtbaseResponse $extbaseResponse): ResponseInterface
242  {
243  $response = new \TYPO3\CMS\Core\Http\Response(
244  'php://temp',
245  $extbaseResponse->getStatusCode(),
246  $extbaseResponse->getUnpreparedHeaders()
247  );
248  $content = $extbaseResponse->getContent();
249  if ($content !== null) {
250  $response->getBody()->write($content);
251  }
252  return $response;
253  }
254 
258  protected function ‪resetSingletons(): void
259  {
260  $this->persistenceManager->persistAll();
261  }
262 }
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Core\Bootstrap\resetSingletons
‪resetSingletons()
Definition: Bootstrap.php:251
‪TYPO3\CMS\Extbase\Core\Bootstrap\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: Bootstrap.php:65
‪TYPO3\CMS\Extbase\Configuration\RequestHandlersConfigurationFactory
Definition: RequestHandlersConfigurationFactory.php:35
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializeConfiguration
‪initializeConfiguration(array $configuration)
Definition: Bootstrap.php:121
‪TYPO3\CMS\Extbase\Core\Bootstrap\handleRequest
‪string handleRequest()
Definition: Bootstrap.php:172
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
Definition: ConfigurationManagerInterface.php:28
‪TYPO3\CMS\Extbase\Core\Bootstrap\$requestHandlerResolver
‪TYPO3 CMS Extbase Mvc RequestHandlerResolver $requestHandlerResolver
Definition: Bootstrap.php:69
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Extbase\Core\Bootstrap\$cObj
‪TYPO3 CMS Frontend ContentObject ContentObjectRenderer $cObj
Definition: Bootstrap.php:53
‪TYPO3\CMS\Extbase\Mvc\Response
Definition: Response.php:26
‪TYPO3\CMS\Extbase\Core\Bootstrap\__construct
‪__construct(ContainerInterface $container, ConfigurationManagerInterface $configurationManager, PersistenceManagerInterface $persistenceManager, RequestHandlerResolver $requestHandlerResolver, CacheService $cacheService)
Definition: Bootstrap.php:75
‪TYPO3\CMS\Extbase\Core\Bootstrap\initialize
‪initialize(array $configuration)
Definition: Bootstrap.php:99
‪TYPO3\CMS\Extbase\Core\Bootstrap\$configurationManager
‪ConfigurationManagerInterface $configurationManager
Definition: Bootstrap.php:61
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Extbase\Core\BootstrapInterface
Definition: BootstrapInterface.php:25
‪TYPO3\CMS\Extbase\Core\Bootstrap\$cacheService
‪TYPO3 CMS Extbase Service CacheService $cacheService
Definition: Bootstrap.php:73
‪TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver
Definition: RequestHandlerResolver.php:26
‪TYPO3\CMS\Extbase\Core\Bootstrap\run
‪string run(string $content, array $configuration)
Definition: Bootstrap.php:163
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializeRequestHandlersConfiguration
‪initializeRequestHandlersConfiguration()
Definition: Bootstrap.php:148
‪TYPO3\CMS\Extbase\Core\Bootstrap
Definition: Bootstrap.php:43
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Extbase\Service\CacheService
Definition: CacheService.php:28
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Extbase\Core\Bootstrap\$container
‪ContainerInterface $container
Definition: Bootstrap.php:57
‪TYPO3\CMS\Extbase\Core
Definition: Bootstrap.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Core\Bootstrap\$persistenceClasses
‪static array $persistenceClasses
Definition: Bootstrap.php:46
‪TYPO3\CMS\Extbase\Persistence\ClassesConfigurationFactory
Definition: ClassesConfigurationFactory.php:37
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:154
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializePersistenceClassesConfiguration
‪initializePersistenceClassesConfiguration()
Definition: Bootstrap.php:138
‪TYPO3\CMS\Extbase\Core\Bootstrap\handleBackendRequest
‪ResponseInterface handleBackendRequest(ServerRequestInterface $request)
Definition: Bootstrap.php:204
‪TYPO3\CMS\Extbase\Core\Bootstrap\convertExtbaseResponseToPsr7Response
‪ResponseInterface convertExtbaseResponseToPsr7Response(ExtbaseResponse $extbaseResponse)
Definition: Bootstrap.php:234