调整了设置参数的的方法
新增了普通路由模式
pathinfo模式支持伪静态了
pathinfo现在支持用户自定义路由了
tVal = array(); } /** * 设置模版文件目录 * @param string $dir */ public static function setTemplateDir($dir) { self::$tDir = $dir; } /** * 是否实时编译 * @param bool $real */ public static function setReal($real) { self::$real = (bool) $real; } /** * 临时文件目录 * @param string $dir */ public static function setTmpDir($dir) { if (!file_exists($dir)) { if (!mkdir($dir, 0, true)) die("tmp dir $dir can't to mkdir"); } self::$tTmpDir = realpath($dir); } /** * URL调度器 * @param Dispatcher $dispatcher */ public static function setU(&$dispatcher) { if (is_object($dispatcher) && method_exists($dispatcher, 'U')) { self::$uDispatcher = $dispatcher; } } /** * 变量赋值 * @param mixed $name * @param mixed $value */ public function assign($name, $value) { $this->tVal[$name] = $value; } /** * 取得模版的变量 * @param string $name */ public function getVal($name) { if (isset($this->tVal[$name])) { return $this->tVal[$name]; }else return false; } /** * 将运行好后的内容,保存到一个html文件中 * @param string $tFile * @param string $html */ public function saveHtml($tFile, $html) { ob_start(); $this->display($tFile); $buffer = ob_get_contents(); ob_end_clean(); file_put_contents($html, $buffer); } /** * 运行并显示模版内容 * @param string $tfile */ public function display($tFile) { $this->tFile = $this->parseTemplatePath($tFile); if (!self::$real) { if (!file_exists($this->getTmpFile())) $this->parse(); elseif ((filemtime($this->tFile) > filemtime($this->getTmpFile()))) $this->parse(); }else $this->parse(); extract($this->tVal, EXTR_OVERWRITE); include $this->getTmpFile(); } /** * 编译好后的文件 * @return string $filepath */ private function getTmpFile() { $basename = basename($this->tFile); $pos = strrpos($basename, '.'); $tmp = 'tpl_' . substr($basename, 0, $pos) . '.php'; return self::$tTmpDir . '/' . $tmp; } private function parse() { $this->tContent = file_get_contents($this->tFile); $this->parseInclude(); $this->parseSection(); $this->parseVal(); $this->parseEval(); file_put_contents($this->getTmpFile(), $this->tContent); } private function parseInclude() { $this->tContent = preg_replace("/{templates+([a-zA-z0-9._]+)}/ies","$this->subtemplate('$1')", $this->tContent); } /** * 获取只模版 * @param string $file */ private function subtemplate($file) { return file_get_contents($this->parseTemplatePath($file)); } /** * 解析模版路径 * @param string $file * @return string $filepath */ private function parseTemplatePath($tFile) { $tFile.='.html'; $tFile = self::$tDir ? self::$tDir . '/' . $tFile : $tFile; if (!file_exists($tFile)) { die("No template file $tFile"); } else { $tFile = realpath($tFile); } return $tFile; } /** * 解析变量 */ private function parseVal() { $this->tContent = preg_replace("/{(\$S+?)}/is","", $this->tContent); } /** * 解析段落 */ private function parseSection() { //逻辑 $this->tContent = preg_replace("/{elseifs+(.+?)}/ies","$this->stripvtags('','')", $this->tContent); $this->tContent = preg_replace("/{else}/is","", $this->tContent); $this->tContent = preg_replace("/{U((.+?))}/ies","$this->parseUrl('$1')", $this->tContent); //循环 for ($i = 0; $i < 6; $i++) { $this->tContent = preg_replace("/{loops+(S+)s+(S+)}(.+?){/loop}/ies","$this->stripvtags('','\3')", $this->tContent); $this->tContent = preg_replace("/{loops+(S+)s+(S+)s+(S+)}(.+?){/loop}/ies","$this->stripvtags(' \3) { ?>','\4')", $this->tContent); $this->tContent = preg_replace("/{ifs+(.+?)}(.+?){/if}/ies","$this->stripvtags('','\2')", $this->tContent); } } private function stripvtags($expr, $statement='') { $expr = str_replace("\"",""", preg_replace("/=(\$.+?)?>/s","\1", $expr)); $statement = str_replace("\"",""", $statement); return $expr . $statement; } /** * 解析PHP语句 */ private function parseEval() { $this->tContent = preg_replace("/{evals+(.+?)}/is","", $this->tContent); } /** * 解析URL */ private function parseUrl($url) { if (is_object(self::$uDispatcher)) { return self::$uDispatcher->U($url); } else { return $url; } } } ?>
查找模块目录的位置 * DEFAULT_MODULE=>默认Module * DEFAULT_ACTION=>默认Action * DEBUG=>开启调试(true|false) * URL_MODEL=>路由模式(0:普通模式,1:pathinfo模式) * URL_DELIMITER=>参数分隔符 pathinfo模式使用 * URL_HTML_SUFFIX=>'文件后缀' pathinfo模式伪静态使用 * ENTRY_INDEX=>入口文件 * URL_ROUTER_ON=>开启自定义路由 * 普通URL模式U(模块名/操作名?参1=值1&参2=值2) * 路由模式U(路由名@?参1=值1&参2=值2) */ class Dispatcher { private static $instance; private static $_SGLOBAL; //调度配置 private static $route = array(); //泛路由 private function __construct() { self::initConfig(); } public static function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance; } private function __clone() { } /** * 运行控制器 */ public function run() { $route = array(); if (self::$_SGLOBAL['URL_MODEL'] == 1) { $route = $this->pathInfoRoute(); } else { $route = $this->generalRoute(); } $modulefile = self::$_SGLOBAL['MODULE_PATH'] ."/{$route['module']}.class.php"; if (file_exists($modulefile)) { include $modulefile; if (class_exists($route['module'])) { $class = new $route['module']; if (method_exists($class, $route['action'])) { call_user_func(array(&$class, $route['action'])); }else die("in {$route['module']} module no this {$route['action']} action"); }else die("no this {$route['module']} module"); }else { die("no this {$route['module']} module"); } self::$_SGLOBAL['endtime'] = microtime(true); $this->debugInfo(); } /** * 输出调试信息 */ private function debugInfo() { if (self::$_SGLOBAL['DEBUG']) { $exectime = self::$_SGLOBAL['endtime'] - self::$_SGLOBAL['starttime']; $debuginfo = << .dispatcher_debug_table th,.dispatcher_debug_table td{padding:5px;} .dispatcher_debug_table th{ border-top:1px solid red; border-left:1px solid red; background-color:#ccc; } .dispatcher_debug_table td{ border-top:1px solid red; border-left:1px solid red; border-right:1px solid red; } .dispatcher_debug_table_last td,.dispatcher_debug_table_last th{ border-bottom:1px solid red; } .dispatcher_debug_table_title{border-right:1px solid red;}
Debug Info | |
---|---|
Execute Time | $exectime s |
Include File |
HTML;
foreach (get_included_files () as $file) {
$debuginfo.=$file ." "; } $debuginfo.=" |
Server Info | ";
$debuginfo.="Host:". $_SERVER['HTTP_HOST'] ." "; $debuginfo.="PHP_Version:". PHP_VERSION ." "; $debuginfo.="Server_Version:". $_SERVER['SERVER_SOFTWARE'] ." "; $debuginfo.=" |
Client Info | ";
$debuginfo.="Remote_Addr:". $_SERVER['REMOTE_ADDR'] ." "; $debuginfo.="User_Agent:". $_SERVER['HTTP_USER_AGENT'] ." "; $debuginfo.=" |
1, 'URL_MODEL' => 1, 'URL_DELIMITER' => '/', 'DEFAULT_ACTION' => 'home', 'URL_REWRITE' => 1, 'URL_ROUTER_ON' => true, 'URL_HTML_SUFFIX'=>'.html'); //自定义泛路由 $router = array('space'=>array('Space', 'index','uid')); $dispatcher->setOption($option); $dispatcher->setRoute($router); Template::setU($dispatcher); Template::setReal(true); Template::setTemplateDir('template/default'); Template::setTmpDir('runtime/tpl'); $dispatcher->run(); ?>