Exemplo n.º 1
0
 public function __toString()
 {
     try {
         return $this->render();
     } catch (Exception $e) {
         /**
          * Display the exception message.
          *
          * We use this method here because it"s impossible to throw an
          * exception from __toString().
          */
         $error_response = JsonApiApplication_Exception::_handler($e);
         return $error_response->body();
     }
 }
Exemplo n.º 2
0
 public function execute_request(Request $request, Response $response)
 {
     $prefix = "Controller_";
     $directory = $request->directory();
     $controller = $request->controller();
     if ($directory) {
         $prefix .= str_replace(array("\\", "/"), "_", trim($directory, "/")) . "_";
     }
     if (JsonApiApplication::$profiling) {
         $benchmark = "'" . $request->uri() . "'";
         if ($request !== Request::$initial and Request::$current) {
             $benchmark .= " « '" . Request::$current->uri() . "'";
         }
         $benchmark = Profiler::start("Requests", $benchmark);
     }
     $previous = Request::$current;
     Request::$current = $request;
     $initial_request = $request === Request::$initial;
     try {
         if (!class_exists($prefix . $controller)) {
             throw HTTP_Exception::factory(404, "The requested URL :uri was not found on this server.", array(":uri" => $request->uri()))->request($request);
         }
         $class = new ReflectionClass($prefix . $controller);
         if ($class->isAbstract()) {
             throw new JsonApiApplication_Exception("Cannot create instances of abstract :controller", array(":controller" => $prefix . $controller));
         }
         $controller = $class->newInstance($request, $response);
         $response = $class->getMethod("execute")->invoke($controller);
         if (!$response instanceof Response) {
             throw new JsonApiApplication_Exception("Controller failed to return a Response");
         }
     } catch (HTTP_Exception $e) {
         if ($e->request() === NULL) {
             $e->request($request);
         }
         $response = $e->get_response();
     } catch (Exception $e) {
         $response = JsonApiApplication_Exception::_handler($e);
     }
     Request::$current = $previous;
     if (isset($benchmark)) {
         Profiler::stop($benchmark);
     }
     return $response;
 }
Exemplo n.º 3
0
 public static function handler(Exception $e)
 {
     $response = JsonApiApplication_Exception::_handler($e);
     echo $response->send_headers()->body();
     exit(1);
 }