TYPO3 API  SVNRelease
Bootstrap.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * Creates a request an dispatches it to the controller which was specified
00027  * by TS Setup, Flexform and returns the content to the v4 framework.
00028  *
00029  * This class is the main entry point for extbase extensions.
00030  *
00031  * @package Extbase
00032  * @version $ID:$
00033  */
00034 class Tx_Extbase_Core_Bootstrap {
00035 
00036     /**
00037      * Back reference to the parent content object
00038      * This has to be public as it is set directly from TYPO3
00039      *
00040      * @var tslib_cObj
00041      */
00042     public $cObj;
00043 
00044     /**
00045      * The application context
00046      * @var string
00047      */
00048     protected $context;
00049 
00050     /**
00051      * @var Tx_Extbase_Configuration_ConfigurationManager
00052      */
00053     protected $configurationManager;
00054 
00055     /**
00056      * @var Tx_Extbase_Object_ObjectManagerInterface
00057      */
00058     protected $objectManager;
00059 
00060     /**
00061      * @var t3lib_cache_Manager
00062      */
00063     protected $cacheManager;
00064 
00065     /**
00066      * @var Tx_Extbase_Reflection_Service
00067      */
00068     protected $reflectionService;
00069 
00070     /**
00071      * @var Tx_Extbase_Persistence_Manager
00072      */
00073     protected $persistenceManager;
00074 
00075     /**
00076      * @var boolean
00077      */
00078     protected $isInitialized = FALSE;
00079 
00080     /**
00081      * Explicitly initializes all necessary Extbase objects by invoking the various initialize* methods.
00082      *
00083      * Usually this method is only called from unit tests or other applications which need a more fine grained control over
00084      * the initialization and request handling process. Most other applications just call the run() method.
00085      *
00086      * @param array $configuration The TS configuration array
00087      * @return void
00088      * @see run()
00089      * @api
00090      */
00091     public function initialize($configuration) {
00092         if (!isset($configuration['extensionName']) || strlen($configuration['extensionName']) === 0) {
00093             throw new RuntimeException('Invalid configuration: "extensionName" is not set', 1290623020);
00094         }
00095         if (!isset($configuration['pluginName']) || strlen($configuration['pluginName']) === 0) {
00096             throw new RuntimeException('Invalid configuration: "pluginName" is not set', 1290623027);
00097         }
00098         $this->initializeClassLoader();
00099         $this->initializeObjectManager();
00100         $this->initializeConfiguration($configuration);
00101         $this->configureObjectManager();
00102         $this->initializeCache();
00103         $this->initializeReflection();
00104         $this->initializePersistence();
00105         $this->initializeBackwardsCompatibility();
00106         $this->isInitialized = TRUE;
00107     }
00108 
00109     /**
00110      * Initializes the autoload mechanism of Extbase. This is supplement to the core autoloader.
00111      *
00112      * @return void
00113      * @see initialize()
00114      */
00115     protected function initializeClassLoader() {
00116         if (!class_exists('Tx_Extbase_Utility_ClassLoader', FALSE)) {
00117             require(t3lib_extmgm::extPath('extbase') . 'Classes/Utility/ClassLoader.php');
00118         }
00119 
00120         $classLoader = new Tx_Extbase_Utility_ClassLoader();
00121         spl_autoload_register(array($classLoader, 'loadClass'));
00122     }
00123 
00124     /**
00125      * Initializes the Object framework.
00126      *
00127      * @return void
00128      * @see initialize()
00129      */
00130     protected function initializeObjectManager() {
00131         $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
00132     }
00133 
00134     /**
00135      * Initializes the Object framework.
00136      *
00137      * @return void
00138      * @see initialize()
00139      */
00140     public function initializeConfiguration($configuration) {
00141         $this->configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
00142         $contentObject = isset($this->cObj) ? $this->cObj : t3lib_div::makeInstance('tslib_cObj');
00143         $this->configurationManager->setContentObject($contentObject);
00144         $this->configurationManager->setConfiguration($configuration);
00145     }
00146 
00147     /**
00148      * Configures the object manager object configuration from
00149      * config.tx_extbase.objects
00150      *
00151      * @return void
00152      * @see initialize()
00153      */
00154     public function configureObjectManager() {
00155         $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
00156         if (!is_array($typoScriptSetup['config.']['tx_extbase.']['objects.'])) {
00157             return;
00158         }
00159         $objectContainer = t3lib_div::makeInstance('Tx_Extbase_Object_Container_Container');
00160         foreach ($typoScriptSetup['config.']['tx_extbase.']['objects.'] as $classNameWithDot => $classConfiguration) {
00161             if (isset($classConfiguration['className'])) {
00162                 $originalClassName = rtrim($classNameWithDot, '.');
00163                 $objectContainer->registerImplementation($originalClassName, $classConfiguration['className']);
00164             }
00165         }
00166     }
00167 
00168     /**
00169      * Initializes the cache framework
00170      *
00171      * @return void
00172      * @see initialize()
00173      */
00174     protected function initializeCache() {
00175         t3lib_cache::initializeCachingFramework();
00176         $this->cacheManager = $GLOBALS['typo3CacheManager'];
00177         try {
00178             $this->cacheManager->getCache('cache_extbase_reflection');
00179         } catch (t3lib_cache_exception_NoSuchCache $exception) {
00180             $GLOBALS['typo3CacheFactory']->create(
00181                 'cache_extbase_reflection',
00182                 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['frontend'],
00183                 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['backend'],
00184                 $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_extbase_reflection']['options']
00185             );
00186         }
00187     }
00188 
00189     /**
00190      * Initializes the Reflection Service
00191      *
00192      * @return void
00193      * @see initialize()
00194      */
00195     protected function initializeReflection() {
00196         $this->reflectionService = $this->objectManager->get('Tx_Extbase_Reflection_Service');
00197         $this->reflectionService->setDataCache($this->cacheManager->getCache('cache_extbase_reflection'));
00198         if (!$this->reflectionService->isInitialized()) {
00199             $this->reflectionService->initialize();
00200         }
00201     }
00202 
00203     /**
00204      * Initializes the persistence framework
00205      *
00206      * @return void
00207      * @see initialize()
00208      */
00209     public function initializePersistence() {
00210         $this->persistenceManager = $this->objectManager->get('Tx_Extbase_Persistence_Manager'); // singleton
00211     }
00212 
00213     /**
00214      * Initializes the backwards compatibility. This is necessary because the
00215      * old Dispatcher provided several static methods.
00216      *
00217      * @return void
00218      * @see initialize()
00219      */
00220     protected function initializeBackwardsCompatibility() {
00221         $dispatcher = t3lib_div::makeInstance('Tx_Extbase_Dispatcher');
00222         $dispatcher->injectConfigurationManager($this->configurationManager);
00223         $dispatcher->injectPersistenceManager($this->persistenceManager);
00224     }
00225 
00226     /**
00227      * Runs the the Extbase Framework by resolving an appropriate Request Handler and passing control to it.
00228      * If the Framework is not initialized yet, it will be initialized.
00229      *
00230      * @param string $content The content
00231      * @param array $configuration The TS configuration array
00232      * @return string $content The processed content
00233      * @api
00234      */
00235     public function run($content, $configuration) {
00236         //var_dump(Tx_Extbase_Utility_Extension::createAutoloadRegistryForExtension('extbase', t3lib_extMgm::extPath('extbase'), array(
00237         //  'tx_extbase_basetestcase' => '$extensionClassesPath . \'../Tests/BaseTestCase.php\'',
00238         //  'tx_extbase_tests_unit_basetestcase' => '$extensionClassesPath . \'../Tests/Unit/BaseTestCase.php\'',
00239         //)));
00240         //die("autoload registry");
00241 
00242         $this->initialize($configuration);
00243 
00244         $requestHandlerResolver = $this->objectManager->get('Tx_Extbase_MVC_RequestHandlerResolver');
00245         $requestHandler = $requestHandlerResolver->resolveRequestHandler();
00246 
00247         $response = $requestHandler->handleRequest();
00248 
00249         // If response is NULL after handling the request we need to stop
00250         // This happens for instance, when a USER object was converted to a USER_INT
00251         // @see Tx_Extbase_MVC_Web_FrontendRequestHandler::handleRequest()
00252         if ($response === NULL) {
00253             $this->reflectionService->shutdown();
00254             return;
00255         }
00256         if (count($response->getAdditionalHeaderData()) > 0) {
00257             $GLOBALS['TSFE']->additionalHeaderData[] = implode(chr(10), $response->getAdditionalHeaderData());
00258         }
00259         $response->sendHeaders();
00260         $content = $response->getContent();
00261 
00262         $this->resetSingletons();
00263         return $content;
00264     }
00265 
00266     /**
00267      * Resets global singletons for the next plugin
00268      *
00269      * @return void
00270      */
00271     protected function resetSingletons() {
00272         $this->persistenceManager->persistAll();
00273         $this->reflectionService->shutdown();
00274     }
00275 
00276      /**
00277       * This method forwards the call to run(). This method is invoked by the mod.php
00278       * function of TYPO3.
00279       *
00280       * @param string $moduleSignature
00281       * @return boolean TRUE, if the request request could be dispatched
00282       * @see run()
00283       **/
00284     public function callModule($moduleSignature) {
00285         if (!isset($GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature])) {
00286             return FALSE;
00287         }
00288         $moduleConfiguration = $GLOBALS['TBE_MODULES']['_configuration'][$moduleSignature];
00289 
00290         // Check permissions and exit if the user has no permission for entry
00291         $GLOBALS['BE_USER']->modAccess($moduleConfiguration, TRUE);
00292         if (t3lib_div::_GP('id')) {
00293             // Check page access
00294             $permClause = $GLOBALS['BE_USER']->getPagePermsClause(TRUE);
00295             $access = is_array(t3lib_BEfunc::readPageAccess((integer)t3lib_div::_GP('id'), $permClause));
00296             if (!$access) {
00297                 throw new RuntimeException('You don\'t have access to this page', 1289917924);
00298             }
00299         }
00300 
00301         // BACK_PATH is the path from the typo3/ directory from within the
00302         // directory containing the controller file. We are using mod.php dispatcher
00303         // and thus we are already within typo3/ because we call typo3/mod.php
00304         $GLOBALS['BACK_PATH'] = '';
00305 
00306         $configuration = array(
00307             'extensionName' => $moduleConfiguration['extensionName'],
00308             'pluginName' => $moduleSignature
00309         );
00310         $content = $this->run('', $configuration);
00311 
00312         print $content;
00313         return TRUE;
00314     }
00315 }
00316 ?>