/**
  * Provides the handler with an opportunity to perform any last minute
  * error handling logic. The returned value will be serialized by the
  * handler's encoder.
  * @param Exception $e The exception that was thrown.
  * @return mixed Returns a serializable value that will be encoded and returned
  *         to the client.
  */
 public function handleException(Exception $e)
 {
     $responseCode = AbstractResponse::RESPONSE_SERVER_ERROR;
     if ($e instanceof RouterExceptionInterface) {
         $responseCode = $e->getAssociatedStatusCode();
     }
     \Vectorface\SnappyRouter\http_response_code($responseCode);
     return parent::handleException($e);
 }
 /**
  * Performs the actual routing.
  * @return string Returns the result of the route.
  */
 public function performRoute()
 {
     $controller = null;
     $action = null;
     $this->determineControllerAndAction($controller, $action);
     $response = $this->invokeControllerAction($controller, $action);
     \Vectorface\SnappyRouter\http_response_code($response->getStatusCode());
     return $this->getEncoder()->encode($response);
 }
示例#3
0
 /**
  * Determines which handler is appropriate for this request.
  *
  * @param bool $isCli True for CLI handlers, false otherwise.
  * @param array $handlerParams An array parameters required by the handler.
  * @return Returns the handler to be used for the route.
  */
 private function invokeHandler($isCli, $handlerParams)
 {
     $activeHandler = null;
     try {
         // determine which handler should handle this path
         $activeHandler = $this->determineHandler($isCli, $handlerParams);
         // invoke the initial plugin hook
         $activeHandler->invokePluginsHook('afterHandlerSelected', array($activeHandler));
         $response = $activeHandler->performRoute();
         $activeHandler->invokePluginsHook('afterFullRouteInvoked', array($activeHandler));
         return $response;
     } catch (Exception $e) {
         // if we have a valid handler give it a chance to handle the error
         if (null !== $activeHandler) {
             $activeHandler->invokePluginsHook('errorOccurred', array($activeHandler, $e));
             return $activeHandler->getEncoder()->encode(new Response($activeHandler->handleException($e)));
         }
         // if not on the command line, set an HTTP response code
         if (!$isCli) {
             $responseCode = AbstractResponse::RESPONSE_SERVER_ERROR;
             if ($e instanceof RouterExceptionInterface) {
                 $responseCode = $e->getAssociatedStatusCode();
             }
             \Vectorface\SnappyRouter\http_response_code($responseCode);
         }
         return $e->getMessage();
     }
 }
 /**
  * Tests that an invalid status code throws an exception.
  * @expectedException \Exception
  * @expectedExceptionMessage Invalid response code: 9999
  */
 public function testInvalidStatusCodeThrowsException()
 {
     \Vectorface\SnappyRouter\http_response_code(9999);
 }