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