TYPO3 API  SVNRelease
RequestHandlerResolver.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 class is a backport of the corresponding class of FLOW3.
00009  *  All credits go to the v5 team.
00010  *
00011  *  This script is part of the TYPO3 project. The TYPO3 project is
00012  *  free software; you can redistribute it and/or modify
00013  *  it under the terms of the GNU General Public License as published by
00014  *  the Free Software Foundation; either version 2 of the License, or
00015  *  (at your option) any later version.
00016  *
00017  *  The GNU General Public License can be found at
00018  *  http://www.gnu.org/copyleft/gpl.html.
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Analyzes the raw request and delivers a request handler which can handle it.
00030  */
00031 class Tx_Extbase_MVC_RequestHandlerResolver {
00032 
00033     /**
00034      * @var Tx_Extbase_Object_ObjectManagerInterface
00035      */
00036     protected $objectManager;
00037 
00038     /**
00039      * @var Tx_Extbase_Reflection_ReflectionService
00040      */
00041     protected $reflectionService;
00042 
00043     /**
00044      * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
00045      */
00046     protected $configurationManager;
00047 
00048     /**
00049      * Injects the object manager
00050      *
00051      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager A reference to the object manager
00052      * @return void
00053      */
00054     public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00055         $this->objectManager = $objectManager;
00056     }
00057 
00058     /**
00059      * Injects the reflection service
00060      *
00061      * @param Tx_Extbase_Reflection_Service $reflectionService
00062      * @return void
00063      */
00064     public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
00065         $this->reflectionService = $reflectionService;
00066     }
00067 
00068     /**
00069      * Injects the configuration manager
00070      *
00071      * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
00072      * @return void
00073      */
00074     public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
00075         $this->configurationManager = $configurationManager;
00076     }
00077 
00078     /**
00079      * Analyzes the raw request and tries to find a request handler which can handle
00080      * it. If none is found, an exception is thrown.
00081      *
00082      * @return Tx_Extbase_MVC_RequestHandler A request handler
00083      * @throws Tx_Extbase_MVC_Exception
00084      */
00085     public function resolveRequestHandler() {
00086         $availableRequestHandlerClassNames = $this->getRegisteredRequestHandlerClassNames();
00087 
00088         $suitableRequestHandlers = array();
00089         foreach ($availableRequestHandlerClassNames as $requestHandlerClassName) {
00090             $requestHandler = $this->objectManager->get($requestHandlerClassName);
00091             if ($requestHandler->canHandleRequest()) {
00092                 $priority = $requestHandler->getPriority();
00093                 if (isset($suitableRequestHandlers[$priority])) throw new Tx_Extbase_MVC_Exception('More than one request handler with the same priority can handle the request, but only one handler may be active at a time!', 1176475350);
00094                 $suitableRequestHandlers[$priority] = $requestHandler;
00095             }
00096         }
00097         if (count($suitableRequestHandlers) === 0) throw new Tx_Extbase_MVC_Exception('No suitable request handler found.', 1205414233);
00098         ksort($suitableRequestHandlers);
00099         return array_pop($suitableRequestHandlers);
00100     }
00101 
00102     /**
00103      * Returns a list of all registered request handlers.
00104      *
00105      * @return array
00106      */
00107     public function getRegisteredRequestHandlerClassNames() {
00108         $settings = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
00109         return is_array($settings['mvc']['requestHandlers']) ? $settings['mvc']['requestHandlers'] : array();
00110     }
00111 }
00112 ?>