Beispiel #1
0
 /**
  * 执行指定URI的控制器
  *
  * @param string $uri
  */
 public static function execute($uri)
 {
     $found = Core::find_controller($uri);
     if ($found) {
         if (isset($found['route'])) {
             $class_name = $found['class'];
             $class_exists = class_exists($class_name, true);
             $arguments = array();
             if (isset($found['route']['action']) && $found['route']['action']) {
                 $arguments[] = $found['route']['action'];
             }
         } else {
             if (!class_exists($found['class'], false)) {
                 require $found['file'];
             }
             if (class_exists($found['class'], false)) {
                 $class_name = $found['class'];
                 $class_exists = true;
             } else {
                 $class_name = str_replace('.', '_', $found['ns']) . '_' . $found['class'];
                 $class_exists = class_exists($class_name, false);
             }
         }
         if ($class_exists) {
             $controller = new $class_name();
             Controller::$controllers[] = $controller;
             # 是否有必要将action从$arguments中移出
             $need_shift_action = false;
             $arguments = $found['args'];
             if ($arguments && $arguments) {
                 $action = str_replace('-', '_', current($arguments));
                 if (0 === strlen($action)) {
                     $action = 'default';
                 } else {
                     $need_shift_action = true;
                 }
             } else {
                 $action = 'index';
             }
             $action_name = 'action_' . $action;
             if (!method_exists($controller, $action_name)) {
                 if ($action_name != 'action_default' && method_exists($controller, 'action_default')) {
                     $action = 'default';
                     $action_name = 'action_default';
                 } elseif ($action_name !== '' && (!$arguments || $arguments === array('')) && method_exists($controller, 'action_index')) {
                     $action = 'index';
                     $action_name = 'action_index';
                 } elseif (method_exists($controller, '__call')) {
                     $controller->__call($action_name, $arguments);
                     Core::rm_controller($controller);
                     return null;
                 } else {
                     Core::rm_controller($controller);
                     throw new Exception(__('Page Not Found'), E_PAGE_NOT_FOUND);
                 }
             } elseif ($need_shift_action) {
                 array_shift($arguments);
             }
             # 对后缀进行判断
             if ($found['suffix']) {
                 if ($found['suffix'] == Core::config('url_suffix')) {
                     # 默认允许的后缀
                 } elseif (is_array($controller->allow_suffix)) {
                     if (!isset($controller->allow_suffix[$action]) || !in_array($found['suffix'], explode('|', $controller->allow_suffix[$action]))) {
                         Core::rm_controller($controller);
                         throw new Exception(__('Page Not Found'), E_PAGE_NOT_FOUND);
                     }
                 } elseif (!in_array($found['suffix'], explode('|', $controller->allow_suffix))) {
                     Core::rm_controller($controller);
                     throw new Exception(__('Page Not Found'), E_PAGE_NOT_FOUND);
                 }
                 # 默认输出页面头信息
                 if (!in_array($found['suffix'], array('php', 'html', 'htm'))) {
                     @header('Content-Type: ' . File::mime_by_ext($found['suffix']));
                 }
             }
             $is_public_method = new ReflectionMethod($controller, $action_name);
             if (!$is_public_method->isPublic()) {
                 Core::rm_controller($controller);
                 Core::show_404(__('Request Method Not Allowed.'), 405);
             }
             unset($is_public_method);
             # POST 方式,自动CSRF判断
             if (HttpIO::METHOD === 'POST') {
                 $auto_check_post_method_referrer = isset($controller->auto_check_post_method_referrer) ? $controller->auto_check_post_method_referrer : Core::config('auto_check_post_method_referrer', true);
                 if ($auto_check_post_method_referrer && !HttpIO::csrf_check()) {
                     Core::rm_controller($controller);
                     Core::show_404(__('Not Acceptable.'), 406);
                 }
             }
             if (isset($found['route'])) {
                 # 设置Route参数
                 foreach ($found['route'] as $k => $v) {
                     $controller->{$k} = $v;
                 }
             } else {
                 $controller->ids = $found['ids'];
             }
             # 将参数传递给控制器
             $controller->action = $action_name;
             $controller->controller = $found['class'];
             $controller->uri = $uri;
             $controller->directory = $found['dir'];
             $controller->suffix = $found['suffix'];
             if (IS_SYSTEM_MODE) {
                 # 系统内部调用参数
                 $controller->arguments = @unserialize(HttpIO::POST('data', HttpIO::PARAM_TYPE_OLDDATA));
             } else {
                 $controller->arguments = $arguments;
             }
             # 设置 HttpIO 参数
             HttpIO::set_params_controller($controller);
             # 前置方法
             if (method_exists($controller, 'before')) {
                 $controller->before();
             }
             # 执行方法
             $count_arguments = count($arguments);
             switch ($count_arguments) {
                 case 0:
                     $rs = $controller->{$action_name}();
                     break;
                 case 1:
                     $rs = $controller->{$action_name}($arguments[0]);
                     break;
                 case 2:
                     $rs = $controller->{$action_name}($arguments[0], $arguments[1]);
                     break;
                 case 3:
                     $rs = $controller->{$action_name}($arguments[0], $arguments[1], $arguments[2]);
                     break;
                 case 4:
                     $rs = $controller->{$action_name}($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
                     break;
                 default:
                     $rs = call_user_func_array(array($controller, $action_name), $arguments);
                     break;
             }
             # 后置方法
             if (method_exists($controller, 'after')) {
                 $controller->after();
             }
             # 移除控制器
             Core::rm_controller($controller);
             unset($controller);
             return $rs;
         } else {
             Core::show_404();
         }
     } else {
         Core::show_404();
         //throw new Exception(__('Page Not Found'), E_PAGE_NOT_FOUND);
     }
     return null;
 }
Beispiel #2
0
 /**
  * 执行指定URI的控制器
  *
  * @param string $uri
  */
 public static function execute($uri)
 {
     $found = Core::find_controller($uri);
     if ($found) {
         if (isset($found['route'])) {
             $class_name = $found['class'];
             $class_exists = class_exists($class_name, true);
             $arguments = array();
             if (isset($found['route']['action']) && $found['route']['action']) {
                 $arguments[] = $found['route']['action'];
             }
         } else {
             require $found['file'];
             if ($found['ns'] == 'team-library' || $found['ns'] == 'project') {
                 $class_name = $found['class'];
             } else {
                 $class_name = str_replace('.', '_', $found['ns']) . '_' . $found['class'];
             }
             $class_exists = class_exists($class_name, false);
         }
         if ($class_exists) {
             $controller = new $class_name();
             Controller::$controllers[] = $controller;
             # 是否有必要将action从$arguments中移出
             $need_shift_action = false;
             $arguments = $found['args'];
             if ($arguments) {
                 $action = current($arguments);
                 if (0 === strlen($action)) {
                     $action = 'default';
                 } else {
                     $need_shift_action = true;
                 }
             } 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);
                     Core::rm_controoler($controller);
                     return;
                 } else {
                     Core::rm_controoler($controller);
                     throw new Exception(__('Page Not Found'), 404);
                 }
             } elseif ($need_shift_action) {
                 array_shift($arguments);
             }
             $ispublicmethod = new ReflectionMethod($controller, $action_name);
             if (!$ispublicmethod->isPublic()) {
                 Core::rm_controoler($controller);
                 throw new Exception(__('Request Method Not Allowed.'), 405);
             }
             unset($ispublicmethod);
             if (isset($found['route'])) {
                 # 设置Route参数
                 foreach ($found['route'] as $k => $v) {
                     $controller->{$k} = $v;
                 }
             } else {
                 $controller->ids = $found['ids'];
             }
             # 将参数传递给控制器
             $controller->action = $action_name;
             $controller->controller = $found['class'];
             $controller->uri = $uri;
             $controller->directory = $found['dir'];
             if (IS_SYSTEM_MODE) {
                 # 系统内部调用参数
                 $controller->arguments = @unserialize(HttpIO::POST('data', HttpIO::PARAM_TYPE_OLDDATA));
             } else {
                 $controller->arguments = $arguments;
             }
             # 设置 HttpIO 参数
             HttpIO::set_params_controller($controller);
             # 前置方法
             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();
             }
             # 移除控制器
             Core::rm_controoler($controller);
             unset($controller);
         } else {
             throw new Exception(__('Page Not Found'), 404);
         }
     } else {
         throw new Exception(__('Page Not Found'), 404);
     }
 }