TYPO3 CMS  TYPO3_8-7
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 
24 {
31  public $cObj;
32 
38  protected $context;
39 
44 
48  protected $objectManager;
49 
53  protected $cacheManager;
54 
58  protected $reflectionService;
59 
64 
76  public function initialize($configuration)
77  {
78  if (!$this->isInCliMode()) {
79  if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
80  throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
81  }
82  if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
83  throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
84  }
85  }
86  $this->initializeObjectManager();
87  $this->initializeConfiguration($configuration);
88  $this->configureObjectManager();
89  $this->initializeCache();
90  $this->initializeReflection();
91  $this->initializePersistence();
92  }
93 
99  protected function initializeObjectManager()
100  {
101  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
102  }
103 
110  public function initializeConfiguration($configuration)
111  {
112  $this->configurationManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
114  $contentObject = isset($this->cObj) ? $this->cObj : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
115  $this->configurationManager->setContentObject($contentObject);
116  $this->configurationManager->setConfiguration($configuration);
117  }
118 
125  public function configureObjectManager()
126  {
127  $frameworkSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
128  if (!is_array($frameworkSetup['objects'])) {
129  return;
130  }
131  $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
132  foreach ($frameworkSetup['objects'] as $classNameWithDot => $classConfiguration) {
133  if (isset($classConfiguration['className'])) {
134  $originalClassName = rtrim($classNameWithDot, '.');
135  $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
136  }
137  }
138  }
139 
145  protected function initializeCache()
146  {
147  $this->cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
148  }
149 
155  protected function initializeReflection()
156  {
157  $this->reflectionService = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
158  $this->reflectionService->setDataCache($this->cacheManager->getCache('extbase_reflection'));
159  if (!$this->reflectionService->isInitialized()) {
160  $this->reflectionService->initialize();
161  }
162  }
163 
169  public function initializePersistence()
170  {
171  $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
172  }
173 
183  public function run($content, $configuration)
184  {
185  $this->initialize($configuration);
186  return $this->handleRequest();
187  }
188 
193  protected function handleRequest()
194  {
196  $requestHandlerResolver = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver::class);
197  $requestHandler = $requestHandlerResolver->resolveRequestHandler();
198 
199  $response = $requestHandler->handleRequest();
200  // If response is NULL after handling the request we need to stop
201  // This happens for instance, when a USER object was converted to a USER_INT
202  // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
203  if ($response === null) {
204  $this->reflectionService->shutdown();
205  $content = '';
206  } else {
207  $content = $response->shutdown();
208  $this->resetSingletons();
209  $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
210  if ($this->isInCliMode() && $response->getExitCode()) {
211  throw new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('The request has been terminated as the response defined an exit code.', $response->getExitCode());
212  }
213  }
214 
215  return $content;
216  }
217 
221  protected function resetSingletons()
222  {
223  $this->persistenceManager->persistAll();
224  $this->reflectionService->shutdown();
225  }
226 
230  protected function isInCliMode()
231  {
232  return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI;
233  }
234 }
run($content, $configuration)
Definition: Bootstrap.php:183
static makeInstance($className,... $constructorArguments)