ソースを参照

Fixed some bugs

朱金辉 2 年 前
コミット
d6265a119e

+ 26 - 9
src/Application.php

@@ -20,6 +20,14 @@ use Qii\Request\Url;
 
 class Application
 {
+    /**
+     * @var Application $app
+     */
+    public static $app;
+    /**
+     * @var string $appPATH 项目目录
+     */
+    public static $appPATH = '.';
     /**
      * 存储网站配置文件内容
      *
@@ -62,17 +70,16 @@ class Application
     public $helper;
     /**
      * 全局middleware
-     * @var array
+     * @var array $middleware
      */
     private $middleware = [];
-    /**
-     * 路由middleware
-     * @var array middleware
-     */
+
+    public $viewEngine = 'smarty';
 
     public function __construct()
     {
         $this->helper = Psr4::getInstance()->loadClass('\Qii\Autoloader\Helper');
+        self::$app = & $this;
     }
 
     /**
@@ -136,6 +143,8 @@ class Application
             throw new FolderDoesNotExist(\Qii::i(1045, $workspace), __LINE__);
         }
         $workspace = Psr4::getInstance()->realpath($workspace);
+        //设置 app path , appPATH 为工作目录的上一级目录
+        self::$appPATH =  dirname($workspace);
         Psr4::getInstance()->removeNamespace('workspace', self::$workspace);
         //如果配置了使用namespace就走namespace
         self::$workspace = $workspace;
@@ -207,7 +216,7 @@ class Application
         $this->request = $this->getRequest();
         $this->dispatcher = $this->getDispatcher();
         if (!$this->dispatcher instanceof Dispatcher) {
-            throw new \Exception('Dispatcher must instance of Qii\Base\Dispatcher', __LINE__);
+            throw new \Exception('Dispatcher must instance of \Qii\Base\Dispatcher', __LINE__);
         }
         Setting::getInstance()->setDefaultTimeZone();
         Setting::getInstance()->setDefaultControllerAction();
@@ -387,6 +396,14 @@ class Application
         return $data;
     }
 
+    /**
+     * view engine 名字
+     * @param $engine
+     * @return void
+     */
+    public function setViewEngine($engine = 'smarty') {
+        $this->viewEngine = $engine;
+    }
     /**
      * 设置view
      *
@@ -400,8 +417,8 @@ class Application
         //如果之前实例化过相同的就不再实例化
         if (!$engine) $engine = $viewConfigure['engine'];
         $policy = (array)$policy;
-        if (!$policy) {
-            $policy = array_merge($policy, $viewConfigure[$engine]);
+        if (!$policy && isset($viewConfigure[$engine])) {
+            $policy = array_merge($policy, (array) $viewConfigure[$engine]);
         }
         $viewEngine = Psr4::getInstance()->loadClass('\Qii\View\Loader');
         $viewEngine->setView($engine, $policy);
@@ -552,8 +569,8 @@ class Application
                 }
             }
         }
-        $this->request->setDispatcher($this->dispatcher);
         //rewrite规则
+        $this->request->setDispatcher($this->dispatcher);
         $this->dispatcher->setRequest($this->request);
         $this->dispatcher->dispatch();
         $this->request->setDispatched(true);

+ 4 - 6
src/Autoloader/Import.php

@@ -46,14 +46,12 @@ class Import
         }
         $file = str_replace(array('\\', '/'), DS, $file);
         if (self::getIncludeFiles($file) !== null) self::getIncludeFiles($file);
-        if (file_exists($file)) {
-            $configure = include($file);
-            self::setIncludeFiles($file, $configure);
-            return $configure;
-        }else{
+        if(!file_exists($file)) {
             throw new FileNotFound($file, 404);
         }
-        return false;
+        $configure = include($file);
+        self::setIncludeFiles($file, $configure);
+        return $configure;
     }
 
     /**

+ 3 - 7
src/Base/Controller.php

@@ -11,7 +11,7 @@ use Qii\Driver\Model;
 use \Qii\View\Intf as viewIntf;
 
 /**
- * Qii_Controller_Abstract class
+ * Qii\Controller\Abstract class
  * @author Zhu Jinhui
  */
 abstract class Controller
@@ -29,7 +29,7 @@ abstract class Controller
      */
     public $language;
     /**
-     * @var Qii\Controller\Base $controller
+     * @var \Qii\Controller\Base $controller
      */
     public $controller;
     /**
@@ -87,11 +87,7 @@ abstract class Controller
         }
         //载入view
         if ($this->enableView) {
-            $this->view = $this->setView();
-        }
-        
-        if (!$this->beforeRun()) {
-            exit();
+            $this->view = $this->setView(\Qii::getInstance()->viewEngine);
         }
     }
     

+ 8 - 5
src/Base/Dispatcher.php

@@ -95,10 +95,13 @@ class Dispatcher
         }
         array_unshift($funcArgs, $controllerName);
         $psr4 = Psr4::getInstance();
-        $controllerCls = call_user_func_array(array($psr4, 'loadClass'), $funcArgs);
-
+        $this->controllerCls = call_user_func_array(array($psr4, 'loadClass'), $funcArgs);
+        //load beforeRun 不放到具体的Controller里边执行,要不这里的属性controllerCls获取不到值
+        if(method_exists($this->controllerCls, 'beforeRun') && is_callable(array($this->controllerCls, 'beforeRun'))) {
+            !$this->controllerCls->beforeRun() && exit();
+        }
         /**middleWare**/
-        $this->getMiddleWare($controllerCls)->gatherMiddleware();
+        $this->getMiddleWare($this->controllerCls)->gatherMiddleware();
 
         $next = array_reduce($this->requestMiddleWare,function ($carry, $item){
             return function () use ($carry, $item){
@@ -118,9 +121,9 @@ class Dispatcher
             $param = $property->getName();
             $this->request->setParam($param, $$param);
         }
-        $this->controllerCls = $controllerCls;
+        //$this->controllerCls = $controllerCls;
         $this->controllerCls->setRequest($this->request);
-        $this->controllerCls->controller = $controllerCls;
+        //$this->controllerCls->controller = $controllerCls;
         $this->controllerCls->controllerId = $controller;
         $this->controllerCls->actionId = $action;
         $realAction = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);

+ 2 - 2
src/Base/Request.php

@@ -428,7 +428,7 @@ abstract class Request
      * setBaseUri
      *
      * @param string $baseUri
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Qii\Request\Abstract
      */
     public function setBaseUri($baseUri)
     {
@@ -507,7 +507,7 @@ abstract class Request
      *
      * @param \Qii\Controller\Dispatcher $dispatcher
      */
-    public function setDispatcher(\Qii\Base\Dispatcher $dispatcher)
+    public function setDispatcher(Dispatcher $dispatcher)
     {
         $this->dispatcher = $dispatcher;
     }

+ 11 - 2
src/Cache/Apcu.php

@@ -1,7 +1,7 @@
 <?php
 namespace Qii\Cache;
 
-class Apcu implements Qii_Cache_Intf
+class Apcu implements Intf
 {
     const VERSION = '1.2';
     public $policy = array('life_time' => 3600);//设置目录、过期时间、文件前缀
@@ -13,7 +13,7 @@ class Apcu implements Qii_Cache_Intf
         }
     }
 
-    public function set($key, $value, $policy)
+    public function set($key, $value, array $policy = null)
     {
         if(is_array($policy))
         {
@@ -36,4 +36,13 @@ class Apcu implements Qii_Cache_Intf
     {
         return apcu_delete($key);
     }
+
+    public function clean()
+    {
+        // TODO: Implement clean() method.
+    }
+    public function remove($key)
+    {
+        return $this->del($key);
+    }
 }

+ 10 - 6
src/Cache/File.php

@@ -9,7 +9,11 @@
  * $this->cache->get(id);
  * $this->cache->remove(id);
  */
-class Qii_Cache_File implements Qii_Cache_Intf
+namespace Qii\Cache;
+use Qii\Autoloader\Psr4;
+use Qii\Exceptions\AccessDenied;
+
+class File implements Intf
 {
     const VERSION = '1.2';
     public $policy = array('path' => 'tmp', 'life_time' => 3600, 'prefix' => 'file');//设置目录、过期时间、文件前缀
@@ -21,7 +25,7 @@ class Qii_Cache_File implements Qii_Cache_Intf
             $this->policy = array_merge($this->policy, $policy);
         }
         $this->exclude = array();
-        $this->exclude[] = Qii_DIR;
+        $this->exclude[] = QII_DIR;
     }
 
     /**
@@ -44,13 +48,13 @@ class Qii_Cache_File implements Qii_Cache_Intf
     {
         if (!is_dir($this->policy['path'])) mkdir($this->policy['path'], 0777);
         if (is_dir($this->policy['path'])) {
-            $this->policy['path'] = \Qii_Autoloader_Psr4::realpath($this->policy['path']);
+            $this->policy['path'] = Psr4::realpath($this->policy['path']);
         } else {
             \Qii::setError(false, __LINE__, 1401, $this->policy['path']);
         }
         //如果在系统目录就不让保存
         if (in_array($this->policy['path'], $this->exclude)) {
-            throw  new \Qii_Execptions_AccessDenied($this->policy['path']);
+            throw  new AccessDenied($this->policy['path']);
         }
     }
 
@@ -104,7 +108,7 @@ class Qii_Cache_File implements Qii_Cache_Intf
      *
      * @param $id
      * @return mixed|void
-     * @throws AccessDeniedExecption
+     * @throws AccessDenied
      */
     public function get($id)
     {
@@ -140,7 +144,7 @@ class Qii_Cache_File implements Qii_Cache_Intf
     /**
      * 清除所有缓存
      *
-     * @throws AccessDeniedExecption
+     * @throws AccessDenied
      */
     public function clean()
     {

+ 2 - 1
src/Cache/Loader.php

@@ -6,6 +6,7 @@
  */
 namespace Qii\Cache;
 
+use Qii\Autoloader\Import;
 use Qii\Autoloader\Psr4;
 
 class Loader
@@ -41,6 +42,6 @@ class Loader
         if (!is_file($cacheFile)) {
             throw new \Exception('Unsupported cache class '. $cacheFile);
         }
-        \Qii\Autoloader\Import::requires($cacheFile);
+        Import::requires($cacheFile);
     }
 }

+ 10 - 6
src/Cache/Redis.php

@@ -1,7 +1,12 @@
 <?php
 namespace Qii\Cache;
 
-\Qii\Autoloader\Import::requires(array(dirname(__FILE__) . DS . 'Redis/Client.php', dirname(__FILE__) . DS . 'Redis/Cluster.php'));
+use Qii\Autoloader\Import;
+use Qii\Cache\Redis\Cluster;
+use Qii\Exceptions\Errors;
+use Qii\Exceptions\MethodNotFound;
+
+Import::requires(array(dirname(__FILE__) . DS . 'Redis/Client.php', dirname(__FILE__) . DS . 'Redis/Cluster.php'));
 
 /**
  * PHP 操作 redis
@@ -32,7 +37,7 @@ class Redis implements Intf
     public function __construct(array $policy = null)
     {
         if (!extension_loaded('redis')) {
-            throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1006), __LINE__);
+            throw new MethodNotFound(\Qii::i(1006), __LINE__);
         }
 
 
@@ -44,7 +49,7 @@ class Redis implements Intf
         foreach ($this->policy['servers'] AS $value) {
             $redisServer[] = array('host' => $value['host'], 'port' => $value['port'], 'password' => (isset($value['password']) ? $value['password'] : ''));
         }
-        $this->redis = new \Qii\Cache\Redis\Cluster($redisServer, 128);
+        $this->redis = new Cluster($redisServer, 128);
     }
 
     /**
@@ -63,7 +68,7 @@ class Redis implements Intf
                 $this->redis->expire($id, $this->policy['life_time']);
             }
         } catch (\CredisException $e) {
-            throw new \Qii\Exceptions\Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
+            throw new Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
         }
         return $res;
     }
@@ -80,11 +85,10 @@ class Redis implements Intf
         try {
             $res = $this->redis->set($id, $value);
             if (isset($this->policy['life_time']) && $this->policy['life_time'] > 0) {
-                //$this->redis->setTimeout($id, $this->policy['life_time']);
                 $this->redis->expire($id, $this->policy['life_time']);
             }
         } catch (\CredisException $e) {
-            throw new \Qii\Exceptions\Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
+            throw new Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
         }
 
         return $res;

+ 1 - 1
src/Cache/XCache.php

@@ -27,7 +27,7 @@ class XCache implements Intf
     /**
      * 构造函数
      *
-     * @param 默认的缓存策略 $default_policy
+     * @param array $default_policy 默认的缓存策略
      */
     public function __construct(array $default_policy = null)
     {

+ 20 - 20
src/Config/Namespace.php

@@ -29,25 +29,25 @@ return array(
     ),
     //设置指定名称空间的文件路径,如按照namespace的不用指定
     'addNamespace' => array(
-        array('Qii\\', Qii_DIR . DS),
-        array('Qii\Action', Qii_DIR . DS . 'Action'),
-        array('Qii\Autoloader', Qii_DIR . DS . 'Autoloader'),
-        array('Qii\Controller', Qii_DIR . DS . 'Controller'),
-        array('Qii\Bootstrap', Qii_DIR . DS . 'Bootstrap'),
-        array('Qii\Config', Qii_DIR . DS . 'Config'),
-        array('Qii\Contracts', Qii_DIR . DS . 'Contracts'),
-        array('Qii\Exceptions', Qii_DIR . DS . 'Exceptions'),
-        array('Qii\Language', Qii_DIR . DS . 'Language'),
-        array('Qii\Library', Qii_DIR . DS . 'Library'),
-        array('Qii\Logger', Qii_DIR . DS . 'Logger'),
-        array('Qii\Plugin', Qii_DIR . DS . 'Plugin'),
-        array('Qii\Request', Qii_DIR . DS . 'Request'),
-        array('Qii\Response', Qii_DIR . DS . 'Response'),
-        array('Qii\Router', Qii_DIR . DS . 'Router'),
-        array('Qii\View', Qii_DIR . DS . 'View'),
-        array('Smarty', Qii_DIR . DS . 'View' . DS . 'smarty'),
-        array('Smarty', Qii_DIR . DS . 'View' . DS . 'smarty' . DS . 'sysplugins'),
-        array('WhichBrowser', Qii_DIR . DS . 'Library'. DS . 'Third'. DS . 'WhichBrowser'),
-        array('BigPipe', Qii_DIR . DS . 'Library'. DS .'BigPipe'. DS .'BigPipe'),
+        array('Qii\\', QII_DIR . DS),
+        array('Qii\Action', QII_DIR . DS . 'Action'),
+        array('Qii\Autoloader', QII_DIR . DS . 'Autoloader'),
+        array('Qii\Controller', QII_DIR . DS . 'Controller'),
+        array('Qii\Bootstrap', QII_DIR . DS . 'Bootstrap'),
+        array('Qii\Config', QII_DIR . DS . 'Config'),
+        array('Qii\Contracts', QII_DIR . DS . 'Contracts'),
+        array('Qii\Exceptions', QII_DIR . DS . 'Exceptions'),
+        array('Qii\Language', QII_DIR . DS . 'Language'),
+        array('Qii\Library', QII_DIR . DS . 'Library'),
+        array('Qii\Logger', QII_DIR . DS . 'Logger'),
+        array('Qii\Plugin', QII_DIR . DS . 'Plugin'),
+        array('Qii\Request', QII_DIR . DS . 'Request'),
+        array('Qii\Response', QII_DIR . DS . 'Response'),
+        array('Qii\Router', QII_DIR . DS . 'Router'),
+        array('Qii\View', QII_DIR . DS . 'View'),
+        array('Smarty', QII_DIR . DS . 'View' . DS . 'smarty'),
+        array('Smarty', QII_DIR . DS . 'View' . DS . 'smarty' . DS . 'sysplugins'),
+        array('WhichBrowser', QII_DIR . DS . 'Library'. DS . 'Third'. DS . 'WhichBrowser'),
+        array('BigPipe', QII_DIR . DS . 'Library'. DS .'BigPipe'. DS .'BigPipe'),
     )
 );

+ 2 - 2
src/Config/Setting.php

@@ -41,8 +41,8 @@ class Setting
     {
         $this->language = Psr4::getInstance()->loadClass('\Qii\Language\Loader');
         //加载语言包
-        $this->language->load('error', Qii_DIR);
-        $this->language->load('exception', Qii_DIR);
+        $this->language->load('error', QII_DIR);
+        $this->language->load('exception', QII_DIR);
         return $this;
     }
 

+ 1 - 0
src/Driver/EasyORM.php

@@ -624,6 +624,7 @@ final class EasyORM {
         if(!$orderBy) {
             return $this;
         }
+        //@todo 验证一下 orderBy 字段
         if(!is_array($orderBy)) {
             $this->_easyORM->orderBy[] = $orderBy;
         }else{

+ 5 - 5
src/Driver/Model.php

@@ -87,11 +87,11 @@ class Model
             $this->_driver = array_shift($this->_allow);
         }
         Import::requires(array(
-            Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'Base.php',
-            Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnBase.php',
-            Qii_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnIntf.php',
-            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',
+            QII_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'Base.php',
+            QII_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnBase.php',
+            QII_DIR . DS . 'Qii' . DS . 'Driver' . DS . 'ConnIntf.php',
+            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(
             '\Qii\Driver\\' . ucWords($this->_driver) . '\Driver',

+ 4 - 3
src/Driver/Mysqli/Connection.php

@@ -5,6 +5,7 @@ use Qii\Config\Consts;
 use Qii\Config\Register;
 use Qii\Driver\ConnBase;
 use Qii\Driver\ConnIntf;
+use Qii\Exceptions\Errors;
 
 class Connection extends ConnBase implements ConnIntf
 {
@@ -38,7 +39,7 @@ class Connection extends ConnBase implements ConnIntf
 		if ($useSlave) {
 			try {
 				$connection = mysqli_connect($dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db']);
-				if (!$connection) throw new \Qii\Exceptions\Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
+				if (!$connection) throw new Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
 				mysqli_select_db($connection, $dbInfo['db']);
 				return self::$_readConnection = $connection;
 			} catch (Exception  $e) {
@@ -60,11 +61,11 @@ class Connection extends ConnBase implements ConnIntf
 		$dbInfo = $this->_dbInfo['master'];
 		try {
 			$connection = @mysqli_connect($dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db']);
-			if (!$connection) throw new \Qii\Exceptions\Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
+			if (!$connection) throw new Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
 			mysqli_select_db($connection, $dbInfo['db']);
 			return self::$_writeConnection = $connection;
 		} catch (Exception  $e) {
-			throw new \Qii\Exceptions\Errors(\Qii::i(1500, $dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db'], toUTF8($e->getMessage())), __LINE__);
+			throw new Errors(\Qii::i(1500, $dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db'], toUTF8($e->getMessage())), __LINE__);
 		}
 	}
 }

+ 5 - 2
src/Driver/Pdo/Connection.php

@@ -6,8 +6,11 @@
  */
 namespace Qii\Driver\Pdo;
 
+use Qii\Config\Consts;
+use Qii\Config\Register;
 use Qii\Driver\ConnBase;
 use Qii\Driver\ConnIntf;
+use Qii\Exceptions\Errors;
 
 class Connection extends ConnBase implements ConnIntf
 {
@@ -15,7 +18,7 @@ class Connection extends ConnBase implements ConnIntf
 
 	public function __construct()
 	{
-		$this->_dbInfo = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_DB));
+		$this->_dbInfo = Register::getAppConfigure(Register::get(Consts::APP_DB));
 		if(!isset($this->_dbInfo['use_db_driver'])) $this->_dbInfo['use_db_driver'] = 'mysql';
 	}
 
@@ -63,7 +66,7 @@ class Connection extends ConnBase implements ConnIntf
 			}
 			return self::$_writeConnection = new \PDO($dsn, $dbInfo['user'], $dbInfo['password']);
 		} catch (Exception  $e) {
-			throw new \Qii\Exceptions\Errors(\Qii::i(1500, $dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db'], toUTF8($e->getMessage())), __LINE__);
+			throw new Errors(\Qii::i(1500, $dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db'], toUTF8($e->getMessage())), __LINE__);
 		}
 	}
 }

+ 2 - 2
src/Driver/Pdo/Driver.php

@@ -43,13 +43,13 @@ class Driver extends Base implements Intf
     /**
      * 最后一次执行的SQL
      *
-     * @var unknown_type
+     * @var string $sql;
      */
     private $sql;
     /**
      * 是否开启执行SQL的时间
      *
-     * @var BOOL
+     * @var bool $_debugTime
      */
     public $_debugTime = false;
     public $_errorInfo = array();

+ 1 - 1
src/Driver/TraitCache.php

@@ -15,7 +15,7 @@ trait TraitCache
      */
     final public function setCache($cache, $policy)
     {
-        Import::requires(Qii_DIR . DS . 'Qii' . DS . 'Cache.php');
+        Import::requires(QII_DIR . DS . 'Qii' . DS . 'Cache.php');
         $this->cache = Psr4::loadClass('\Qii\Cache', $cache)->initialization($policy);//载入cache类文件
     }
 

+ 4 - 3
src/Exceptions/Error.php

@@ -120,7 +120,7 @@ class Error
             $filePath = Psr4::getInstance()->searchMappedFile($controllerCls);
             if (!is_file($filePath)) {
                 if ($env == 'product') return '';
-                Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
+                Import::requires(QII_DIR . DS . 'Exceptions' . DS . 'Error.php');
                 call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
                 exit();
             }
@@ -128,6 +128,7 @@ class Error
             \Qii::getInstance()->request->setActionName($action);
             \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request);
             \Qii::getInstance()->dispatcher->dispatch($controller, $action, new \Exception($msg ."\n". self::getTraceAsString(), $code));
+            return true;
         }
         return false;
     }
@@ -138,7 +139,7 @@ class Error
      */
     public static function showError($message)
     {
-        include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
+        include(join(DS, array(QII_DIR, 'Exceptions', 'View', 'error.php')));
     }
     
     /**
@@ -147,7 +148,7 @@ class Error
      */
     public static function showMessage($message)
     {
-        include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'message.php')));
+        include(join(DS, array(QII_DIR, 'Exceptions', 'View', 'message.php')));
     }
     
     public function __call($method, $args)

+ 2 - 2
src/Exceptions/Errors.php

@@ -87,7 +87,7 @@ class Errors extends \Exception
             $filePath = Psr4::getInstance()->searchMappedFile($controllerCls);
             if (!is_file($filePath)) {
                 if ($env == 'product') return '';
-                Import::requires(Qii_DIR . DS . 'Exceptions' . DS . 'Error.php');
+                Import::requires(QII_DIR . DS . 'Exceptions' . DS . 'Error.php');
                 call_user_func_array(array('\Qii\Exceptions\Error', 'index'), array($controller, $action));
                 die();
             }
@@ -100,7 +100,7 @@ class Errors extends \Exception
             return \Qii::getInstance()->dispatcher->setRequest(\Qii::getInstance()->request)->dispatch($controller, $action, $e);
         }
         ob_start();
-        include(join(DS, array(Qii_DIR, 'Exceptions', 'View', 'error.php')));
+        include(join(DS, array(QII_DIR, 'Exceptions', 'View', 'error.php')));
         $html = ob_get_contents();
         ob_clean();
         

+ 2 - 2
src/Exceptions/View/example.php

@@ -5,7 +5,7 @@
 	<meta name="description" content="Qii 框架,以最简单的方式创建你的Web应用。" />
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
     <meta http-equiv="content-type" content="text/html;charset=utf-8">
-    <?php include(Qii_DIR . '/view/style.php'); ?>
+    <?php include(QII_DIR . '/view/style.php'); ?>
 </head>
 <body>
 <h1>使用方法:</h1>
@@ -367,6 +367,6 @@
 
 </code>
 </pre>
-<?php include(Qii_DIR . '/view/footer.php'); ?>
+<?php include(QII_DIR . '/view/footer.php'); ?>
 </body>
 </html>

+ 3 - 2
src/Functions/Funcs.php

@@ -1,5 +1,6 @@
 <?php
 
+use Qii\Autoloader\Import;
 use Qii\Autoloader\Psr4;
 
 /**
@@ -133,12 +134,12 @@ function _DBDriver(\Qii\Driver\Rules $rule, $privateKey = null, $fieldsVal = nul
  * _include include文件
  */
 function _include($files){
-	return \Qii\Autoloader\Import::includes($files);
+	return Import::includes($files);
 }
 
 function _require($files)
 {
-	return \Qii\Autoloader\Import::requires($files);
+	return Import::requires($files);
 }
 
 /**

+ 4 - 4
src/Language/Loader.php

@@ -5,10 +5,10 @@
  * @version 1.2
  *
  * Usage:
- *    \Qii\Language\Loader::getInstance()->load('error', Qii_DIR); 加载系统目录中的语言
+ *    \Qii\Language\Loader::getInstance()->load('error', QII_DIR); 加载系统目录中的语言
  *    \Qii\Language\Loader::getInstance()->load('error'); 加载程序目录中的语言
  * OR
- *    _language()->load('error', Qii_DIR); 加载系统目录中的语言
+ *    _language()->load('error', QII_DIR); 加载系统目录中的语言
  *    _language()->load('error'); 加载程序目录中的语言
  */
 namespace Qii\Language;
@@ -52,9 +52,9 @@ class Loader
 		{
 			$dir = \Qii::getInstance()->getWorkspace() . DS;
 		}
-		else if ($dir == Qii_DIR)
+		else if ($dir == QII_DIR)
 		{
-			$dir = Qii_DIR . DS . 'Language' . DS;
+			$dir = QII_DIR . DS . 'Language' . DS;
 		}
 		else
 		{

+ 1 - 1
src/Library/Http.php

@@ -14,7 +14,7 @@ namespace Qii\Library;
         ['hightman\http', true],
     ])
     ->addNamespaces([
-        ['hightman\http', Qii_DIR . DS .'Library'. DS . 'Third'. DS .'hightman'],
+        ['hightman\http', QII_DIR . DS .'Library'. DS . 'Third'. DS .'hightman'],
     ]);
 use hightman\http\Client;
 

+ 1 - 1
src/Library/LibUrl.php

@@ -14,7 +14,7 @@ namespace Qii\Library;
         ['Curl', true],
     ])
     ->addNamespaces([
-        ['Curl', Qii_DIR . DS .'Library'. DS . 'Third'. DS .'Curl'],
+        ['Curl', QII_DIR . DS .'Library'. DS . 'Third'. DS .'Curl'],
     ]);
 use Curl\Curl;
 class LibUrl extends Curl

+ 1 - 1
src/Library/Mail.php

@@ -7,7 +7,7 @@ namespace Qii\Library;
  * @author Jinhui.zhu	<jinhui.zhu@live.cn>
  * @version  $Id: mail.plugin.php,v 1.1 2010/04/23 06:02:12 Jinhui.Zhu Exp $
  */
-_require(Qii_DIR . "/Library/Third/phpmailer/class.phpmailer.php");
+_require(QII_DIR . "/Library/Third/phpmailer/class.phpmailer.php");
 class Mail extends \PHPMailer
 {
 	private $mailConfig;

+ 1 - 1
src/Library/PHPSplit.php

@@ -6,7 +6,7 @@ namespace Qii\Library;
         ['phpSplit', true],
     ])
     ->addNamespaces([
-        ['phpSplit', Qii_DIR . DS .'Library'. DS . 'Third'. DS .'php-split'. DS . 'src'. DS],
+        ['phpSplit', QII_DIR . DS .'Library'. DS . 'Third'. DS .'php-split'. DS . 'src'. DS],
     ]);
 
 use phpSplit\Split;

+ 1 - 1
src/Library/PHPSplitWord.php

@@ -1,7 +1,7 @@
 <?php
 namespace Qii\Library;
 
-_require(Qii_DIR . "/Library/Third/PHPWord.php");
+_require(QII_DIR . "/Library/Third/PHPWord.php");
 class PHPSplitWord extends PHPWord
 {
 }

+ 1 - 1
src/Library/Pinyin.php

@@ -8,7 +8,7 @@ use Yurun\Util\Chinese;
         ['Yurun\Util', true],
     ])
     ->addNamespaces([
-        ['Yurun\Util', Qii_DIR . DS .'Library'. DS . 'Third'. DS .'ChineseUtil'. DS . 'src'. DS],
+        ['Yurun\Util', QII_DIR . DS .'Library'. DS . 'Third'. DS .'ChineseUtil'. DS . 'src'. DS],
     ]);
 /**
  * 拼音

+ 2 - 2
src/Library/Qr.php

@@ -46,8 +46,8 @@ namespace Qii\Library;
         ['QrCode', true],
     ])
     ->addNamespaces([
-        ['Zxing', Qii_DIR . DS . 'Library' . DS . 'QrReader'],
-        ['QrCode', Qii_DIR . DS . 'Library' . DS . 'QrCode'],
+        ['Zxing', QII_DIR . DS . 'Library' . DS . 'QrReader'],
+        ['QrCode', QII_DIR . DS . 'Library' . DS . 'QrCode'],
     ]);
 use Zxing\QrReader;
 

+ 1 - 1
src/Library/UAgent.php

@@ -2,7 +2,7 @@
 namespace Qii\Library;
 
 
-_require(Qii_DIR . "/Library/Third/WhichBrowser/Parser.php");
+_require(QII_DIR . "/Library/Third/WhichBrowser/Parser.php");
 
 class UAgent extends \WhichBrowser\Parser
 {

+ 14 - 14
src/Qii.php

@@ -9,7 +9,7 @@ use \Qii\Config\Register;
 /**
  * Qii 框架基本库所在路径
  */
-define('Qii_DIR', dirname(__FILE__));
+define('QII_DIR', dirname(__FILE__));
 /**
  * DIRECTORY_SEPARATOR 的简写
  */
@@ -41,16 +41,16 @@ if (IS_CLI) {
 define('QII_EOL', IS_CLI ? PHP_EOL : '<br />');
 define('QII_SPACE', IS_CLI ? ' ' : '&nbsp;');
 
-require Qii_DIR . DS . 'Autoloader' . DS . 'Import.php';
-Import::setFileLoaded(Qii_DIR . DS . 'Autoloader' . DS . 'Import.php');
+require QII_DIR . DS . 'Autoloader' . DS . 'Import.php';
+Import::setFileLoaded(QII_DIR . DS . 'Autoloader' . DS . 'Import.php');
 
 Import::requires(
-    array(Qii_DIR . DS . 'Config' . DS . 'Consts.php',
-        Qii_DIR . DS . 'Functions' . DS . 'Funcs.php',
-        Qii_DIR . DS . 'Autoloader' . DS . 'Factory.php',
-        Qii_DIR . DS . 'Application.php',
-        Qii_DIR . DS . 'Autoloader' . DS . 'Psr4.php',
-        Qii_DIR . DS . 'Config' . DS . 'Arrays.php',
+    array(QII_DIR . DS . 'Config' . DS . 'Consts.php',
+        QII_DIR . DS . 'Functions' . DS . 'Funcs.php',
+        QII_DIR . DS . 'Autoloader' . DS . 'Factory.php',
+        QII_DIR . DS . 'Application.php',
+        QII_DIR . DS . 'Autoloader' . DS . 'Psr4.php',
+        QII_DIR . DS . 'Config' . DS . 'Arrays.php',
     )
 );
 
@@ -125,7 +125,7 @@ class Qii extends Application
      * @param Int|String $code
      * @return Bool
      */
-    public static function setError($condition, $line = 0, $code, $args = null)
+    public static function setError($condition, $line = 0, $code, $args = null, $msg = '')
     {
         return call_user_func_array(array('\Qii\Exceptions\Error', 'setError'), func_get_args());
     }
@@ -205,16 +205,16 @@ if (!function_exists('catch_fatal_error')) {
 }
 
 //注册名称空间
-$namespace = _include(Qii_DIR . DS . 'Config' . DS . 'Namespace.php');
+$namespace = _include(QII_DIR . DS . 'Config' . DS . 'Namespace.php');
 Psr4::getInstance()
     ->register()
     ->setUseNamespaces($namespace['setUseNamespace'] ?? [])
     ->addNamespaces($namespace['addNamespace'] ?? []);
 
 //加载默认语言包
-Factory::getInstance('\Qii\Language\Loader')->load('error', Qii_DIR . DS . 'Language');
-Factory::getInstance('\Qii\Language\Loader')->load('exception', Qii_DIR . DS . 'Language');
-Factory::getInstance('\Qii\Language\Loader')->load('resource', Qii_DIR . DS . 'Language');
+Factory::getInstance('\Qii\Language\Loader')->load('error', QII_DIR . DS . 'Language');
+Factory::getInstance('\Qii\Language\Loader')->load('exception', QII_DIR . DS . 'Language');
+Factory::getInstance('\Qii\Language\Loader')->load('resource', QII_DIR . DS . 'Language');
 
 
 //捕获FATAL错误,用户可以选择记录到日志,还是直接显示或者不显示错误

+ 1 - 1
src/Router/Parse.php

@@ -40,7 +40,7 @@ class Parse
         $router = Register::getAppConfigure(Consts::APP_SITE_ROUTER);
         $rewriteRule = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE), 'rewriteRule');
         if (!$rewriteRule) $rewriteRule = 'Normal';
-        Import::requires(Qii_DIR . DS . 'Router' . DS . 'Parse' .DS. $rewriteRule . '.php');
+        Import::requires(QII_DIR . DS . 'Router' . DS . 'Parse' .DS. $rewriteRule . '.php');
         $className = '\Qii\Router\Parse\\' . $rewriteRule;
         if (!class_exists($className, false)) {
             throw new ClassNotFound(\Qii::i(1103, $className), __LINE__);

+ 26 - 3
src/View/Includes.php

@@ -8,7 +8,10 @@
  */
 namespace Qii\View;
 
-class Includes implements \Qii\View\Intf
+use Qii\Config\Consts;
+use Qii\Config\Register;
+
+class Includes implements Intf
 {
 	const VERSION = '1.2';
 	public $data;
@@ -17,7 +20,7 @@ class Includes implements \Qii\View\Intf
 
 	public function __construct()
 	{
-		$appConfigure = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_INI_FILE));
+		$appConfigure = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE));
 		$this->viewPath = $appConfigure['view']['path'];
 	}
 
@@ -72,6 +75,26 @@ class Includes implements \Qii\View\Intf
 		return isset($this->_blocks[$block]) ? $this->_blocks[$block] : '';
 	}
 
+
+    /**
+     * fetch method
+     * @param string $tpl 模版
+     * @param array $data 数据
+     * @return mixed
+     */
+    public function fetch($tpl, $data = array()) {
+        if($data) {
+            $this->assign($data);
+        }
+
+        extract((array)$this->data);
+
+        $tpl = $this->viewPath . DS . $tpl;
+        if (!\Qii::setError(is_file($tpl), __LINE__,404,  $tpl . ' does not exist', null , $tpl . '文件不存在')) {
+            require($tpl);
+        }
+    }
+
 	/**
 	 * 载入数据和模板
 	 *
@@ -81,7 +104,7 @@ class Includes implements \Qii\View\Intf
 	{
 		$tpl = $this->viewPath . DS . $tpl;
 		extract((array)$this->data);
-		if (!\Qii::setError(is_file($tpl), 1405, $tpl . ' does not exist')) {
+        if (!\Qii::setError(is_file($tpl), __LINE__,404,  $tpl . ' does not exist', null , $tpl . '文件不存在')) {
 			include($tpl);
 		}
 	}

+ 19 - 13
src/View/Loader.php

@@ -1,30 +1,36 @@
 <?php
 namespace Qii\View;
 
-use Qii\Exceptions\Unsupported;
-
 class Loader
 {
 	protected $view;
-	protected $allow = array('smarty', 'require', 'include');
+	protected $allow = array('smarty', 'requires', 'includes');
 	
-	public function __construct()
-	{
+	public function __construct(){
 
 	}
-	
-	public function setView($engine, $policy = array())
-	{
+
+    /**
+     * 设置 view 支持自定义
+     * @param string $engine 模版引擎
+     * @param array $policy 初始化传递的参数
+     * @return mixed|\Qii\Autoloader\Psr4|null
+     */
+	public function setView($engine, $policy = array()) {
+        $class = '\Qii\View\\'. ucwords($engine);
 		if(!in_array($engine, $this->allow))
 		{
-			throw new Unsupported(\Qii::i('Unsupported method', $engine));
+            $class = ucwords($engine);
 		}
-		$class = '\Qii\View\\'. ucwords($engine);
-		return $this->view = new $class();
+		return $this->view = _loader($class, $policy);
 	}
 
-	public function getView()
-	{
+    /**
+     * 获取view
+     *
+     * @return mixed
+     */
+	public function getView() {
 		return $this->view;
 	}
 }

+ 5 - 4
src/View/Render.php

@@ -2,6 +2,7 @@
 namespace Qii\View;
 
 use Qii\Autoloader\Psr4;
+use Qii\Request\Url;
 
 class Render
 {
@@ -25,10 +26,10 @@ class Render
 			if (is_array($resource->blockLinkJs)) {
 				foreach ($resource->blockLinkJs AS $js) {
 					if ($js == '') continue;
-					$blockJs[] = '<script src="' . \Qii\Request\Url::getSourceFullUrl($js) . '"></script>';
+					$blockJs[] = '<script src="' . Url::getSourceFullUrl($js) . '"></script>';
 				}
 			} else {
-				$blockJs[] = '<script src="' . \Qii\Request\Url::getSourceFullUrl($resource->blockLinkJs) . '"></script>';
+				$blockJs[] = '<script src="' . Url::getSourceFullUrl($resource->blockLinkJs) . '"></script>';
 			}
 		}
 		$this->_view->assign('blockLinkJs', "\n" . join("\n", $blockJs) . "\n");
@@ -48,10 +49,10 @@ class Render
 			if (is_array($resource->blockLinkCss)) {
 				foreach ($resource->blockLinkCss AS $css) {
 					if ($css == '') continue;
-					$blockCss[] = '<link href="' . \Qii\Request\Url::getSourceFullUrl($css) . '" rel="stylesheet">';
+					$blockCss[] = '<link href="' . Url::getSourceFullUrl($css) . '" rel="stylesheet">';
 				}
 			} else {
-				$blockCss[] = '<link href="' . \Qii\Request\Url::getSourceFullUrl($resource->blockLinkCss) . '" rel="stylesheet">';
+				$blockCss[] = '<link href="' . Url::getSourceFullUrl($resource->blockLinkCss) . '" rel="stylesheet">';
 			}
 		}
 		$this->_view->assign('blockLinkCss', "\n" . join("\n", $blockCss) . "\n");

+ 27 - 8
src/View/Requires.php

@@ -8,6 +8,9 @@
  */
 namespace Qii\View;
 
+use Qii\Config\Consts;
+use Qii\Config\Register;
+
 class Requires implements Intf
 {
 	const VERSION = '1.2';
@@ -17,7 +20,7 @@ class Requires implements Intf
 
 	public function __construct()
 	{
-		$appConfigure = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_INI_FILE));
+		$appConfigure = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE));
 		$this->viewPath = $appConfigure['view']['path'];
 	}
 
@@ -61,17 +64,33 @@ class Requires implements Intf
 	 * @param Mix $name
 	 * @param Mix $val
 	 */
-	public function assign($name, $val)
+	public function assign($name, $val = '')
 	{
-		if (isset($val)) {
+        if (is_array($name)) {
+            foreach ($name AS $k => $v) {
+                $this->data[$k] = $v;
+            }
+        } else if ($val) {
 			$this->data[$name] = $val;
-		} else if (is_array($name)) {
-			foreach ($name AS $k => $v) {
-				$this->data[$k] = $v;
-			}
 		}
 	}
 
+    /**
+     * fetch method
+     * @param string $tpl 模版
+     * @param array $data 数据
+     * @return mixed
+     */
+    public function fetch($tpl, $data = array()) {
+        if($data) {
+            $this->assign($data);
+        }
+
+        extract((array)$this->data);
+        if (!\Qii::setError(is_file($tpl), __LINE__,404,  $tpl . ' does not exist', $tpl . '文件不存在')) {
+            require($this->viewPath . DS . $tpl);
+        }
+    }
 	/**
 	 * 载入数据和模板
 	 *
@@ -81,7 +100,7 @@ class Requires implements Intf
 	{
 		$tpl = $this->viewPath . DS . $tpl;
 		extract((array)$this->data);
-		if (!\Qii::setError(is_file($tpl), __LINE__, 1405, $tpl)) {
+        if (!\Qii::setError(is_file($tpl), __LINE__,404,  $tpl . ' does not exist' , $tpl . '文件不存在')) {
 			require($tpl);
 		}
 	}

+ 12 - 8
src/View/Smarty.php

@@ -9,12 +9,15 @@
 namespace Qii\View;
 
 use Qii\Autoloader\Import;
+use Qii\Autoloader\Psr4;
+use Qii\Config\Consts;
+use Qii\Config\Register;
 
-Import::requires(Qii_DIR . DS . 'View' . DS . 'smarty' . DS . 'Autoloader.php');
+Import::requires(QII_DIR . DS . 'View' . DS . 'smarty' . DS . 'Autoloader.php');
 \Smarty_Autoloader::register(true);
-Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'SmartyBC.class.php');
-Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartyexception.php');
-Import::requires(Qii_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartycompilerexception.php');
+Import::requires(QII_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'SmartyBC.class.php');
+Import::requires(QII_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartyexception.php');
+Import::requires(QII_DIR . DS . DS . 'View' . DS . 'smarty' . DS . 'sysplugins' .DS. 'smartycompilerexception.php');
 
 class Smarty extends \SmartyBC implements Intf
 {
@@ -96,13 +99,13 @@ class Smarty extends \SmartyBC implements Intf
 	public function __construct()
 	{
 		parent::__construct();
-		$appConfigure = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_INI_FILE));
+		$appConfigure = Register::getAppConfigure(Register::get(Consts::APP_INI_FILE));
 		$viewInfo = $appConfigure['view']['smarty'];
 		if (isset($viewInfo['ldelimiter']) && !empty($viewInfo['ldelimiter'])) $this->left_delimiter = $viewInfo['ldelimiter'];//变量左边界
 		if (isset($viewInfo['rdelimiter']) && !empty($viewInfo['rdelimiter'])) $this->right_delimiter = $viewInfo['rdelimiter'];//变量右边界
-		if (isset($viewInfo['path']) && !empty($viewInfo['path'])) $this->template_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['path']));
-		if (isset($viewInfo['compile']) && !empty($viewInfo['compile'])) $this->compile_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['compile']));
-		if (isset($viewInfo['cache']) && !empty($viewInfo['cache'])) $this->cache_dir = \Qii\Autoloader\Psr4::realpath(\Qii\Autoloader\Psr4::getInstance()->getFolderByPrefix($viewInfo['cache']));
+		if (isset($viewInfo['path']) && !empty($viewInfo['path'])) $this->template_dir = Psr4::realpath(Psr4::getInstance()->getFolderByPrefix($viewInfo['path']));
+		if (isset($viewInfo['compile']) && !empty($viewInfo['compile'])) $this->compile_dir = Psr4::realpath(Psr4::getInstance()->getFolderByPrefix($viewInfo['compile']));
+		if (isset($viewInfo['cache']) && !empty($viewInfo['cache'])) $this->cache_dir = Psr4::realpath(Psr4::getInstance()->getFolderByPrefix($viewInfo['cache']));
 		if (isset($viewInfo['lifetime']) && !empty($viewInfo['lifetime'])) $this->cache_lifetime = $viewInfo['lifetime'];
 		//将老版本过度到新版本
 		$this->setTemplateDir($this->template_dir)
@@ -158,6 +161,7 @@ class Smarty extends \SmartyBC implements Intf
 	}*/
 	/**
 	 * 检查模板文件名称,只允许使用tpl
+     *
 	 * @param string $template 模板文件
 	 * @return bool | throw Exception
 	 */