Sfoglia il codice sorgente

Update: 将加载修改为单例或不限制

zjh 3 settimane fa
parent
commit
956fe5a615

+ 5 - 5
src/Application.php

@@ -82,7 +82,7 @@ class Application
 
     public function __construct()
     {
-        $this->helper = Psr4::getInstance()->loadClass('\Qii\Autoloader\Helper');
+        $this->helper = Psr4::getInstance()->loadClassOnce('\Qii\Autoloader\Helper');
         self::$app = & $this;
     }
 
@@ -345,7 +345,7 @@ class Application
         if ($this->request !== null) {
             return $this->request;
         }
-        return Psr4::getInstance()->loadClass('\Qii\Request\Http');
+        return Psr4::getInstance()->loadClassOnce('\Qii\Request\Http');
     }
     /**
      * Set Dispatcher
@@ -356,7 +356,7 @@ class Application
         if ($this->dispatcher !== null) {
             return $this->dispatcher;
         }
-        return Psr4::getInstance()->loadClass('\Qii\Base\Dispatcher');
+        return Psr4::getInstance()->loadClassOnce('\Qii\Base\Dispatcher');
     }
     /**
      * 设置写loger的类
@@ -436,7 +436,7 @@ class Application
         if (!$policy && isset($viewConfigure[$engine])) {
             $policy = array_merge($policy, (array) $viewConfigure[$engine]);
         }
-        $viewEngine = Psr4::getInstance()->loadClass('\Qii\View\Loader');
+        $viewEngine = Psr4::getInstance()->loadClassOnce('\Qii\View\Loader');
         $viewEngine->setView($engine, $policy);
         return $viewEngine;
     }
@@ -552,7 +552,7 @@ class Application
         $reserve = array_unique(array_reverse($this->middleware));
         $next = array_reduce($reserve,function ($carry, $item){
             return function () use ($carry, $item){
-                return _loadClass($item)->handle(\Qii::getInstance()->request, $carry);
+                return _loadClassOnce($item)->handle(\Qii::getInstance()->request, $carry);
             };
         }, function(){
             return true;

+ 61 - 7
src/Autoloader/Psr4.php

@@ -300,6 +300,8 @@ class Psr4
      */
     public function getClassName($class)
     {
+        //去掉第一个斜杠
+        $class = preg_replace("/^\\\\/", "", $class);
         // the current namespace prefix
         //replace "_" to "\" use common method to load class
         $class = str_replace("_", "\\", $class);
@@ -324,8 +326,6 @@ class Psr4
         if (count($args) == 0) {
             return;
         }
-        //去掉第一个斜杠
-        $class = preg_replace("/^\\\\/", "", $class);
         $class = $this->getClassName($class);
 
         if (class_exists($class, false)) {
@@ -338,6 +338,31 @@ class Psr4
         throw new ClassNotFound(\Qii::i(1103, $class), __LINE__);
     }
 
+    /**
+     * 只载入一次
+     *
+     * @param $class
+     * @return mixed|void
+     * @throws ClassNotFound
+     * @throws FileNotFound
+     */
+    public function loadClassOnce($class) {
+        $args = func_get_args();
+        if (count($args) == 0) {
+            return;
+        }
+        //去掉第一个斜杠
+        $class = $this->getClassName($class);
+
+        if (class_exists($class, false)) {
+
+            return call_user_func_array(array($this, 'instanceOnce'), $args);
+        }
+        if ($this->loadFileByClass($class)) {
+            return call_user_func_array(array($this, 'instanceOnce'), $args);
+        }
+        throw new ClassNotFound(\Qii::i(1103, $class), __LINE__);
+    }
     /**
      * 调用静态的方法
      * @param string $class 类名
@@ -452,6 +477,29 @@ class Psr4
         return \Qii\Autoloader\Import::requires($file);
     }
 
+    /**
+     * 初始化一次
+     *
+     * @return array|mixed|object|null
+     * @throws \ReflectionException
+     */
+    public function instanceOnce(){
+        $args = func_get_args();
+        $class = array_shift($args);
+        $className = $this->getClassName($class);
+        //如果实例化的参数发生变化,就重新实例化
+        $paramsHash = md5(print_r($args, true));
+        if (isset(self::$_loadedClass[$className .'-'. $paramsHash])) return self::$_loadedClass[$className  .'-'. $paramsHash];
+        self::$_loadedClassParams[$className][] = $paramsHash;
+        $refClass = new \ReflectionClass($className);
+        self::$_loadedClass[$className .'-'. $paramsHash] = $instance = $refClass->newInstanceArgs($args);
+        //如果有_initialize方法就自动调用_initialize方法,并将参数传递给_initialize方法
+        if ($refClass->hasMethod('_initialize')) {
+            call_user_func_array(array($instance, '_initialize'), $args);
+        }
+        return $instance;
+    }
+
     /**
      * instance class
      * @param string $class
@@ -463,20 +511,26 @@ class Psr4
         $class = array_shift($args);
         $className = $this->getClassName($class);
         //如果实例化的参数发生变化,就重新实例化
-        $paramsHash = md5(print_r($args, true));
+        /*$paramsHash = md5(print_r($args, true));
         if (isset(self::$_loadedClass[$className .'-'. $paramsHash])) return self::$_loadedClass[$className  .'-'. $paramsHash];
-        self::$_loadedClassParams[$className][] = $paramsHash;
+        self::$_loadedClassParams[$className][] = $paramsHash;*/
 
         if (!class_exists($className, false)) {
             throw new CallUndefinedClass(\Qii::i('1105', $className), __LINE__);
         }
-        $refClass = new \ReflectionClass($className);
-        self::$_loadedClass[$className .'-'. $paramsHash] = $instance = $refClass->newInstanceArgs($args);
+        $class = new $className(...$args);
+        if(method_exists($class, '_initialize')) {
+            call_user_func_array(array($class, '_initialize'), $args);
+        }
+        return $class;
+        /*$refClass = new \ReflectionClass($className);
+        //self::$_loadedClass[$className .'-'. $paramsHash] =
+        $instance = $refClass->newInstanceArgs($args);
         //如果有_initialize方法就自动调用_initialize方法,并将参数传递给_initialize方法
         if ($refClass->hasMethod('_initialize')) {
             call_user_func_array(array($instance, '_initialize'), $args);
         }
-        return $instance;
+        return $instance;*/
     }
 
     /**

+ 3 - 3
src/Base/Controller.php

@@ -74,8 +74,8 @@ abstract class Controller
 
     public function __construct()
     {
-        $this->load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
-        $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
+        $this->load = Psr4::getInstance()->loadClassOnce('\Qii\Autoloader\Loader');
+        $this->request = Psr4::getInstance()->loadClassOnce('\Qii\Request\Http');
         $this->controllerId = $this->request->controller;
         $this->actionId = $this->request->action;
         $this->language = Factory::getInstance('\Qii\Language\Loader');
@@ -126,7 +126,7 @@ abstract class Controller
      */
     final public function enableDB()
     {
-        return $this->db = _loadClass('\Qii\Driver\Model');
+        return $this->db = _loadClassOnce('\Qii\Driver\Model');
     }
     
     /**

+ 3 - 3
src/Base/Dispatcher.php

@@ -96,7 +96,7 @@ class Dispatcher
         }
         array_unshift($funcArgs, $controllerName);
         $psr4 = Psr4::getInstance();
-        $this->controllerCls = call_user_func_array(array($psr4, 'loadClass'), $funcArgs);
+        $this->controllerCls = call_user_func_array(array($psr4, 'loadClassOnce'), $funcArgs);
         //load beforeRun 不放到具体的Controller里边执行,要不这里的属性controllerCls获取不到值
         //step beforeRun -> middleware -> initialization -> run
         if(method_exists($this->controllerCls, 'beforeRun') && is_callable(array($this->controllerCls, 'beforeRun'))) {
@@ -106,7 +106,7 @@ class Dispatcher
         $this->getMiddleWare($this->controllerCls)->gatherMiddleware();
         $next = array_reduce($this->requestMiddleWare,function ($carry, $item){
             return function () use ($carry, $item){
-                return _loadClass($item)->handle(\Qii::getInstance()->request, $carry);
+                return _loadClassOnce($item)->handle(\Qii::getInstance()->request, $carry);
             };
         }, function(){
             return true;
@@ -132,7 +132,7 @@ class Dispatcher
             && $this->controllerCls->actions[$action]) {
             $actionArgs = array();
             $actionArgs[] = $this->controllerCls->actions[$action];
-            $actionCls = call_user_func_array(array($psr4, 'loadClass'), $actionArgs);
+            $actionCls = call_user_func_array(array($psr4, 'loadClassOnce'), $actionArgs);
             $this->actionCls = $actionCls;
             $this->actionCls->setRequest($this->request);
             $this->actionCls->controller = $this->controllerCls;

+ 3 - 3
src/Base/Route.php

@@ -355,7 +355,7 @@ class Route
         //获取全局middleware
         return array_reduce($middleware, function ($carry, $item){
             return function () use ($carry, $item){
-                return _loadClass($item)->handle(\Qii::getInstance()->request, $carry);
+                return _loadClassOnce($item)->handle(\Qii::getInstance()->request, $carry);
             };
         }, function(){
             return true;
@@ -393,7 +393,7 @@ class Route
                 // $route->any('/default/*', ['class', '$2']); 这里的$2值的是request->get(2)这个值
                 if(is_array($callable)) {
                     $action = (isset($callable[1]) ? $callable[1] : Register::get("APP_DEFAULT_ACTION"));
-                    $controllerCls = _loadClass($callable[0]);
+                    $controllerCls = _loadClassOnce($callable[0]);
                 }else{
                     if(strpos($callable, ":") > 0) {
                         $split = explode(":", $callable);
@@ -402,7 +402,7 @@ class Route
                     }else{
                         $action = Register::get("APP_DEFAULT_ACTION");
                     }
-                    $controllerCls = _loadClass($callable);
+                    $controllerCls = _loadClassOnce($callable);
                 }
 
                 preg_match('/\$\d$/', $action, $match);

+ 2 - 2
src/Base/Rules.php

@@ -71,7 +71,7 @@ class Rules
 
     public function __construct()
     {
-        $this->validate = \_loadClass('\Qii\Library\Validate');
+        $this->validate = \_loadClassOnce('\Qii\Library\Validate');
         $this->constants();
         $this->clean();
     }
@@ -435,7 +435,7 @@ class Rules
     {
         $data = array();
         $data['data'] = array();
-        $valid = _loadClass('Qii\Library\Validate');
+        $valid = _loadClassOnce('Qii\Library\Validate');
         foreach($this->data AS $key => $val)
         {
             $rule = $this->get($key);

+ 1 - 1
src/Cache/Loader.php

@@ -27,7 +27,7 @@ class Loader
      */
     public function initialization($policy)
     {
-        return Psr4::getInstance()->loadClass('Qii\Cache\\' . ucwords($this->cache), $policy);
+        return Psr4::getInstance()->loadClassOnce('Qii\Cache\\' . ucwords($this->cache), $policy);
     }
 
     /**

+ 1 - 1
src/Config/Setting.php

@@ -39,7 +39,7 @@ class Setting
      */
     public function setDefaultLanguage()
     {
-        $this->language = Psr4::getInstance()->loadClass('\Qii\Language\Loader');
+        $this->language = Psr4::getInstance()->loadClassOnce('\Qii\Language\Loader');
         //加载语言包
         $this->language->load('error', QII_DIR);
         $this->language->load('exception', QII_DIR);

+ 30 - 5
src/Driver/Base.php

@@ -59,6 +59,8 @@ class Base
      */
     public $executeSQL = '';
 
+    public $fetchSQL = false;
+
     public $operateVal = array('or', 'and', 'like');//连接条件操作;
     public $shortExpression = array(
         'equal' => "%s`%s` = '%s'",
@@ -91,8 +93,8 @@ class Base
 
     public function __construct()
     {
-        $this->language = Psr4::getInstance()->loadClass('Qii\Language\Loader');
-        $this->load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
+        $this->language = Psr4::getInstance()->loadClassOnce('Qii\Language\Loader');
+        $this->load = Psr4::getInstance()->loadClassOnce('\Qii\Autoloader\Loader');
         $this->response = new Response();
     }
     /**
@@ -110,6 +112,9 @@ class Base
             $this->where($where);
         }
         $sql = $this->selectSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $all = $this->getAll($sql);
         $this->endTime($sql);
@@ -122,6 +127,9 @@ class Base
     final function rs($table)
     {
         $sql = $this->selectSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $rs = $this->setQuery($sql);
         $this->endTime($sql);
@@ -141,6 +149,9 @@ class Base
             $this->where($where);
         }
         $sql = $this->selectSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $row = $this->getRow($sql);
         $this->endTime($sql);
@@ -173,6 +184,9 @@ class Base
             throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
         }
         $sql = $this->selectSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $one = $this->getOne($sql);
         $this->endTime($sql);
@@ -197,6 +211,9 @@ class Base
     final function insertObject($table, $dataArray)
     {
         $sql = $this->insertSQL($table, $dataArray);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $this->setQuery($sql);
         $this->endTime($sql);
@@ -212,6 +229,9 @@ class Base
     final function replaceObject($table, $dataArray)
     {
         $sql = $this->replaceSQL($table, $dataArray);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $rs = $this->setQuery($sql);
         $this->setError();
@@ -228,6 +248,9 @@ class Base
     public function update($table)
     {
         $sql = $this->updateSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $res = $this->exec($sql);
         $this->endTime($sql);
@@ -256,6 +279,9 @@ class Base
     final function delete($table)
     {
         $sql = $this->deleteSQL($table);
+        if($this->fetchSQL) {
+            return $sql;
+        }
         $this->startTime($sql);
         $res = $this->exec($sql);
         $this->endTime($sql);
@@ -531,8 +557,8 @@ class Base
      * @param string $sql
      * @return $this
      */
-    public function fetchSql(&$sql) {
-        $this->executeSQL = &$sql;
+    public function fetchSQL($fetch = false) {
+        $this->fetchSQL = $fetch;
         return $this;
     }
     /**
@@ -640,7 +666,6 @@ class Base
             else if($count - 1 == $index)
             {
                 $tmpWhere[$index] = $val;
-                print_r($tmpWhere);
                 $slices[] = array_values($tmpWhere);
                 $tmpWhere = array();
             }

+ 47 - 20
src/Driver/Entity/Base.php

@@ -21,7 +21,7 @@ class Base {
     /**
      * @var Model $db
      */
-    public static $db = null;
+    public $db = null;
     /**
      * 主键
      * @var mix $privateKeys null
@@ -83,6 +83,8 @@ class Base {
      */
     public $__Error;
 
+    public $fetchSQL = false;
+
     public function __construct(){
         $this->cleanVars();
     }
@@ -90,13 +92,14 @@ class Base {
     /**
      * 初始化的时候清除所有静态变量的值
      *
-     * @return void
+     * @return $this
      */
     final public function cleanVars() {
         $this->properties = [];
         $this->uniqKeys = null;
         $this->privateKeys = null;
         $this->exclude = [];
+        return $this;
     }
     /**
      * 默认值,仅在新增数据的时候支持,更新不支持
@@ -112,7 +115,7 @@ class Base {
      * @return $this
      */
     public function setTableName($tableName) {
-        if(is_callable($tableName)) {
+        if(is_callable($tableName, true)) {
             $this->tableName = $tableName();
         }
         if($tableName) $this->tableName = $tableName;
@@ -210,7 +213,7 @@ class Base {
         $or = [];
         if(isset($this->hooker['or'])) {
             foreach ($this->hooker['or'] as $value) {
-                if(is_callable($value['func'])) {
+                if(is_callable($value['func'], true)) {
                     $or[] = call_user_func($value['func'], [], $value['args']);
                 }
             }
@@ -272,7 +275,7 @@ class Base {
         }
         // 遍历
         foreach ($this->hooker['where'] as $item) {
-            if(is_callable($item['func'])) {
+            if(is_callable($item['func'], true)) {
                 $subWhere = call_user_func($item['func'], $where, $item['args']);
                 foreach ($subWhere as $key => $value) {
                     if(!preg_match("/^[0-9]/", $key) && is_array($value) && count($value, 1) != count($value)) {
@@ -346,7 +349,7 @@ class Base {
         $join = [];
         if(isset($this->hooker['join'])) {
             foreach ($this->hooker['join'] as $value) {
-                if(is_callable($value['func'])) {
+                if(is_callable($value['func'], true)) {
                     $join[] = call_user_func($value['func'], [], $value['args']);
                 }
             }
@@ -441,7 +444,7 @@ class Base {
      * @return int|mixed
      */
     public function getLimitHooker() {
-        if(isset($this->hooker['limit']) && is_callable($this->hooker['limit']['func'])) {
+        if(isset($this->hooker['limit']) && is_callable($this->hooker['limit']['func'], true)) {
             return call_user_func($this->hooker['limit']['func'], [], $this->hooker['limit']['args']);
         }
         return [];
@@ -479,7 +482,7 @@ class Base {
      * @return mixed|null[]
      */
     public function getCacheHooker() {
-        if(isset($this->hooker['cache']) && is_callable($this->hooker['cache']['func'])) {
+        if(isset($this->hooker['cache']) && is_callable($this->hooker['cache']['func'], true)) {
             return call_user_func($this->hooker['cache']['func'], [], $this->hooker['cache']['args']);
         }
         return [null, null, null];
@@ -549,7 +552,7 @@ class Base {
         $callback = [];
         if(isset($this->hooker['with'])) {
             foreach ($this->hooker['with'] AS $hooker) {
-                if(is_callable($hooker['func'])) {
+                if(is_callable($hooker['func'], true)) {
                     $callback[] = call_user_func($hooker['func'], [], $hooker['args']);
                 }
             }
@@ -564,7 +567,7 @@ class Base {
      * @throws \Exception
      */
     private function checkCallable($hooker) {
-        if($hooker && !is_callable($hooker)) {
+        if($hooker && !is_callable($hooker, true)) {
             throw new \Exception('Hooker is uncallable');
         }
     }
@@ -575,7 +578,7 @@ class Base {
      * @throws \Exception
      */
     public function setQueryFieldsHooker($hooker, $args = null) {
-        if(!is_callable($hooker)) {
+        if(!is_callable($hooker, true)) {
             throw new \Exception('Hooker is un callable');
         }
         $this->setHooker('fields', $hooker, $args);
@@ -587,7 +590,7 @@ class Base {
      * @return mixed|string
      */
     public function getFieldsHooker() {
-        if(isset($this->hooker['fields']) && is_callable($this->hooker['fields']['func'])) {
+        if(isset($this->hooker['fields']) && is_callable($this->hooker['fields']['func'], true)) {
             return call_user_func($this->hooker['fields']['func'], [], $this->hooker['fields']['args']);
         }
         return '*';
@@ -611,10 +614,11 @@ class Base {
      * @return \Qii\Driver\Base
      */
     public function db() {
-        if(self::$db !== null && self::$db instanceof Model) {
-            return self::$db;
+        if($this->db !== null && $this->db instanceof Model) {
+            return $this->db;
         }
-        return _loadClass('\Qii\Driver\Model');
+        $this->db = _loadClass('\Qii\Driver\Model');
+        return $this->db;
     }
 
     /**
@@ -628,14 +632,14 @@ class Base {
         if(!($db instanceof \Qii\Driver\Model)) {
             throw new \Exception('DB is illegal');
         }
-        self::$db = $db;
+        $this->db = $db;
     }
     /**
      * 返回 entity 信息
      * @return \Qii\Driver\Entity\Entity
      */
     public function entity() {
-        return _loadClass('\Qii\Driver\Entity\Entity');
+        return _loadClassOnce('\Qii\Driver\Entity\Entity');
     }
     /**
      * 设置查询字段
@@ -1499,6 +1503,16 @@ class Base {
     final public function endRecord(){
         $this->db()->endRecord();
     }
+
+    /**
+     * 是否只返回SQL
+     * @param $fetch
+     * @return $this
+     */
+    final public function fetchSQL($fetch = false) {
+        $this->fetchSQL = $fetch;
+        return $this;
+    }
     /**
      * 获取where及or条件并创建查询条件
      *
@@ -1508,7 +1522,7 @@ class Base {
         if(empty($fields)) {
             $fields = $this->getFields();
         }
-        $query = $this->db()->fields($fields)->where($this->getWhereHooker())->fetchSql($this->calledSQL);
+        $query = $this->db()->fields($fields)->where($this->getWhereHooker())->fetchSQL($this->fetchSQL);
         $or = $this->getOrHooker();
         foreach ($or as $v) {
             $query = $query->orTerms($v);
@@ -1663,7 +1677,6 @@ class Base {
         }
         return $query->rs($this->prepareTable());
     }
-
     /**
      * select rows
      * @param int $page 页码
@@ -1688,6 +1701,20 @@ class Base {
         return $lists;
     }
 
+    /**
+     * 获取一列
+     *
+     * @return array|false
+     * @throws \Qii\Exceptions\TableException
+     */
+    final public function getOne() {
+        $query = $this->createQuery();
+        $query = $query->groupBy($this->getGroupBy());
+        $query = $query->orderBy($this->getOrderBy());
+        $query->limit(0, 1);
+        return $query->selectOne($this->prepareTable());
+    }
+
     /**
      * 获取一行
      *
@@ -2056,7 +2083,7 @@ class Base {
     public function getOrderBy() {
         $order = array();
         $orderBy = $this->orderBy();
-        if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'])) {
+        if(isset($this->hooker['order']) && is_callable($this->hooker['order']['func'], true)) {
             $orderBy = call_user_func($this->hooker['order']['func'], $order, $this->hooker['order']['args']);
         }
         foreach ($orderBy as $key => $val) {

+ 1 - 1
src/Driver/Entity/Entity.php

@@ -24,7 +24,7 @@ class Entity {
     }
 
     public function db() {
-        return _loadClass('\Qii\Driver\Model');
+        return _loadClassOnce('\Qii\Driver\Model');
     }
     /**
      * 设置 entity 的名字空间

+ 6 - 6
src/Driver/Model.php

@@ -78,10 +78,10 @@ class Model
         if(!is_array($dbInfo)) {
             throw new Variable('配置信息必须为数组');
         }
-        $this->_load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
-        $this->_language = Psr4::getInstance()->loadClass('\Qii\Language\Loader');
-        $this->_request = Psr4::getInstance()->loadClass('Qii\Request\Http');
-        $this->_helper = Psr4::getInstance()->loadClass('Qii\Autoloader\Helper');
+        $this->_load = Psr4::getInstance()->loadClassOnce('\Qii\Autoloader\Loader');
+        $this->_language = Psr4::getInstance()->loadClassOnce('\Qii\Language\Loader');
+        $this->_request = Psr4::getInstance()->loadClassOnce('Qii\Request\Http');
+        $this->_helper = Psr4::getInstance()->loadClassOnce('Qii\Autoloader\Helper');
         $dbInfo = Register::mergeAppConfigure(Register::getAppConfigure(Consts::APP_DB), $dbInfo);
         if (isset($this->_dbInfo['driver'])) {
             $this->_driver = $this->_dbInfo['driver'];
@@ -96,9 +96,9 @@ class Model
             QII_DIR . DS . 'Qii' . DS . 'Driver' . DS . ucWords($this->_driver) . DS . 'Connection.php',
             QII_DIR . DS . 'Qii' . DS . 'Driver' . DS . ucWords($this->_driver) . DS . 'Driver.php',
         ));
-        $this->db = Psr4::getInstance()->loadClass(
+        $this->db = Psr4::getInstance()->loadClassOnce(
             '\Qii\Driver\\' . ucWords($this->_driver) . '\Driver',
-            Psr4::getInstance()->loadClass(
+            Psr4::getInstance()->loadClassOnce(
                 '\Qii\Driver\\' . ucWords($this->_driver) . '\Connection', $dbInfo
             )
         );

+ 9 - 0
src/Driver/Traits/SQL.php

@@ -270,6 +270,15 @@ trait SQL
         if (ini_get("magic_quotes_gpc")) {
             return $word;
         }
+        if(is_array($word)) {
+            foreach ($word as $k => $v) {
+                $word[$k] = addslashes($v);
+            }
+            return $word;
+        }
+        if(is_callable($word, false)) {
+            return $word($this);
+        }
         $this->handleError(in_array(gettype($word), array("object", "resource","resource (closed)")), '期待参数为数组或字符串,获取到的是:'. gettype($word)."(". json_encode($word) .")");
         if(is_array($word) && count($word) !=  count($word, 1)) {
             return $word;

+ 13 - 3
src/Functions/Funcs.php

@@ -119,18 +119,19 @@ function _loader($class = null)
 {
 	$args = func_get_args();
 	if($class != null){
-		return call_user_func_array(array(Psr4::getInstance(), 'loadClass'), $args);
+		return call_user_func_array(array(Psr4::getInstance(), 'loadClassOnce'), $args);
 	}
 	return Psr4::getInstance();
 }
 
+
 /**
  * load library
  * @param $name
  * @return mixed
  */
 function _library($name) {
-    return \_loadClass("\Qii\Library\\". $name);
+    return \_loadClassOnce("\Qii\Library\\". $name);
 }
 /**
  * 简便的loadClass方法
@@ -142,6 +143,15 @@ function _loadClass()
 	return call_user_func_array(array(\_loader(), 'loadClass'), $args);
 }
 
+/**
+ * 简便的loadClassOnce方法
+ * \Qii\Autoloader\Psr4::getInstance()->loadClassOnce(.., ..);
+ */
+function _loadClassOnce()
+{
+    $args = func_get_args();
+    return call_user_func_array(array(\_loader(), 'loadClassOnce'), $args);
+}
 /**
  * 根据文件前缀获取文件路径
  *
@@ -162,7 +172,7 @@ function _getFileByPrefix($file)
  */
 function _DBDriver(\Qii\Driver\Rules $rule, $privateKey = null, $fieldsVal = null)
 {
-    $rules = _loadClass('Qii\Driver\Easy')->_initialize();
+    $rules = _loadClassOnce('Qii\Driver\Easy')->_initialize();
     if ($privateKey) $rules->setPrivateKey($privateKey);
     $rules->setRules($rule);
     if ($fieldsVal) $rules->setFieldsVal($fieldsVal);

+ 4 - 4
src/Qii.php

@@ -115,7 +115,7 @@ class Qii extends Application
      */
     public static function setPrivate($name, $value)
     {
-        return Psr4::getInstance()->loadClass('\Qii\Config\Arrays')->setPrivate($name, $value);
+        return Psr4::getInstance()->loadClassOnce('\Qii\Config\Arrays')->setPrivate($name, $value);
     }
 
     /**
@@ -127,7 +127,7 @@ class Qii extends Application
      */
     public static function getPrivate($name, $key = '')
     {
-        $private = Psr4::getInstance()->loadClass('\Qii\Config\Arrays')->get($name);
+        $private = Psr4::getInstance()->loadClassOnce('\Qii\Config\Arrays')->get($name);
         if (preg_match('/^\s*$/', $key)) {
             return $private;
         }
@@ -142,7 +142,7 @@ class Qii extends Application
     public static function i()
     {
         return call_user_func_array(array(
-            Psr4::getInstance()->loadClass('\Qii\Language\Loader'), 'i'),
+            Psr4::getInstance()->loadClassOnce('\Qii\Language\Loader'), 'i'),
             func_get_args()
         );
     }
@@ -190,7 +190,7 @@ class Qii extends Application
      */
     public function __call($className, $args)
     {
-        return call_user_func_array(array(Psr4::getInstance(), 'loadClass'), func_get_args());
+        return call_user_func_array(array(Psr4::getInstance(), 'loadClassOnce'), func_get_args());
     }
 
     /**

+ 1 - 1
src/Request/Url.php

@@ -22,7 +22,7 @@ class Url
             throw new Unsupported("链接模式错误,链接格式只能为 '<u><font color=\"green\">" . join("', '", $allow) . "</font></u>',当前模式为 '<font color=\"red\">" . $rewriteRule . "</font>'", __LINE__);
         }
         $className = '\Qii\Request\Url\\' . $rewriteRule;
-        $this->request = Psr4::getInstance()->loadClass($className, $rewriteRule);
+        $this->request = Psr4::getInstance()->loadClassOnce($className, $rewriteRule);
         return $this;
     }
 

+ 2 - 2
src/View/Render.php

@@ -132,7 +132,7 @@ class Render
 				continue;
 			}
 			$executed[$className . ':' . $method] = true;
-			$class = Psr4::loadClass($className);
+			$class = Psr4::loadClassOnce($className);
 			$class->controller = $this;
 			$class->actionId = $this->_action;
 			$class->input = $resource->input;
@@ -150,7 +150,7 @@ class Render
 					$className = array_shift($subRes);
 					$method = array_shift($subRes);
 
-					$class = Psr4::loadClass($className);
+					$class = Psr4::loadClassOnce($className);
 					$class->controller = $this;
 					$class->actionId = $this->_action;
 					$class->input = $resource->input;