Exemplo n.º 1
0
 /**
  * 获取当前请求类型
  *
  * @return string
  */
 private function getRequestType()
 {
     $request_type = 'get';
     if ($this->request->isPostRequest()) {
         $request_type = 'post';
     } elseif ($this->request->isPutRequest()) {
         $request_type = 'put';
     } elseif ($this->request->isDeleteRequest()) {
         $request_type = 'delete';
     }
     return $request_type;
 }
Exemplo n.º 2
0
 /**
  * request response view
  *
  * @param string $property
  * @return Response|Request|View|Config|null
  */
 function __get($property)
 {
     switch ($property) {
         case 'config':
             return $this->config = $this->delegate->getConfig();
         case 'request':
             return $this->request = Request::getInstance();
         case 'response':
             return $this->response = Response::getInstance();
         case 'view':
             return $this->view = $this->initView();
     }
     return null;
 }
Exemplo n.º 3
0
 /**
  * 初始化App配置
  * @param string $app_name
  * @param array $runtime_config
  * @return $this
  * @throws \Cross\Exception\FrontException
  */
 private static function initConfig($app_name, $runtime_config)
 {
     $config = Config::load(APP_PATH_DIR . $app_name . DIRECTORY_SEPARATOR . 'init.php')->parse($runtime_config);
     $request = Request::getInstance();
     $host = $request->getHostInfo();
     $index_name = $request->getIndexName();
     $request_url = $request->getBaseUrl();
     $base_script_path = $request->getScriptFilePath();
     //设置app名称和路径
     $config->set('app', array('name' => $app_name, 'path' => APP_PATH_DIR . $app_name . DIRECTORY_SEPARATOR));
     //静态文件url和绝对路径
     $config->set('static', array('url' => $host . $request_url . '/static/', 'path' => $base_script_path . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR));
     //url相关设置
     $config->set('url', array('index' => $index_name, 'host' => $host, 'request' => $request_url, 'full_request' => $host . $request_url));
     return $config;
 }
Exemplo n.º 4
0
 /**
  * 按类型解析请求字符串
  *
  * @param string $prefix
  * @param array $url_config
  * @return string
  */
 public function getUriRequest($prefix = '/', &$url_config = array())
 {
     $url_config = $this->config->get('url');
     switch ($url_config['type']) {
         case 1:
         case 3:
             $request = Request::getInstance()->getUriRequest('QUERY_STRING', $url_config['rewrite']);
             break;
         case 2:
         case 4:
         case 5:
             $request = Request::getInstance()->getUriRequest('PATH_INFO', $url_config['rewrite']);
             break;
         default:
             $request = '';
     }
     return $prefix . htmlspecialchars(urldecode(ltrim($request, '/')), ENT_QUOTES);
 }
Exemplo n.º 5
0
 /**
  * 根据给定的参数解析文件的绝对路径
  * <pre>
  *  格式如下:
  *  1 file_name 直接指定文件路径
  *  2 ::[path/]file_name 从当前项目根目录查找
  *  3 app::[path/]file_name 当前app路径
  *  4 core::[path/]file_name 核心目录
  * </pre>
  *
  * @param $class
  * @param string $append_file_ext
  * @return array
  */
 static function parseFileRealPath($class, $append_file_ext = '.php')
 {
     $files = $list = array();
     $_defines = array('app' => self::$app_path, 'static' => Request::getInstance()->getScriptFilePath() . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR, 'project' => PROJECT_REAL_PATH);
     if (is_array($class)) {
         $files = $class;
     } elseif (false !== strpos($class, ',')) {
         $files = array_map('trim', explode(',', $class));
     } else {
         $files[] = $class;
     }
     foreach ($files as $f) {
         if (false !== strpos($f, '::')) {
             list($path, $file_info) = explode('::', $f);
             if (!$path) {
                 $path = 'project';
             }
             $file_real_path = $_defines[strtolower($path)] . str_replace('/', DIRECTORY_SEPARATOR, $file_info);
             $file_path_info = pathinfo($file_real_path);
             if (!isset($file_path_info['extension'])) {
                 $file_real_path .= $append_file_ext;
             }
             $list[] = $file_real_path;
         } else {
             $list[] = $f;
         }
     }
     return $list;
 }
Exemplo n.º 6
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;
 }