TYPO3 API  SVNRelease
Dispatcher.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  * Dispatches requests to the controller which was specified by the request and
00030  * returns the response the controller generated.
00031  *
00032  */
00033 class Tx_Extbase_MVC_Dispatcher implements t3lib_Singleton {
00034 
00035     /**
00036      * @var Tx_Extbase_Object_ObjectManagerInterface A reference to the object manager
00037      */
00038     protected $objectManager;
00039 
00040     /**
00041      * @var Tx_Extbase_Reflection_Service
00042      */
00043     protected $reflectionService;
00044 
00045     /**
00046      * @var array
00047      */
00048     protected $settings = array();
00049 
00050     /**
00051      * Constructs the global dispatcher
00052      *
00053      * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager A reference to the object manager
00054      */
00055     public function __construct(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
00056         $this->objectManager = $objectManager;
00057     }
00058 
00059     /**
00060      * Injects the Reflection Service
00061      *
00062      * @param Tx_Extbase_Reflection_Service $reflectionService
00063      * @return void
00064      */
00065     public function injectReflectionService(Tx_Extbase_Reflection_Service $reflectionService) {
00066         $this->reflectionService = $reflectionService;
00067     }
00068 
00069     /**
00070      * Dispatches a request to a controller and initializes the security framework.
00071      *
00072      * @param Tx_Extbase_MVC_RequestInterface $request The request to dispatch
00073      * @param Tx_Extbase_MVC_ResponseInterface $response The response, to be modified by the controller
00074      * @return void
00075      */
00076     public function dispatch(Tx_Extbase_MVC_RequestInterface $request, Tx_Extbase_MVC_ResponseInterface $response) {
00077         $dispatchLoopCount = 0;
00078         while (!$request->isDispatched()) {
00079             if ($dispatchLoopCount++ > 99) throw new Tx_Extbase_MVC_Exception_InfiniteLoop('Could not ultimately dispatch the request after '  . $dispatchLoopCount . ' iterations. Most probably, a @dontvalidate annotation is missing on re-displaying a form with validation errors.', 1217839467);
00080             $controller = $this->resolveController($request);
00081             try {
00082                 $controller->processRequest($request, $response);
00083             } catch (Tx_Extbase_MVC_Exception_StopAction $ignoredException) {
00084             }
00085         }
00086     }
00087 
00088     /**
00089      * Finds and instanciates a controller that matches the current request.
00090      * If no controller can be found, an instance of NotFoundControllerInterface is returned.
00091      *
00092      * @param Tx_Extbase_MVC_RequestInterface $request The request to dispatch
00093      * @return Tx_Extbase_MVC_Controller_ControllerInterface
00094      * @author Bastian Waidelich <bastian@typo3.org>
00095      * @author Robert Lemke <robert@typo3.org>
00096      */
00097     protected function resolveController(Tx_Extbase_MVC_RequestInterface $request) {
00098         $controllerObjectName = $request->getControllerObjectName();
00099         $controller = $this->objectManager->get($controllerObjectName);
00100         if (!$controller instanceof Tx_Extbase_MVC_Controller_ControllerInterface) {
00101             throw new Tx_Extbase_MVC_Exception_InvalidController('Invalid controller "' . $request->getControllerObjectName() . '". The controller must implement the Tx_Extbase_MVC_Controller_ControllerInterface.', 1202921619);
00102         }
00103         return $controller;
00104     }
00105 
00106 }
00107 ?>