1 Star 0 Fork 0

lloyd/router

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
crouter.php 2.69 KB
一键复制 编辑 原始数据 按行查看 历史
lloyd 提交于 2015-10-11 02:34 +08:00 . update the Declaration function
<?php
/**
* @author Lloyd Zhou (lloydzhou@qq.com)
* compile the Router to source code, the $_tree, $_error and $_hook is plain array.
* no need to add so many callback handlers, can save time.
*/
include ("router.php");
class CRouter extends Router {
protected $target = null;
protected $debug = null;
protected $compile = null;
/* helper function to get the compile status*/
protected function needComplie(){
if (null === $this->compile){
$this->compile = ($this->debug || !file_exists($this->target)
|| @filemtime($_SERVER['SCRIPT_FILENAME'])>@filemtime($this->target));
}
return $this->compile;
}
/* helper function to dump callback handlers into plain array source code.*/
protected function export ($var, $return=false){
if ($var instanceof Closure){ /* dump anonymous function in to plain code.*/
$ref = new ReflectionFunction($var);
$file = new SplFileObject($ref->getFileName());
$file->seek($ref->getStartLine()-1);
$result = '';
while ($file->key() < $ref->getEndLine()){
$result .= $file->current();
$file->next();
}
$begin = strpos($result, 'function');
$end = strrpos($result, '}');
$result = substr($result, $begin, $end - $begin + 1);
} elseif (is_object($var)){ /* dump object with construct function. */
$result = 'new '. get_class($var). '('. $this->export(get_object_vars($var), true). ')';
} elseif (is_array($var)) { /* dump array in plain array.*/
$array = array ();
foreach($var as $k=>$v) $array[] = var_export($k, true).' => '. $this->export($v, true);
$result = 'array('. implode(', ', $array). ')';
} else $result = var_export($var, true);
if (!$return) print $result;
return $result;
}
/* construct function */
public function __construct($target=null, $debug=false){
$this->debug = $debug;
$this->target = $target?$target:$_SERVER['SCRIPT_FILENAME'].'c';
}
/**
* compile router into source code, and execute the compiled source code with parameters.
*/
public function execute($params=array(), $method=null, $path=null){
if ($this->needComplie()){
$code = "<?php\nreturn new Router("
. $this->export($this->_tree, true). ', '
. $this->export($this->_error, true). ', '
. $this->export($this->_hook, true). ');';
file_put_contents($this->target, $code);
}
$router = include ($this->target);
$router->execute($params, $method, $path);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/lloyd/router.git
git@gitee.com:lloyd/router.git
lloyd
router
router
master

搜索帮助