示例#1
0
文件: Kernel.php 项目: nirix/radium
 /**
  * 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();
 }
示例#2
0
 /**
  * Adds the filter to the event dispatcher.
  *
  * @param string $when     Either 'before' or 'after'
  * @param string $action
  * @param mixed  $callback
  */
 protected function addFilter($when, $action, $callback)
 {
     if (!is_callable($callback) && !is_array($callback)) {
         $callback = [$this, $callback];
     }
     if (is_array($action)) {
         foreach ($action as $method) {
             $this->addFilter($when, $method, $callback);
         }
     } else {
         EventDispatcher::addListener("{$when}." . get_called_class() . "::{$action}", $callback);
     }
 }