Zend Framework教程之分发器Zend(3)

<?php /** Zend_Loader */ require_once 'Zend/Loader.php'; /** Zend_Controller_Action_HelperBroker */ require_once 'Zend/Controller/Action/HelperBroker.php'; /** Zend_Controller_Plugin_Broker */ require_once 'Zend/Controller/Plugin/Broker.php'; class Zend_Controller_Front { protected $_baseUrl = null; protected $_controllerDir = null; protected $_dispatcher = null; protected static $_instance = null; protected $_invokeParams = array(); protected $_moduleControllerDirectoryName = 'controllers'; protected $_plugins = null; protected $_request = null; protected $_response = null; protected $_returnResponse = false; protected $_router = null; protected $_throwExceptions = false; protected function __construct() { $this->_plugins = new Zend_Controller_Plugin_Broker(); } private function __clone() { } public static function getInstance() { if (null === self::$_instance) { self::$_instance = new self(); } return self::$_instance; } public function resetInstance() { $reflection = new ReflectionObject($this); foreach ($reflection->getProperties() as $property) { $name = $property->getName(); switch ($name) { case '_instance': break; case '_controllerDir': case '_invokeParams': $this->{$name} = array(); break; case '_plugins': $this->{$name} = new Zend_Controller_Plugin_Broker(); break; case '_throwExceptions': case '_returnResponse': $this->{$name} = false; break; case '_moduleControllerDirectoryName': $this->{$name} = 'controllers'; break; default: $this->{$name} = null; break; } } Zend_Controller_Action_HelperBroker::resetHelpers(); } public static function run($controllerDirectory) { self::getInstance() ->setControllerDirectory($controllerDirectory) ->dispatch(); } public function addControllerDirectory($directory, $module = null) { $this->getDispatcher()->addControllerDirectory($directory, $module); return $this; } public function setControllerDirectory($directory, $module = null) { $this->getDispatcher()->setControllerDirectory($directory, $module); return $this; } public function getControllerDirectory($name = null) { return $this->getDispatcher()->getControllerDirectory($name); } public function removeControllerDirectory($module) { return $this->getDispatcher()->removeControllerDirectory($module); } public function addModuleDirectory($path) { try{ $dir = new DirectoryIterator($path); } catch(Exception $e) { require_once 'Zend/Controller/Exception.php'; throw new Zend_Controller_Exception("Directory $path not readable", 0, $e); } foreach ($dir as $file) { if ($file->isDot() || !$file->isDir()) { continue; } $module = $file->getFilename(); // Don't use SCCS directories as modules if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) { continue; } $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName(); $this->addControllerDirectory($moduleDir, $module); } return $this; } public function getModuleDirectory($module = null) { if (null === $module) { $request = $this->getRequest(); if (null !== $request) { $module = $this->getRequest()->getModuleName(); } if (empty($module)) { $module = $this->getDispatcher()->getDefaultModule(); } } $controllerDir = $this->getControllerDirectory($module); if ((null === $controllerDir) || !is_string($controllerDir)) { return null; } return dirname($controllerDir); } public function setModuleControllerDirectoryName($name = 'controllers') { $this->_moduleControllerDirectoryName = (string) $name; return $this; } public function getModuleControllerDirectoryName() { return $this->_moduleControllerDirectoryName; } public function setDefaultControllerName($controller) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultControllerName($controller); return $this; } public function getDefaultControllerName() { return $this->getDispatcher()->getDefaultControllerName(); } public function setDefaultAction($action) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultAction($action); return $this; } public function getDefaultAction() { return $this->getDispatcher()->getDefaultAction(); } public function setDefaultModule($module) { $dispatcher = $this->getDispatcher(); $dispatcher->setDefaultModule($module); return $this; } public function getDefaultModule() { return $this->getDispatcher()->getDefaultModule(); } public function setRequest($request) { ........................... return $this; } public function getRequest() { return $this->_request; } public function setRouter($router) { .................... return $this; } public function getRouter() { .................. return $this->_router; } public function setBaseUrl($base = null) { .............. return $this; } public function getBaseUrl() { return $this->_baseUrl; } /** * Set the dispatcher object. The dispatcher is responsible for * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and * call the action method of the controller. * * @param Zend_Controller_Dispatcher_Interface $dispatcher * @return Zend_Controller_Front */ public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher) { $this->_dispatcher = $dispatcher; return $this; } /** * Return the dispatcher object. * * @return Zend_Controller_Dispatcher_Interface */ public function getDispatcher() { /** * Instantiate the default dispatcher if one was not set. */ if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) { require_once 'Zend/Controller/Dispatcher/Standard.php'; $this->_dispatcher = new Zend_Controller_Dispatcher_Standard(); } return $this->_dispatcher; } public function setResponse($response) {.................. return $this; } public function getResponse() { return $this->_response; } public function setParam($name, $value) { $name = (string) $name; $this->_invokeParams[$name] = $value; return $this; } public function setParams(array $params) { $this->_invokeParams = array_merge($this->_invokeParams, $params); return $this; } public function getParam($name) { if(isset($this->_invokeParams[$name])) { return $this->_invokeParams[$name]; } return null; } public function getParams() { return $this->_invokeParams; } public function clearParams($name = null) { if (null === $name) { $this->_invokeParams = array(); } elseif (is_string($name) && isset($this->_invokeParams[$name])) { unset($this->_invokeParams[$name]); } elseif (is_array($name)) { foreach ($name as $key) { if (is_string($key) && isset($this->_invokeParams[$key])) { unset($this->_invokeParams[$key]); } } } return $this; } public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null) { $this->_plugins->registerPlugin($plugin, $stackIndex); return $this; } public function unregisterPlugin($plugin) { $this->_plugins->unregisterPlugin($plugin); return $this; } public function hasPlugin($class) { return $this->_plugins->hasPlugin($class); } public function getPlugin($class) { return $this->_plugins->getPlugin($class); } public function getPlugins() { return $this->_plugins->getPlugins(); } public function throwExceptions($flag = null) { ..................... return $this->_throwExceptions; } public function returnResponse($flag = null) { ................ return $this->_returnResponse; } /** * Dispatch an HTTP request to a controller/action. * * @param Zend_Controller_Request_Abstract|null $request * @param Zend_Controller_Response_Abstract|null $response * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true */ public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null) { if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) { // Register with stack index of 100 require_once 'Zend/Controller/Plugin/ErrorHandler.php'; $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100); } if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) { require_once 'Zend/Controller/Action/Helper/ViewRenderer.php'; Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer()); } /** * Instantiate default request object (HTTP version) if none provided */ if (null !== $request) { $this->setRequest($request); } elseif ((null === $request) && (null === ($request = $this->getRequest()))) { require_once 'Zend/Controller/Request/Http.php'; $request = new Zend_Controller_Request_Http(); $this->setRequest($request); } /** * Set base URL of request object, if available */ if (is_callable(array($this->_request, 'setBaseUrl'))) { if (null !== $this->_baseUrl) { $this->_request->setBaseUrl($this->_baseUrl); } } /** * Instantiate default response object (HTTP version) if none provided */ if (null !== $response) { $this->setResponse($response); } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) { require_once 'Zend/Controller/Response/Http.php'; $response = new Zend_Controller_Response_Http(); $this->setResponse($response); } /** * Register request and response objects with plugin broker */ $this->_plugins ->setRequest($this->_request) ->setResponse($this->_response); /** * Initialize router */ $router = $this->getRouter(); $router->setParams($this->getParams()); /** * Initialize dispatcher */ $dispatcher = $this->getDispatcher(); $dispatcher->setParams($this->getParams()) ->setResponse($this->_response); // Begin dispatch try { /** * Route request to controller/action, if a router is provided */ /** * Notify plugins of router startup */ $this->_plugins->routeStartup($this->_request); try { $router->route($this->_request); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of router completion */ $this->_plugins->routeShutdown($this->_request); /** * Notify plugins of dispatch loop startup */ $this->_plugins->dispatchLoopStartup($this->_request); /** * Attempt to dispatch the controller/action. If the $this->_request * indicates that it needs to be dispatched, move to the next * action in the request. */ do { $this->_request->setDispatched(true); /** * Notify plugins of dispatch startup */ $this->_plugins->preDispatch($this->_request); /** * Skip requested action if preDispatch() has reset it */ if (!$this->_request->isDispatched()) { continue; } /** * Dispatch request */ try { $dispatcher->dispatch($this->_request, $this->_response); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch completion */ $this->_plugins->postDispatch($this->_request); } while (!$this->_request->isDispatched()); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } /** * Notify plugins of dispatch loop completion */ try { $this->_plugins->dispatchLoopShutdown(); } catch (Exception $e) { if ($this->throwExceptions()) { throw $e; } $this->_response->setException($e); } if ($this->returnResponse()) { return $this->_response; } $this->_response->sendResponse(); } }

以上对Zend_Controller_Front和Zend_Controller_Dispatcher做了简单的标记,通过分析代码不难看出,基本的运行机制。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/97b888cac825b6e3f20007902c9bf6e0.html