‪TYPO3CMS  9.5
Bootstrap.php
Go to the documentation of this file.
1 <?php
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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
21 use ‪TYPO3\CMS\Extbase\Mvc\Cli\Response as CliResponse;
22 use ‪TYPO3\CMS\Extbase\Mvc\Web\Response as WebResponse;
23 
31 {
38  public ‪$cObj;
39 
43  protected ‪$configurationManager;
44 
48  protected ‪$objectManager;
49 
53  protected ‪$persistenceManager;
54 
65  public function ‪initialize($configuration)
66  {
67  if (!‪Environment::isCli()) {
68  if (!isset($configuration['vendorName']) || $configuration['vendorName'] === '') {
69  throw new \RuntimeException('Invalid configuration: "vendorName" is not set', 1526629315);
70  }
71  if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
72  throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
73  }
74  if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
75  throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
76  }
77  }
79  $this->‪initializeConfiguration($configuration);
80  $this->‪configureObjectManager(true);
81  $this->‪initializePersistence();
82  }
83 
89  protected function ‪initializeObjectManager()
90  {
91  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\‪Extbase\Object\ObjectManager::class);
92  }
93 
101  public function ‪initializeConfiguration($configuration)
102  {
103  $this->configurationManager = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Configuration\ConfigurationManagerInterface::class);
105  $contentObject = $this->cObj ?? \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
106  $this->configurationManager->setContentObject($contentObject);
107  $this->configurationManager->setConfiguration($configuration);
108  }
109 
118  public function ‪configureObjectManager($isInternalCall = false)
119  {
120  if (!$isInternalCall) {
121  trigger_error(self::class . '::configureObjectManager() will be removed in TYPO3 v10.0.', E_USER_DEPRECATED);
122  }
123  $frameworkSetup = $this->configurationManager->getConfiguration(\‪TYPO3\CMS\‪Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
124  if (!isset($frameworkSetup['objects']) || !is_array($frameworkSetup['objects'])) {
125  return;
126  }
127  trigger_error('Overriding object implementations via TypoScript settings config.tx_extbase.objects and plugin.tx_%plugin%.objects will be removed in TYPO3 v10.0.', E_USER_DEPRECATED);
128  $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\‪TYPO3\CMS\‪Extbase\Object\Container\Container::class);
129  foreach ($frameworkSetup['objects'] as $classNameWithDot => $classConfiguration) {
130  if (isset($classConfiguration['className'])) {
131  $originalClassName = rtrim($classNameWithDot, '.');
132  $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
133  }
134  }
135  }
136 
143  public function ‪initializePersistence()
144  {
145  $this->persistenceManager = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\PersistenceManager::class);
146  }
147 
156  public function ‪run($content, $configuration)
157  {
158  $this->‪initialize($configuration);
159  return $this->‪handleRequest();
160  }
161 
166  protected function ‪handleRequest()
167  {
169  $requestHandlerResolver = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\RequestHandlerResolver::class);
170  $requestHandler = $requestHandlerResolver->resolveRequestHandler();
171 
172  $response = $requestHandler->handleRequest();
173  // If response is NULL after handling the request we need to stop
174  // This happens for instance, when a USER object was converted to a USER_INT
175  // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
176  if ($response === null) {
177  $content = '';
178  } else {
179  $content = $response->shutdown();
180  $this->‪resetSingletons();
181  $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
182  if ($response instanceof CliResponse && $response->getExitCode()) {
183  throw new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('The request has been terminated as the response defined an exit code.', $response->getExitCode());
184  }
185  }
186 
187  return $content;
188  }
189 
197  public function ‪handleBackendRequest(ServerRequestInterface $request): ResponseInterface
198  {
199  // build the configuration from the Server request / route
201  $route = $request->getAttribute('route');
202  $moduleConfiguration = $route->getOption('moduleConfiguration');
203  $configuration = [
204  'extensionName' => $moduleConfiguration['extensionName'],
205  'pluginName' => $route->getOption('moduleName')
206  ];
207  if (isset($moduleConfiguration['vendorName'])) {
208  $configuration['vendorName'] = $moduleConfiguration['vendorName'];
209  }
210 
211  $this->‪initialize($configuration);
212 
214  $requestHandlerResolver = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Mvc\RequestHandlerResolver::class);
215  $requestHandler = $requestHandlerResolver->resolveRequestHandler();
217  $extbaseResponse = $requestHandler->handleRequest();
218 
219  // Convert to PSR-7 response and hand it back to TYPO3 Core
220  $response = $this->‪convertExtbaseResponseToPsr7Response($extbaseResponse);
221  $this->‪resetSingletons();
222  $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
223  return $response;
224  }
225 
232  protected function ‪convertExtbaseResponseToPsr7Response(WebResponse $extbaseResponse): ResponseInterface
233  {
234  $response = new \TYPO3\CMS\Core\Http\Response(
235  'php://temp',
236  $extbaseResponse->getStatusCode(),
237  $extbaseResponse->getUnpreparedHeaders()
238  );
239  $content = $extbaseResponse->getContent();
240  if ($content !== null) {
241  $response->getBody()->write($content);
242  }
243  return $response;
244  }
245 
249  protected function ‪resetSingletons()
250  {
251  $this->persistenceManager->persistAll();
252  }
253 }
‪TYPO3\CMS\Extbase\Core\Bootstrap\convertExtbaseResponseToPsr7Response
‪ResponseInterface convertExtbaseResponseToPsr7Response(WebResponse $extbaseResponse)
Definition: Bootstrap.php:228
‪TYPO3\CMS\Extbase\Core\Bootstrap\initialize
‪initialize($configuration)
Definition: Bootstrap.php:61
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Core\Bootstrap\resetSingletons
‪resetSingletons()
Definition: Bootstrap.php:245
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializeObjectManager
‪initializeObjectManager()
Definition: Bootstrap.php:85
‪TYPO3\CMS\Extbase\Core\Bootstrap\run
‪string run($content, $configuration)
Definition: Bootstrap.php:152
‪TYPO3
‪TYPO3\CMS\Extbase\Core\Bootstrap\handleRequest
‪string handleRequest()
Definition: Bootstrap.php:162
‪TYPO3\CMS\Extbase\Core\Bootstrap\configureObjectManager
‪configureObjectManager($isInternalCall=false)
Definition: Bootstrap.php:114
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:23
‪TYPO3\CMS\Extbase\Core\Bootstrap\$cObj
‪TYPO3 CMS Frontend ContentObject ContentObjectRenderer $cObj
Definition: Bootstrap.php:37
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:25
‪TYPO3\CMS\Extbase\Core\Bootstrap\$persistenceManager
‪TYPO3 CMS Extbase Persistence Generic PersistenceManager $persistenceManager
Definition: Bootstrap.php:49
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializePersistence
‪initializePersistence()
Definition: Bootstrap.php:139
‪TYPO3\CMS\Extbase\Core\BootstrapInterface
Definition: BootstrapInterface.php:22
‪TYPO3\CMS\Extbase\Core\Bootstrap\$configurationManager
‪TYPO3 CMS Extbase Configuration ConfigurationManager $configurationManager
Definition: Bootstrap.php:41
‪TYPO3\CMS\Extbase\Mvc\Cli\Response
Definition: Response.php:23
‪TYPO3\CMS\Extbase\Core\Bootstrap
Definition: Bootstrap.php:31
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Extbase\Core
Definition: Bootstrap.php:2
‪TYPO3\CMS\Extbase\Core\Bootstrap\$objectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface $objectManager
Definition: Bootstrap.php:45
‪TYPO3\CMS\Extbase\Core\Bootstrap\initializeConfiguration
‪initializeConfiguration($configuration)
Definition: Bootstrap.php:97
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:127
‪TYPO3\CMS\Extbase\Core\Bootstrap\handleBackendRequest
‪ResponseInterface handleBackendRequest(ServerRequestInterface $request)
Definition: Bootstrap.php:193