TYPO3 CMS  TYPO3_7-6
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 
77  public function initialize($configuration)
78  {
79  if (!$this->isInCliMode()) {
80  if (!isset($configuration['extensionName']) || $configuration['extensionName'] === '') {
81  throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
82  }
83  if (!isset($configuration['pluginName']) || $configuration['pluginName'] === '') {
84  throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
85  }
86  }
87  $this->initializeObjectManager();
88  $this->initializeConfiguration($configuration);
89  $this->configureObjectManager();
90  $this->initializeCache();
91  $this->initializeReflection();
92  $this->initializePersistence();
93  }
94 
101  protected function initializeObjectManager()
102  {
103  $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
104  }
105 
113  public function initializeConfiguration($configuration)
114  {
115  $this->configurationManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
117  $contentObject = isset($this->cObj) ? $this->cObj : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
118  $this->configurationManager->setContentObject($contentObject);
119  $this->configurationManager->setConfiguration($configuration);
120  }
121 
129  public function configureObjectManager()
130  {
131  $frameworkSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
132  if (!is_array($frameworkSetup['objects'])) {
133  return;
134  }
135  $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
136  foreach ($frameworkSetup['objects'] as $classNameWithDot => $classConfiguration) {
137  if (isset($classConfiguration['className'])) {
138  $originalClassName = rtrim($classNameWithDot, '.');
139  $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
140  }
141  }
142  }
143 
150  protected function initializeCache()
151  {
152  $this->cacheManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
153  }
154 
161  protected function initializeReflection()
162  {
163  $this->reflectionService = $this->objectManager->get(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class);
164  $this->reflectionService->setDataCache($this->cacheManager->getCache('extbase_reflection'));
165  if (!$this->reflectionService->isInitialized()) {
166  $this->reflectionService->initialize();
167  }
168  }
169 
176  public function initializePersistence()
177  {
178  $this->persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
179  }
180 
190  public function run($content, $configuration)
191  {
192  $this->initialize($configuration);
193  return $this->handleRequest();
194  }
195 
200  protected function handleRequest()
201  {
203  $requestHandlerResolver = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\RequestHandlerResolver::class);
204  $requestHandler = $requestHandlerResolver->resolveRequestHandler();
205 
206  $response = $requestHandler->handleRequest();
207  // If response is NULL after handling the request we need to stop
208  // This happens for instance, when a USER object was converted to a USER_INT
209  // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
210  if ($response === null) {
211  $this->reflectionService->shutdown();
212  $content = '';
213  } else {
214  $content = $response->shutdown();
215  $this->resetSingletons();
216  $this->objectManager->get(\TYPO3\CMS\Extbase\Service\CacheService::class)->clearCachesOfRegisteredPageIds();
217  if ($this->isInCliMode() && $response->getExitCode()) {
218  throw new \TYPO3\CMS\Extbase\Mvc\Exception\CommandException('The request has been terminated as the response defined an exit code.', $response->getExitCode());
219  }
220  }
221 
222  return $content;
223  }
224 
230  protected function resetSingletons()
231  {
232  $this->persistenceManager->persistAll();
233  $this->reflectionService->shutdown();
234  }
235 
239  protected function isInCliMode()
240  {
241  return defined('TYPO3_cliMode') && TYPO3_cliMode;
242  }
243 }
run($content, $configuration)
Definition: Bootstrap.php:190