예제 #1
0
 public function __construct()
 {
     $this->delegate = self::$app_delegate;
     $runtime_config = $this->delegate->getClosureContainer()->run('~controller~runtime~');
     $this->view_controller = $runtime_config['view_controller_namespace'];
     $this->action_annotate = $runtime_config['action_annotate'];
     $this->controller = $runtime_config['controller'];
     $this->action = $runtime_config['action'];
     $this->params = $runtime_config['params'];
 }
예제 #2
0
파일: Rest.php 프로젝트: im286er/crossphp
 /**
  * 处理请求
  *
  * @throws CoreException
  */
 function run()
 {
     $match = false;
     $params = array();
     $process_closure = null;
     if (!empty($this->custom_router_config[$this->request_type])) {
         $custom_routers = $this->custom_router_config[$this->request_type];
         if (!empty($custom_routers['high']) && isset($custom_routers['high'][$this->request_string])) {
             $match = true;
             $process_closure = $custom_routers['high'][$this->request_string];
         } elseif (!empty($custom_routers['current'])) {
             $match = $this->matchProcess($custom_routers['current'], $process_closure, $params);
         } elseif (!empty($custom_routers['global'])) {
             $match = $this->matchProcess($custom_routers['global'], $process_closure, $params);
         }
     }
     if ($match && $process_closure !== null) {
         $this->response($process_closure, $params);
     } else {
         $closure_container = $this->delegate->getClosureContainer();
         if ($closure_container->has('mismatching')) {
             $closure_container->run('mismatching');
         } else {
             throw new CoreException('Not match uri');
         }
     }
 }
예제 #3
0
파일: Rest.php 프로젝트: ss7247/crossphp
 /**
  * 处理请求
  *
  * @throws CoreException
  */
 function run()
 {
     $match = false;
     $params = array();
     $process_closure = null;
     if (!empty($this->custom_router_config[$this->request_type])) {
         $custom_routers = $this->custom_router_config[$this->request_type];
         if (!empty($custom_routers['high_level']) && isset($custom_routers['high_level'][$this->request_string])) {
             $match = true;
             $process_closure = $custom_routers['high_level'][$this->request_string];
         } elseif (!empty($custom_routers['low_level'])) {
             foreach ($custom_routers['low_level'] as $router => $router_config) {
                 $params = array();
                 if (true === $this->matchCustomRouter($router, $router_config['params_key'], $params)) {
                     $match = true;
                     $process_closure = $router_config['process_closure'];
                     break;
                 }
             }
         }
     }
     if ($match && $process_closure !== null) {
         $this->response($process_closure, $params);
     } else {
         $closure_container = $this->delegate->getClosureContainer();
         if ($closure_container->has('mismatching')) {
             $closure_container->run('mismatching');
         } else {
             throw new CoreException('Not match uri');
         }
     }
 }
예제 #4
0
 /**
  * 设置params
  *
  * @param array $params
  */
 private function setParams($params)
 {
     $params_checker = $this->delegate->getClosureContainer()->has('setParams', $closure);
     if ($params_checker && !empty($params)) {
         array_walk($params, $closure);
     }
     $this->params = $params;
 }
예제 #5
0
 /**
  * 运行框架
  *
  * @param object|string $router
  * @param null $args 指定参数
  * @param bool $return_response_content 是否输出执行结果
  * @return array|mixed|string
  * @throws CoreException
  */
 public function dispatcher($router, $args = null, $return_response_content = false)
 {
     $init_prams = true;
     $router = $this->parseRouter($router, $args, $init_prams);
     $cr = $this->initController($router['controller'], $router['action']);
     $annotate_config = $this->getAnnotateConfig();
     if ($init_prams) {
         $action_params = array();
         if (isset($annotate_config['params'])) {
             $action_params = $annotate_config['params'];
         }
         $this->initParams($router['params'], $action_params);
     } else {
         $this->setParams($router['params']);
     }
     $this->delegate->getClosureContainer()->run('dispatcher');
     $cache = false;
     if (isset($annotate_config['cache']) && Request::getInstance()->isGetRequest()) {
         $cache = $this->initRequestCache($annotate_config['cache']);
     }
     if (isset($annotate_config['before'])) {
         $this->getClassInstanceByName($annotate_config['before']);
     }
     if (!empty($annotate_config['basicAuth'])) {
         Response::getInstance()->basicAuth($annotate_config['basicAuth']);
     }
     if ($cache && $cache->getExpireTime()) {
         $response_content = $cache->get();
     } else {
         $action = $this->getAction();
         $controller_name = $this->getController();
         try {
             $cr->setStaticPropertyValue('action_annotate', $annotate_config);
             $cr->setStaticPropertyValue('view_controller_namespace', $this->getViewControllerNameSpace($controller_name));
             $cr->setStaticPropertyValue('controller_name', $controller_name);
             $cr->setStaticPropertyValue('call_action', $action);
             $cr->setStaticPropertyValue('url_params', $this->getParams());
             $cr->setStaticPropertyValue('app_delegate', $this->delegate);
             $controller = $cr->newInstance();
         } catch (Exception $e) {
             throw new CoreException($e->getMessage());
         }
         if (Response::getInstance()->isEndFlush()) {
             return true;
         }
         ob_start();
         $response_content = $controller->{$action}();
         if (!$response_content) {
             $response_content = ob_get_contents();
         }
         ob_end_clean();
         if ($cache) {
             $cache->set(null, $response_content);
         }
     }
     if (!empty($annotate_config['response'])) {
         $this->setResponseConfig($annotate_config['response']);
     }
     if ($return_response_content) {
         return $response_content;
     } else {
         Response::getInstance()->display($response_content);
     }
     if (isset($annotate_config['after'])) {
         $this->getClassInstanceByName($annotate_config['after']);
     }
     return true;
 }