使用新的自动加载机制

preg_replace_callback取代/e模式
This commit is contained in:
huangdijia
2015-03-03 18:33:47 +08:00
parent 9e44381f6f
commit 4ef5a0bcba
12 changed files with 44 additions and 29 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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',"<?php\nnamespace {$module}\\{$path};\nclass {$filename} {\n}");
}
break;
case 'model': // 模型
$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',"<?php\nnamespace {$module}\\{$path};\nclass {$filename} extends \Think\Model{\n}");
}
@@ -61,7 +61,7 @@ class Create {
case 'view': // 视图
break;
default:
$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',"<?php\nnamespace {$module}\\{$path};\nclass {$filename} {\n}");
}

View File

@@ -33,7 +33,7 @@ class Db {
// 解析连接参数 支持数组和字符串
$options = self::parseConfig($config);
// 如果采用lite方式 仅支持原生SQL 包括query和execute方法
$class = $lite? 'think\db\Lite' : 'think\\db\\driver\\'.ucwords($options['type']);
$class = $lite? 'think\db\lite' : 'think\\db\\driver\\'.strtolower($options['type']);
self::$instance[$md5] = new $class($options);
}
self::$_instance = self::$instance[$md5];

View File

@@ -20,7 +20,7 @@ class Log {
// 日志初始化
static public function init($config=[]){
$type = isset($config['type'])?$config['type']:'File';
$class = '\\think\\log\\driver\\'. ucwords($type);
$class = '\\think\\log\\driver\\'. strtolower($type);
unset($config['type']);
self::$storage = new $class($config);
}

View File

@@ -65,7 +65,7 @@ class Session {
session_cache_expire($config['cache_expire']);
}
if(!empty($config['type'])) { // 读取session驱动
$class = '\\think\\session\\driver\\'. ucwords(strtolower($config['type']));
$class = '\\think\\session\\driver\\'. strtolower($config['type']);
// 检查驱动类
session_set_save_handler(new $class());
}

View File

@@ -60,7 +60,7 @@ class Template {
// 初始化模板编译存储器
$type = $this->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('/<!--###literal(\d+)###-->/eis',"\$this->restoreLiteral('\\1')",$content);
$content = preg_replace_callback('/<!--###literal(\d+)###-->/is', function($matches){
return $this->restoreLiteral($matches[1]);
},$content);
// 添加安全代码
$content = '<?php if (!defined(\'THINK_PATH\')) exit();?>'.$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);
}
}
}
}

View File

@@ -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;
}