Example #1
0
 /**
  * 将一个路径路由到指定方法
  *
  * @param string $path 路径
  * @param string $prefix 指定方法所在类的名空间前缀
  * @param string $def_func 当路径中方法名为空时掉用的默认函数
  */
 public static function route($path, $prefix = '', $die = true)
 {
     list($classname, $funcname, $args, $nouse) = self::path_info($path);
     $classname = str_replace('/', '\\', $classname);
     $classname = $prefix . ucwords($classname, '\\');
     try {
         $class = new \ReflectionClass($classname);
         if ($funcname == '' && $class->hasConstant('DEF_FUNC')) {
             $funcname = $class->getConstant('DEF_FUNC');
         }
         $funcname = str_replace('.', '', $funcname);
         if ($funcname == '') {
             if ($die) {
                 Server::show_404();
             }
             return false;
         }
         $funcname = 'c_' . $funcname;
         $func = $class->getMethod($funcname);
         $doc = $func->getDocComment();
         $doc = str_replace(' ', '', $doc);
         if (!preg_match('/^\\*@route(\\(prev=(true|false)\\))?$/im', $doc, $matches)) {
             if ($die) {
                 Server::show_404();
             }
             return false;
         }
         $obj = $class->newInstance();
         if (!isset($matches[2]) || $matches[2] === 'true') {
             if ($class->hasMethod('prev')) {
                 $prev = $class->getMethod('prev');
                 $prev->setAccessible(true);
                 $prev->invoke($obj);
             }
         }
         $func->setAccessible(true);
         $func->invoke($obj, $args);
         if ($die) {
             die;
         }
         return true;
     } catch (Minifw\Exception $ex) {
         if (DEBUG === 1) {
             echo $ex->getMessage();
         }
         if ($die) {
             Server::show_404();
         }
         return false;
     } catch (\Exception $ex) {
         if ($die) {
             Server::show_404();
         }
         return false;
     }
 }
Example #2
0
 /**
  * 读取文件并输出到浏览器
  *
  * @param type $full 文件路径
  * @param type $fsencoding 文件系统的编码,如不为空则会自动进行一些编码转换
  */
 public static function readfile($full, $fsencoding = '')
 {
     $full = self::conv_to($full, $fsencoding);
     if (file_exists($full)) {
         $mtime = filemtime($full);
         Server::show_304($mtime);
         $fi = new \finfo(FILEINFO_MIME_TYPE);
         $mime_type = $fi->file($full);
         header('Content-Type: ' . $mime_type);
         readfile($full);
     } else {
         Server::show_404();
     }
 }
Example #3
0
 /**
  * 分发请求到对应的回调函数
  *
  * @param string $path 请求的路径
  */
 public function dispatch($path)
 {
     foreach ($this->_calls as $v) {
         $matches = [];
         if (preg_match($v['reg'], $path, $matches) === 1) {
             array_shift($matches);
             call_user_func_array($v['callback'], $matches);
             return;
         }
     }
     Minifw\Server::show_404();
 }