protected function initialize() { $arrConf = Loader::getInstance()->getConfig(); if ($arrConf) { //初始化插件列表 if (isset($arrConf['plugins'])) { foreach ($arrConf['plugins'] as $p) { $plugin = new $p(); $this->_dispatcher->addPlugin($plugin); } } //初始化请求类 if (isset($arrConf['request_class']) && $arrConf['request_class']) { $request = new $arrConf['request_class'](); $this->_dispatcher->setRequest($request); } //初始化响应类 if (isset($arrConf['response_class']) && $arrConf['response_class']) { $response = new $arrConf['response_class'](); $this->_dispatcher->setResponse($response); } //初始化试图类 if (isset($arrConf['view_class']) && $arrConf['view_class']) { $view = new $arrConf['view_class'](); $this->_dispatcher->setView($view); } if (isset($arrConf['enable_debug']) && $arrConf['enable_debug']) { $this->setDebugEnabled(true); } } }
public function dispatch(Route $route) { $protoView = $this->getBootstrap()->getResource("view") ? $this->getBootstrap()->getResource("view") : new View(); $controllerPath = $this->getControllerPath(); $router = $this->_router; $request = $this->_request; $protoView->addHelper("pull", function ($uri) use($controllerPath, $router, $request) { $request = clone $request; $request->setUri($uri); $routeObj = $router->match($request); $controllerClassName = $routeObj->getControllerName() . "Controller"; $action = $routeObj->getActionName() . "Action"; $classPath = realpath($controllerPath . DIRECTORY_SEPARATOR . $controllerClassName . ".php"); if (file_exists($classPath)) { require_once $classPath; $controller = new $controllerClassName(); $controller->setParams($routeObj->getParams()); if (method_exists($controller, $action)) { ob_start(); $controller->init(); $data = $controller->{$action}(); ob_end_clean(); return $data; } else { throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404); } } else { throw new RuntimeException("Pull operation {$routeObj->getControllerName()} - {$routeObj->getActionName()} failed.", 404); } }); $dispatcher = new Dispatcher($protoView); $dispatcher->setRouter($this->_router); $dispatcher->setRequest($this->_request); $dispatcher->setEventManager($this->getEventManager()); $dispatcher->setBootstrap($this->_bootstrap); $dispatcher->setControllerPath($this->getControllerPath()); try { $this->_page = $dispatcher->dispatch($route); } catch (Exception $e) { $errorRoute = new Route(); $errorRoute->addParams(array('exception' => $e)); $dispatcher->clearHeaders(); $dispatcher->addHeader("", "", 404); $errorRoute->setControllerName("error"); $errorRoute->setActionName("error"); $this->_page = $dispatcher->dispatch($errorRoute); } return array('headers' => $dispatcher->getHeaders()); }