/** * Set request keys based on values in request object * * @return void */ protected function _setRequestKeys() { if (null !== $this->_request) { $this->_moduleKey = $this->_request->getModuleKey(); $this->_controllerKey = $this->_request->getControllerKey(); $this->_actionKey = $this->_request->getActionKey(); } if (null !== $this->_dispatcher) { $this->_defaults += array($this->_controllerKey => $this->_dispatcher->getDefaultControllerName(), $this->_actionKey => $this->_dispatcher->getDefaultAction(), $this->_moduleKey => $this->_dispatcher->getDefaultModule()); } $this->_keysSet = true; }
/** * Forward request with next action * * @param array $next * @return void */ public function forward(EvHttp_Controller_Request_Abstract $next) { $this->getRequest()->setModuleName($next->getModuleName())->setControllerName($next->getControllerName())->setActionName($next->getActionName())->setParams($next->getParams())->setDispatched(false); }
/** * Set a userland parameter * * Uses $key to set a userland parameter. If $key is an alias, the actual * key will be retrieved and used to set the parameter. * * @param mixed $key * @param mixed $value * @return EvHttp_Controller_Request_Http */ public function setParam($key, $value) { $key = null !== ($alias = $this->getAlias($key)) ? $alias : $key; parent::setParam($key, $value); return $this; }
/** * postDispatch() plugin hook -- check for exceptions and dispatch error * handler if necessary * * If the 'noErrorHandler' front controller flag has been set, * returns early. * * @param EvHttp_Controller_Request_Abstract $request * @return void */ public function postDispatch(EvHttp_Controller_Request_Abstract $request) { $frontController = EvHttp_Controller_Front::getInstance(); if ($frontController->getParam('noErrorHandler')) { return; } $response = $this->getResponse(); if ($this->_isInsideErrorHandlerLoop) { $exceptions = $response->getException(); if (count($exceptions) > $this->_exceptionCountAtFirstEncounter) { // Exception thrown by error handler; tell the front controller to throw it $frontController->throwExceptions(true); throw array_pop($exceptions); } } // check for an exception AND allow the error handler controller the option to forward if ($response->isException() && !$this->_isInsideErrorHandlerLoop) { $this->_isInsideErrorHandlerLoop = true; // Get exception information $error = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS); $exceptions = $response->getException(); $exception = $exceptions[0]; $exceptionType = get_class($exception); $error->exception = $exception; switch ($exceptionType) { case 'EvHttp_Controller_Dispatcher_Exception': $error->type = self::EXCEPTION_NO_CONTROLLER; break; case 'EvHttp_Controller_Action_Exception': if (404 == $exception->getCode()) { $error->type = self::EXCEPTION_NO_ACTION; } else { $error->type = self::EXCEPTION_OTHER; } break; default: $error->type = self::EXCEPTION_OTHER; break; } // Keep a copy of the original request $error->request = clone $request; // get a count of the number of exceptions encountered $this->_exceptionCountAtFirstEncounter = count($exceptions); // Forward to the error handler $request->setParam('error_handler', $error)->setModuleName($this->getErrorHandlerModule())->setControllerName($this->getErrorHandlerController())->setActionName($this->getErrorHandlerAction())->setDispatched(false); } }
/** * Find a matching route to the current PATH_INFO and inject * returning values to the Request object. * * @throws EvHttp_Controller_Router_Exception * @return EvHttp_Controller_Request_Abstract Request object */ public function route(EvHttp_Controller_Request_Abstract $request) { if (!$request instanceof EvHttp_Controller_Request_Http) { require_once 'EvHttp/Controller/Router/Exception.php'; throw new EvHttp_Controller_Router_Exception('EvHttp_Controller_Router_Rewrite requires a EvHttp_Controller_Request_Http-based request object'); } if ($this->_useDefaultRoutes) { $this->addDefaultRoutes(); } // Find the matching route foreach (array_reverse($this->_routes) as $name => $route) { // TODO: Should be an interface method. Hack for 1.0 BC if (method_exists($route, 'isAbstract') && $route->isAbstract()) { continue; } // TODO: Should be an interface method. Hack for 1.0 BC if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) { $match = $request->getPathInfo(); } else { $match = $request; } if ($params = $route->match($match)) { $this->_setRequestParams($request, $params); $this->_currentRoute = $name; break; } } return $request; }
/** * Dispatch an HTTP request to a controller/action. * * @param EvHttp_Controller_Request_Abstract|null $request * @param EvHttp_Controller_Response_Abstract|null $response * @return void|EvHttp_Controller_Response_Abstract Returns response object if returnResponse() is true */ public function dispatch(EvHttp_Controller_Request_Abstract $request = null, EvHttp_Controller_Response_Abstract $response = null) { if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('EvHttp_Controller_Plugin_ErrorHandler')) { // Register with stack index of 100 require_once 'EvHttp/Controller/Plugin/ErrorHandler.php'; $this->_plugins->registerPlugin(new EvHttp_Controller_Plugin_ErrorHandler(), 100); } if (!$this->getParam('noViewRenderer') && !EvHttp_Controller_Action_HelperBroker::hasHelper('viewRenderer')) { require_once 'EvHttp/Controller/Action/Helper/ViewRenderer.php'; EvHttp_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new EvHttp_Controller_Action_Helper_ViewRenderer()); } /** * Instantiate default request object (HTTP version) if none provided */ if (null !== $request) { $this->setRequest($request); } elseif (null !== ($request = $this->getRequest())) { $request->init(); } elseif (null === $request && null === ($request = $this->getRequest())) { require_once 'EvHttp/Controller/Request/Http.php'; $request = new EvHttp_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 = $this->getResponse())) { $this->_response->reset(); } elseif (null === $this->_response && null === ($this->_response = $this->getResponse())) { require_once 'EvHttp/Controller/Response/Http.php'; $response = new EvHttp_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); $router->route($this->_request); /** * 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(); }
/** * Determine the action name * * First attempt to retrieve from request; then from request params * using action key; default to default action * * Returns formatted action name * * @param EvHttp_Controller_Request_Abstract $request * @return string */ public function getActionMethod(EvHttp_Controller_Request_Abstract $request) { $action = $request->getActionName(); if (empty($action)) { $action = $this->getDefaultAction(); $request->setActionName($action); } return $this->formatActionName($action); }