Beispiel #1
0
 /**
  * Runs the application and routes the request.
  *
  * @param object $app Instantiated application object.
  */
 public static function run($app)
 {
     $route = Router::process(new Request());
     // Route to 404 if controller and/or method cannot be found
     if (!class_exists($route['controller']) or !method_exists($route['controller'], "{$route['method']}Action")) {
         if ($app->environment() == 'development') {
             throw new Exception("Unable to find controller for '" . Request::pathInfo() . "'");
         } else {
             $route = Router::set404();
         }
     }
     // Get method parameters
     $r = new ReflectionMethod("{$route['controller']}::{$route['method']}Action");
     $params = [];
     foreach ($r->getParameters() as $param) {
         if (isset($route['params'][$param->getName()])) {
             $params[] = $route['params'][$param->getName()];
         }
     }
     unset($r, $param);
     static::$controller = new $route['controller']();
     // Run before filters
     $beforeAll = EventDispatcher::dispatch("before." . $route['controller'] . "::*");
     $beforeAction = EventDispatcher::dispatch("before." . $route['controller'] . "::{$route['method']}");
     if ($beforeAll instanceof Response || $beforeAction instanceof Response) {
         static::$controller->executeAction = false;
         $response = $beforeAll ?: $beforeAction;
     }
     // Execute action
     if (static::$controller->executeAction) {
         $response = call_user_func_array(array(static::$controller, $route['method'] . 'Action'), $params);
     }
     // Run after filters
     $afterAll = EventDispatcher::dispatch("after." . $route['controller'] . "::*");
     $afterAction = EventDispatcher::dispatch("after." . $route['controller'] . "::{$route['method']}");
     if ($afterAll instanceof Response || $afterAction instanceof Response) {
         $response = $afterAll instanceof Response ? $afterAll : $afterAction;
     }
     // Send response
     if (!$response instanceof Response) {
         throw new Exception("The controller [{$route['controller']}::{$route['method']}] returned an invalid response.");
     } else {
         $response->send();
     }
     // Shutdown the controller
     static::$controller->__shutdown();
 }
Beispiel #2
0
 /**
  * Easily respond to different request formats.
  *
  * @param callable $func
  *
  * @return object
  */
 public function respondTo($func)
 {
     $route = Router::currentRoute();
     $response = $func($route['extension'], $this);
     if ($response === null) {
         return $this->show404();
     }
     return $response;
 }