9d84a20d58405e346b8af4bd7f3918c167527f5d
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Core / Bootstrap.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Core;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Creates a request an dispatches it to the controller which was specified
32 * by TS Setup, flexForm and returns the content to the v4 framework.
33 *
34 * This class is the main entry point for extbase extensions.
35 */
36 class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface {
37
38 /**
39 * Back reference to the parent content object
40 * This has to be public as it is set directly from TYPO3
41 *
42 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
43 */
44 public $cObj;
45
46 /**
47 * The application context
48 *
49 * @var string
50 */
51 protected $context;
52
53 /**
54 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
55 */
56 protected $configurationManager;
57
58 /**
59 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
60 */
61 protected $objectManager;
62
63 /**
64 * @var \TYPO3\CMS\Core\Cache\CacheManager
65 */
66 protected $cacheManager;
67
68 /**
69 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
70 */
71 protected $reflectionService;
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
75 */
76 protected $persistenceManager;
77
78 /**
79 * Explicitly initializes all necessary Extbase objects by invoking the various initialize* methods.
80 *
81 * Usually this method is only called from unit tests or other applications which need a more fine grained control over
82 * the initialization and request handling process. Most other applications just call the run() method.
83 *
84 * @param array $configuration The TS configuration array
85 * @throws \RuntimeException
86 * @return void
87 * @see run()
88 * @api
89 */
90 public function initialize($configuration) {
91 if (!defined('TYPO3_cliMode') || TYPO3_cliMode !== TRUE) {
92 if (!isset($configuration['extensionName']) || strlen($configuration['extensionName']) === 0) {
93 throw new \RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
94 }
95 if (!isset($configuration['pluginName']) || strlen($configuration['pluginName']) === 0) {
96 throw new \RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
97 }
98 }
99 $this->initializeObjectManager();
100 $this->initializeConfiguration($configuration);
101 $this->configureObjectManager();
102 $this->initializeCache();
103 $this->initializeReflection();
104 $this->initializePersistence();
105 }
106
107 /**
108 * Initializes the Object framework.
109 *
110 * @return void
111 * @see initialize()
112 */
113 protected function initializeObjectManager() {
114 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
115 }
116
117 /**
118 * Initializes the Object framework.
119 *
120 * @param array $configuration
121 * @return void
122 * @see initialize()
123 */
124 public function initializeConfiguration($configuration) {
125 $this->configurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
126 $contentObject = isset($this->cObj) ? $this->cObj : \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
127 $this->configurationManager->setContentObject($contentObject);
128 $this->configurationManager->setConfiguration($configuration);
129 }
130
131 /**
132 * Configures the object manager object configuration from
133 * config.tx_extbase.objects
134 *
135 * @return void
136 * @see initialize()
137 */
138 public function configureObjectManager() {
139 $typoScriptSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
140 if (!is_array($typoScriptSetup['config.']['tx_extbase.']['objects.'])) {
141 return;
142 }
143 $objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\Container\\Container');
144 foreach ($typoScriptSetup['config.']['tx_extbase.']['objects.'] as $classNameWithDot => $classConfiguration) {
145 if (isset($classConfiguration['className'])) {
146 $originalClassName = rtrim($classNameWithDot, '.');
147 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
148 }
149 }
150 }
151
152 /**
153 * Initializes the cache framework
154 *
155 * @return void
156 * @see initialize()
157 */
158 protected function initializeCache() {
159 $this->cacheManager = $GLOBALS['typo3CacheManager'];
160 }
161
162 /**
163 * Initializes the Reflection Service
164 *
165 * @return void
166 * @see initialize()
167 */
168 protected function initializeReflection() {
169 $this->reflectionService = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService');
170 $this->reflectionService->setDataCache($this->cacheManager->getCache('extbase_reflection'));
171 if (!$this->reflectionService->isInitialized()) {
172 $this->reflectionService->initialize();
173 }
174 }
175
176 /**
177 * Initializes the persistence framework
178 *
179 * @return void
180 * @see initialize()
181 */
182 public function initializePersistence() {
183 $this->persistenceManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
184 }
185
186 /**
187 * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
188 * If the Framework is not initialized yet, it will be initialized.
189 *
190 * @param string $content The content. Not used
191 * @param array $configuration The TS configuration array
192 * @return string $content The processed content
193 * @api
194 */
195 public function run($content, $configuration) {
196 $this->initialize($configuration);
197 // CLI
198 if (defined('TYPO3_cliMode') && TYPO3_cliMode === TRUE) {
199 $content = $this->handleCommandLineRequest();
200 } else {
201 $content = $this->handleWebRequest();
202 }
203 return $content;
204 }
205
206 /**
207 * @return string
208 */
209 protected function handleCommandLineRequest() {
210 $commandLine = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
211 $request = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\RequestBuilder')->build(array_slice($commandLine, 1));
212 $response = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
213 $extensionName = $request->getControllerExtensionName();
214 $this->configurationManager->setConfiguration(array('extensionName' => $extensionName));
215 $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\Dispatcher')->dispatch($request, $response);
216 $content = $response->getContent();
217 $this->resetSingletons();
218 return $content;
219 }
220
221 /**
222 * @return string
223 */
224 protected function handleWebRequest() {
225 $requestHandlerResolver = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Mvc\\RequestHandlerResolver');
226 $requestHandler = $requestHandlerResolver->resolveRequestHandler();
227 $response = $requestHandler->handleRequest();
228 // If response is NULL after handling the request we need to stop
229 // This happens for instance, when a USER object was converted to a USER_INT
230 // @see TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler::handleRequest()
231 if ($response === NULL) {
232 $this->reflectionService->shutdown();
233 return '';
234 }
235 if (count($response->getAdditionalHeaderData()) > 0) {
236 $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
237 }
238 $response->sendHeaders();
239 $content = $response->getContent();
240 $this->resetSingletons();
241 return $content;
242 }
243
244 /**
245 * Resets global singletons for the next plugin
246 *
247 * @return void
248 */
249 protected function resetSingletons() {
250 $this->persistenceManager->persistAll();
251 $this->reflectionService->shutdown();
252 }
253
254 /**
255 * This method forwards the call to run(). This method is invoked by the mod.php
256 * function of TYPO3.
257 *
258 * @param string $moduleSignature
259 * @throws \RuntimeException
260 * @return boolean TRUE, if the request request could be dispatched
261 * @see run()
262 */
263 public function callModule($moduleSignature) {
264 if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
265 return FALSE;
266 }
267 $moduleConfiguration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
268 // Check permissions and exit if the user has no permission for entry
269 $GLOBALS['BE_USER']->modAccess($moduleConfiguration, TRUE);
270 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id')) {
271 // Check page access
272 $permClause = $GLOBALS['BE_USER']->getPagePermsClause(TRUE);
273 $access = is_array(\TYPO3\CMS\Backend\Utility\BackendUtility::readPageAccess((integer) \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id'), $permClause));
274 if (!$access) {
275 throw new \RuntimeException('You don\'t have access to this page', 1289917924);
276 }
277 }
278 // BACK_PATH is the path from the typo3/ directory from within the
279 // directory containing the controller file. We are using mod.php dispatcher
280 // and thus we are already within typo3/ because we call typo3/mod.php
281 $GLOBALS['BACK_PATH'] = '';
282 $configuration = array(
283 'extensionName' => $moduleConfiguration['extensionName'],
284 'pluginName' => $moduleSignature
285 );
286 if (isset($moduleConfiguration['vendorName'])) {
287 $configuration['vendorName'] = $moduleConfiguration['vendorName'];
288 }
289 $content = $this->run('', $configuration);
290 print $content;
291 return TRUE;
292 }
293 }
294
295 ?>