From 4ef5a0bcba75471921114538199abf6035c298af Mon Sep 17 00:00:00 2001 From: huangdijia Date: Tue, 3 Mar 2015 18:33:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=96=B0=E7=9A=84=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=8A=A0=E8=BD=BD=E6=9C=BA=E5=88=B6=20preg=5Freplace?= =?UTF-8?q?=5Fcallback=E5=8F=96=E4=BB=A3/e=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/org/image.php | 2 +- library/org/oauth.php | 2 +- library/org/parser.php | 2 +- library/org/transform.php | 2 +- library/think/cache.php | 2 +- library/think/config.php | 2 +- library/think/create.php | 6 ++--- library/think/db.php | 2 +- library/think/log.php | 2 +- library/think/session.php | 2 +- library/think/template.php | 47 +++++++++++++++++++++++++------------- library/think/view.php | 2 +- 12 files changed, 44 insertions(+), 29 deletions(-) diff --git a/library/org/image.php b/library/org/image.php index 92c8f591..115e25e1 100644 --- a/library/org/image.php +++ b/library/org/image.php @@ -48,7 +48,7 @@ class Image { */ static public function init($type = 'Gd', $imgname = null){ /* 引入处理库,实例化图片处理对象 */ - $class = '\\Think\\Image\\Driver\\'.ucwords($type); + $class = '\\Think\\Image\\Driver\\'.strtolower($type); self::$im = new $class($imgname); return self::$im; } diff --git a/library/org/oauth.php b/library/org/oauth.php index 48a6b436..48e2d6f9 100644 --- a/library/org/oauth.php +++ b/library/org/oauth.php @@ -34,7 +34,7 @@ class Oauth { * @return object */ static public function connect($type,$options=[]) { - $class = 'Think\\Oauth\\Driver\\'.ucwords($type); + $class = 'think\\oauth\\driver\\'.strtolower($type); self::$handler = new $class($options); return self::$handler; } diff --git a/library/org/parser.php b/library/org/parser.php index 460eb651..996cece6 100644 --- a/library/org/parser.php +++ b/library/org/parser.php @@ -19,7 +19,7 @@ class Parser { // 解析内容 static public function parse($content,$type){ if(!isset(self::$handler[$type])) { - $class = '\\Think\\Parser\\Driver\\'.ucwords($type); + $class = '\\Think\\Parser\\Driver\\'.strtolower($type); self::$handler[$type] = new $class(); } return self::$handler[$type]->parse($content); diff --git a/library/org/transform.php b/library/org/transform.php index 6fd1226c..614b2376 100644 --- a/library/org/transform.php +++ b/library/org/transform.php @@ -23,7 +23,7 @@ class Transform { */ static private function init($type){ if(!isset(self::$handler[$type])) { - $class = '\\Think\\Transform\\Driver\\' . ucwords($type); + $class = '\\Think\\Transform\\Driver\\' . strtolower($type); self::$handler[$type] = new $class(); } } diff --git a/library/think/cache.php b/library/think/cache.php index 4b613a70..d74dc16a 100644 --- a/library/think/cache.php +++ b/library/think/cache.php @@ -27,7 +27,7 @@ class Cache { */ static public function connect($options=[]) { $type = !empty($options['type'])?$options['type']:'File'; - $class = 'think\\cache\\driver\\'.ucwords($type); + $class = 'think\\cache\\driver\\'.strtolower($type); self::$handler = new $class($options); return self::$handler; } diff --git a/library/think/config.php b/library/think/config.php index af99bab1..9195f910 100644 --- a/library/think/config.php +++ b/library/think/config.php @@ -25,7 +25,7 @@ class Config { if(empty($type)) { $type = substr(strrchr($config, '.'),1); } - $class = '\\think\\config\driver\\'.ucwords($type); + $class = '\\think\\config\driver\\'.strtolower($type); self::set((new $class())->parse($config),'',$range); } diff --git a/library/think/create.php b/library/think/create.php index 61d5ce39..5b43994f 100644 --- a/library/think/create.php +++ b/library/think/create.php @@ -47,13 +47,13 @@ class Create { foreach($file as $val){ switch($path) { case 'controller':// 控制器 - $filename = ucwords($val).$path; + $filename = strtolower($val).$path; if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) { file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"config['compile_type']?$this->config['compile_type']:'File'; - $class = '\\think\\template\\driver\\'.ucwords($type); + $class = '\\think\\template\\driver\\'.strtolower($type); $this->storage = new $class(); } @@ -136,7 +136,7 @@ class Template { $this->storage->read($cacheFile,$this->data); // 获取并清空缓存 $content = ob_get_clean(); - if($this->config['cache_id'] && $this->config['display_cache']) { + if(!empty($this->config['cache_id']) && $this->config['display_cache']) { // 缓存页面输出 Cache::set($this->config['cache_id'],$content,$this->config['cache_time']); } @@ -197,7 +197,9 @@ class Template { // 模板解析 $content = $this->parse($content); // 还原被替换的Literal标签 - $content = preg_replace('//eis',"\$this->restoreLiteral('\\1')",$content); + $content = preg_replace_callback('//is', function($matches){ + return $this->restoreLiteral($matches[1]); + },$content); // 添加安全代码 $content = ''.$content; if($this->config['strip_space']) { @@ -230,7 +232,9 @@ class Template { // 检查PHP语法 $content = $this->parsePhp($content); // 首先替换literal标签内容 - $content = preg_replace('/'.$begin.'literal'.$end.'(.*?)'.$begin.'\/literal'.$end.'/eis',"\$this->parseLiteral('\\1')",$content); + $content = preg_replace_callback('/'.$begin.'literal'.$end.'(.*?)'.$begin.'\/literal'.$end.'/is', function($matches){ + return $this->parseLiteral($matches[1]); + },$content); // 获取需要引入的标签库列表 // 标签库只需要定义一次,允许引入多个一次 @@ -259,7 +263,9 @@ class Template { $this->parseTagLib($tag,$content,true); } // 解析普通模板标签 {tagName} - $content = preg_replace('/('.$this->config['tpl_begin'].')([^\d\s'.$this->config['tpl_begin'].$this->config['tpl_end'].'].+?)('.$this->config['tpl_end'].')/eis',"\$this->parseTag('\\2','\\0')",$content); + $content = preg_replace_callback('/('.$this->config['tpl_begin'].')([^\d\s'.$this->config['tpl_begin'].$this->config['tpl_end'].'].+?)('.$this->config['tpl_end'].')/is',function($matches){ + return $this->parseTag($matches[2], $matches[0]); + },$content); return $content; } @@ -326,14 +332,20 @@ class Template { //替换extend标签 $content = str_replace($matches[0],'',$content); // 记录页面中的block标签 - preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->parseBlock('\\1','\\2')",$content); + preg_replace_callback('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/is',function($matches){ + return $this->parseBlock($matches[1],$matches[2]); + },$content); // 读取继承模板 $array = $this->parseXmlAttrs($matches[1]); $content = $this->parseTemplateName($array['name']); // 替换block标签 - $content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"\$this->replaceBlock('\\1','\\2')",$content); + $content = preg_replace_callback('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/is',function($matches){ + return $this->replaceBlock($matches[1],$matches[2]); + },$content); }else{ - $content = preg_replace('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/eis',"stripslashes('\\2')",$content); + $content = preg_replace_callback('/'.$begin.'block\sname=(.+?)\s*?'.$end.'(.*?)'.$begin.'\/block'.$end.'/is',function($matches){ + return stripslashes($matches[2]); + },$content); } return $content; } @@ -439,7 +451,7 @@ class Template { protected function parseTagLib($tagLib,&$content,$hide=false) { $begin = $this->config['taglib_begin']; $end = $this->config['taglib_end']; - $className = '\\think\\template\\taglib\\'.ucwords($tagLib); + $className = '\\think\\template\\taglib\\'.strtolower($tagLib); $tLib = new $className($this); foreach ($tLib->getTags() as $name=>$val){ $tags = [$name]; @@ -457,14 +469,17 @@ class Template { } $n1 = empty($val['attr'])?'(\s*?)':'\s([^'.$end.']*)'; if (!$closeTag){ - $patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/eis'; - $replacement = "\$this->parseXmlTag(\$tLib,'$tagLib','$tag','$1','')"; - $content = preg_replace($patterns, $replacement,$content); + $patterns = '/'.$begin.$parseTag.$n1.'\/(\s*?)'.$end.'/is'; + $content = preg_replace_callback($patterns, function($matches){ + return $this->parseXmlTag($tLib, $tagLib, $tag, $matches[0], $matches[1]); + }, $content); }else{ - $patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/eis'; - $replacement = "\$this->parseXmlTag(\$tLib,'$tagLib','$tag','$1','$2')"; - for($i=0;$i<$level;$i++) - $content=preg_replace($patterns,$replacement,$content); + $patterns = '/'.$begin.$parseTag.$n1.$end.'(.*?)'.$begin.'\/'.$parseTag.'(\s*?)'.$end.'/is'; + for($i=0;$i<$level;$i++){ + $content = preg_replace_callback($patterns, function($matches){ + return $this->parseXmlTag($tLib, $tagLib, $tag, $matches[0], $matches[1]); + }, $content); + } } } } diff --git a/library/think/view.php b/library/think/view.php index bffed796..8c42b684 100644 --- a/library/think/view.php +++ b/library/think/view.php @@ -72,7 +72,7 @@ class View { * @return View */ public function engine($engine, array $config = []){ - $class = '\\think\\view\\driver\\' . ucwords($engine); + $class = '\\think\\view\\driver\\' . strtolower($engine); $this->engine = new $class($config); return $this; }