Example #1
0
 protected static function rm_controller($controller)
 {
     foreach (Controller::$controllers as $k => $c) {
         if ($c === $controller) {
             unset(Controller::$controllers[$k]);
         }
     }
     Controller::$controllers = array_values(Controller::$controllers);
 }
Example #2
0
 /**
  * 执行指定URI的控制器
  *
  * @param string $uri
  */
 public static function execute($uri)
 {
     $found = self::find_controller($uri);
     if ($found) {
         require $found['file'];
         $class_name = $found['namespace'] . $found['class'];
         if (class_exists($class_name, false)) {
             $controller = new $class_name();
             Controller::$controllers[] = $controller;
             $rm_controoler = function () use($controller) {
                 foreach (Controller::$controllers as $k => $c) {
                     if ($c === $controller) {
                         unset(Controller::$controllers[$k]);
                     }
                 }
                 Controller::$controllers = array_values(Controller::$controllers);
             };
             $arguments = $found['args'];
             if ($arguments) {
                 $action = current($arguments);
                 if (0 === strlen($action)) {
                     $action = 'default';
                 }
             } else {
                 $action = 'index';
             }
             $action_name = 'action_' . $action;
             if (!method_exists($controller, $action_name)) {
                 if ($action_name != 'action_default' && method_exists($controller, 'action_default')) {
                     $action_name = 'action_default';
                 } elseif (method_exists($controller, '__call')) {
                     $controller->__call($action_name, $arguments);
                     $rm_controoler();
                     return;
                 } else {
                     $rm_controoler();
                     throw new Exception(__('Page Not Found'), 404);
                 }
             } else {
                 array_shift($arguments);
             }
             $ispublicmethod = new ReflectionMethod($controller, $action_name);
             if (!$ispublicmethod->isPublic()) {
                 $rm_controoler();
                 throw new Exception(__('Request Method Not Allowed.'), 405);
             }
             unset($ispublicmethod);
             # 将参数传递给控制器
             $controller->action = $action_name;
             $controller->controller = $found['class'];
             $controller->ids = $found['ids'];
             if (IS_SYSTEM_MODE) {
                 # 系统内部调用参数
                 $controller->arguments = @unserialize(HttpIO::POST('data', HttpIO::PARAM_TYPE_OLDDATA));
             } else {
                 $controller->arguments = $arguments;
             }
             # 前置方法
             if (method_exists($controller, 'before')) {
                 $controller->before();
             }
             # 执行方法
             $count_arguments = count($arguments);
             switch ($count_arguments) {
                 case 0:
                     $controller->{$action_name}();
                     break;
                 case 1:
                     $controller->{$action_name}($arguments[0]);
                     break;
                 case 2:
                     $controller->{$action_name}($arguments[0], $arguments[1]);
                     break;
                 case 3:
                     $controller->{$action_name}($arguments[0], $arguments[1], $arguments[2]);
                     break;
                 case 4:
                     $controller->{$action_name}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
                     break;
                 default:
                     call_user_func_array(array($controller, $action_name), $arguments);
                     break;
             }
             # 后置方法
             if (method_exists($controller, 'after')) {
                 $controller->after();
             }
             # 移除控制器
             $rm_controoler();
         } else {
             throw new Exception(__('Page Not Found'), 404);
         }
     } else {
         throw new Exception(__('Page Not Found'), 404);
     }
 }