Ver código fonte

Update: 框架优化

zhujinhui 2 anos atrás
pai
commit
07f9ca3c4a

+ 1 - 1
composer.json

@@ -11,7 +11,7 @@
     ],
     "minimum-stability": "dev",
     "require": {
-        "php": ">=5.3.0",
+        "php": ">=5.4.0",
         "ext-gd" : ">=2.1.0"
     }
 }

+ 3 - 3
demo/private/plugins/loger.php

@@ -30,10 +30,10 @@ class loger implements \Qii\Loger\Writer
 		return $this->trimSpace(print_r($log, true));
 	}
 
-	public function writeLog($loger)
+	public function writeLog($logger)
 	{
-		if(is_array($loger)) $loger = $this->formatLog($loger);
+		if(is_array($logger)) $logger = $this->formatLog($logger);
 
-		file_put_contents($this->fileName, date('Y-m-d H:i:s') ."\n\t". $loger . "\n", FILE_APPEND);
+		file_put_contents($this->fileName, date('Y-m-d H:i:s') ."\n\t". $logger . "\n", FILE_APPEND);
 	}
 }

+ 4 - 4
src/Autoloader/Helper.php

@@ -1,6 +1,6 @@
 <?php
 namespace Qii\Autoloader;
-
+use \Qii\Exceptions;
 /**
  * Helper 将自动注册到系统 Helper中,直接调用即可
  *
@@ -39,7 +39,7 @@ class Helper
     public function get($helper)
     {
         if (isset(self::$helpers[$helper])) return self::$helpers[$helper];
-        throw new \Qii\Exceptions\CallUndefinedClass(\Qii::i('1105', $helper), __LINE__);
+        throw new CallUndefinedClass(\Qii::i('1105', $helper), __LINE__);
     }
 
     /*
@@ -55,11 +55,11 @@ class Helper
             return;
         }
         foreach (glob(str_replace("//", DS, $appPath . DS . 'helper' . DS . '*.php'), GLOB_BRACE) AS $file) {
-            if(\Qii\Autoloader\Import::requires($file)){
+            if(Import::requires($file)){
                 //如果里边包含class的话就将class注册到Qii::instance('class');
                 $className = 'helper\\'. pathinfo($file)['filename'];
                 if (!isset(self::$helpers[$className]) && class_exists($className, false)) {
-                    self::$helpers[$className] = \Qii\Autoloader\Psr4::getInstance()->instance($className);
+                    self::$helpers[$className] = Psr4::getInstance()->instance($className);
                 }
             }
         }

+ 13 - 13
src/Autoloader/Import.php

@@ -1,5 +1,6 @@
 <?php
 namespace Qii\Autoloader;
+use \Qii\Exceptions;
 
 class Import
 {
@@ -10,8 +11,8 @@ class Import
     /**
      * require文件
      *
-     * @param string $file 需要require的文件
-     * @return array|bool|void
+     * @param array | string $file 需要require的文件
+     * @return array|bool
      */
     public static function requires($file)
     {
@@ -33,7 +34,7 @@ class Import
     /**
      * 包含文件
      * @param string $file 文件路径
-     * @return mix
+     * @return array | mix[]
      */
     public static function includes($file)
     {
@@ -44,15 +45,13 @@ 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{
-            //print_r(debug_backtrace());
-            throw new \Qii\Exceptions\FileNotFound($file, 404);
+            throw new FileNotFound($file, 404);
         }
-        return false;
+        $configure = include($file);
+        self::setIncludeFiles($file, $configure);
+        return $configure;
     }
 
     /**
@@ -63,18 +62,19 @@ class Import
      */
     public static function requireByClass($className)
     {
-        return \Qii\Autoloader\Psr4::getInstance()->loadFileByClass($className);
+        return Psr4::getInstance()->loadFileByClass($className);
     }
 
     /**
      * 载入文件夹中所有文件
      *
      * @param string $dir 目录
-     * @throws Qii\Exceptions\FileNotFound
+     * @return void
+     * @throws FileNotFound
      */
     public static function requireByDir($dir)
     {
-        if (!is_dir($dir)) throw new \Qii\Exceptions\FileNotFound($dir, 404);
+        if (!is_dir($dir)) throw new FileNotFound($dir, 404);
         $files = self::findFiles($dir, array('php'));
         if (isset($files['php'])) self::requires($files['php']);
     }

+ 5 - 4
src/Autoloader/Instance.php

@@ -1,5 +1,6 @@
 <?php
 namespace Qii\Autoloader;
+use \Qii\Exceptions;
 /**
  * Instance类
  * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-10-22 19:45
@@ -30,7 +31,7 @@ class Instance
     public static function getInstance($className)
     {
         if (isset(self::$loadedClass[self::APP_LOAD_PREFIX . $className])) return self::$loadedClass[self::APP_LOAD_PREFIX . $className];
-        throw new \Qii\Exceptions\CallUndefinedClass(\Qii::i('1105', $className), __LINE__);
+        throw new CallUndefinedClass(\Qii::i('1105', $className), __LINE__);
     }
 
     /**
@@ -44,8 +45,8 @@ class Instance
         if (isset(self::$loadedClass[self::APP_LOAD_PREFIX . $className])
             && self::$loadedClass[self::APP_LOAD_PREFIX . $className]
         ) return self::$loadedClass[self::APP_LOAD_PREFIX . $className];
-        $loader = new \ReflectionClass($className);
         try {
+            $loader = new \ReflectionClass($className);
             $instance = $loader->newInstanceArgs($args);
             self::$loadedClass[self::APP_LOAD_PREFIX . $className] = $instance;
             //如果有_initialize方法就自动调用_initialize方法,并将参数传递给_initialize方法
@@ -65,8 +66,8 @@ class Instance
     {
         $args = func_get_args();
         $className = array_shift($args);
-        $className = \Qii\Autoloader\Psr4::getInstance()->getClassName($className);
-        \Qii\Autoloader\Psr4::getInstance()->loadFileByClass($className);
+        $className = Psr4::getInstance()->getClassName($className);
+        Psr4::getInstance()->loadFileByClass($className);
         return call_user_func_array(array('\Qii\Autoloader\Instance', 'initialize'), array_merge(array($className), $args));
     }
 }

+ 2 - 2
src/Autoloader/Loader.php

@@ -41,7 +41,7 @@ class Loader
      */
     public static function Instance()
     {
-        return \Qii\Autoloader\Instance::instance('\Qii\Autoloader\Loader');
+        return Instance::instance('\Qii\Autoloader\Loader');
     }
 
     /**
@@ -56,7 +56,7 @@ class Loader
         $class = array_shift($args);
         if ($this->lastCall) {
             $className = $this->lastCall . '\\' . $method;
-            \Qii\Autoloader\Import::requireByClass($className);
+            Import::requireByClass($className);
         } else {
             $className = $method . '\\' . $class;
         }

+ 2 - 3
src/Autoloader/Psr4.php

@@ -81,8 +81,7 @@ class Psr4
     /**
      * Setting is use namespaces for class
      *
-     * @param $prefix 以prefix前缀开头的使用namespace
-     * @param bool $useNamespace
+     * @param array $arr namespace设置
      * @return object $this
      */
     public function setUseNamespaces($arr)
@@ -279,7 +278,7 @@ class Psr4
         if(!$this->searchMappedFile($class))
         {
             $notLoaded = isset(self::$lastErrorLoadedFile[$class]) ? self::$lastErrorLoadedFile[$class] : self::getClassName($class);
-            throw new \Qii\Exceptions\FileNotFound($notLoaded, 404);
+            throw new FileNotFound($notLoaded, 404);
         }
     }
 

+ 14 - 11
src/Base/Controller.php

@@ -5,10 +5,13 @@
 
 namespace Qii\Base;
 
+use Qii\Autoloader\Factory;
 use \Qii\Autoloader\Psr4;
 
 use \Qii\Config\Register;
 use \Qii\Config\Consts;
+use Qii\Language\Loader;
+use Qii\View\Intf;
 
 /**
  * Qii_Controller_Abstract class
@@ -21,15 +24,15 @@ abstract class Controller
      */
     public $actions = array();
     /**
-     * @var \Qii\Autoloader\Psr4::getInstance() $load
+     * @var Psr4::getInstance() $load
      */
     public $load;
     /**
-     * @var \Qii\Language\Loader $language
+     * @var Loader $language
      */
     public $language;
     /**
-     * @var Qii\Controller\Base $controller
+     * @var \Qii\Controller\Base $controller
      */
     public $controller;
     /**
@@ -41,7 +44,7 @@ abstract class Controller
      */
     public $actionId = 'index';
     /**
-     * @var Qii\Request\Base $request
+     * @var \Qii\Request\Base $request
      */
     public $request;
     /**
@@ -57,7 +60,7 @@ abstract class Controller
      */
     public $view;
     /**
-     * @var Qii\Cache\Abslute $cache
+     * @var \Qii\Cache\Abslute $cache
      */
     public $cache;
     /**
@@ -78,8 +81,8 @@ abstract class Controller
         $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
         $this->controllerId = $this->request->controller;
         $this->actionId = $this->request->action;
-        $this->language = \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader');
-        $this->response = \Qii\Autoloader\Factory::getInstance('\Qii\Base\Response');
+        $this->language = Factory::getInstance('\Qii\Language\Loader');
+        $this->response = Factory::getInstance('\Qii\Base\Response');
         $this->cache = new \stdClass();
         //载入model
         if ($this->enableDB) {
@@ -151,7 +154,7 @@ abstract class Controller
      */
     public function assign($key, $val = null)
     {
-        if(!$this->view && !($this->view instanceof \Qii\View\Intf)) {
+        if(!$this->view && !($this->view instanceof Intf)) {
             throw new \Exception(_i(6001), __LINE__);
         }
 
@@ -169,7 +172,7 @@ abstract class Controller
      */
     public function render($tpl, $arr = [])
     {
-        if(!$this->view && !($this->view instanceof \Qii\View\Intf)) {
+        if(!$this->view && !($this->view instanceof Intf)) {
             throw new \Exception(_i(6001), __LINE__);
         }
 
@@ -190,7 +193,7 @@ abstract class Controller
      * 设置request
      * @param $request
      */
-    public function setRequest(\Qii\Base\Request $request)
+    public function setRequest(Request $request)
     {
         return $this->request = $request;
     }
@@ -214,7 +217,7 @@ abstract class Controller
             return;
         }
         if ($this->response instanceof \Qii\Base\Response) {
-            if ($this->response->needRender() && $this->view && $this->view instanceof \Qii\View\Intf) {
+            if ($this->response->needRender() && $this->view && $this->view instanceof Intf) {
                 $this->response->setRender($this->view);
             }
             $this->response->response();

+ 9 - 5
src/Base/Dispatcher.php

@@ -1,8 +1,11 @@
 <?php
 namespace Qii\Base;
 
+use \Qii\Autoloader;
+use \Qii\Request;
 use \Qii\Config\Register;
 use \Qii\Config\Consts;
+use \Qii\Exceptions;
 
 class Dispatcher
 {
@@ -18,9 +21,10 @@ class Dispatcher
     }
     /**
      * 设置请求
-     * @param \Qii\Request\Http $request 当前请求
+     * @param Http $request 当前请求
+     * @return $this
      */
-    public function setRequest(\Qii\Request\Http $request)
+    public function setRequest(Http $request)
     {
         $this->request = $request;
         return $this;
@@ -48,7 +52,7 @@ class Dispatcher
             $funcArgs = array_slice($args, 2);
         }
         array_unshift($funcArgs, $controllerName);
-        $psr4 = \Qii\Autoloader\Psr4::getInstance();
+        $psr4 = Psr4::getInstance();
         $controllerCls = call_user_func_array(array($psr4, 'loadClass'), $funcArgs);
 
         $method = new \ReflectionMethod($this, 'dispatch');
@@ -82,7 +86,7 @@ class Dispatcher
                 call_user_func_array(array($this->actionCls, 'initialization'), array($this->actionCls));
             }
             if (!method_exists($this->actionCls, 'run')) {
-                throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $this->controllerCls->actions[$action] . '->run'), __LINE__);
+                throw new MethodNotFound(\Qii::i(1101, $this->controllerCls->actions[$action] . '->run'), __LINE__);
             }
             $response = call_user_func_array(array($this->actionCls, 'run'), array_slice($funcArgs, 1));
         } else {
@@ -93,7 +97,7 @@ class Dispatcher
             array_shift($funcArgs);
             $actionName = $action . Register::get(Consts::APP_DEFAULT_ACTION_SUFFIX);
             if (!method_exists($this->controllerCls, $actionName) && !method_exists($this->controllerCls, '__call')) {
-                throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $controller . '->' . $actionName), __LINE__);
+                throw new MethodNotFound(\Qii::i(1101, $controller . '->' . $actionName), __LINE__);
             }
             $this->controllerCls->response = $response = call_user_func_array(array($this->controllerCls, $actionName), $funcArgs);
         }

+ 19 - 18
src/Base/Request.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace Qii\Base;
+use \Qii\Config;
 
 abstract class Request
 {
@@ -44,7 +45,7 @@ abstract class Request
         foreach ($_GET AS $key => $val) {
             $this->setParam($key, $val);
         }
-        $rewriteRule = \Qii::getInstance()->appConfigure(\Qii\Config\Consts::APP_SITE_METHOD);
+        $rewriteRule = \Qii::getInstance()->appConfigure(Consts::APP_SITE_METHOD);
         $this->url = new \Qii\Request\Url($rewriteRule);
         $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
         $params = (array)$this->url->getParams();
@@ -85,7 +86,7 @@ abstract class Request
      */
     public function defaultController()
     {
-        return \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER, 'index');
+        return Register::get(Consts::APP_DEFAULT_CONTROLLER, 'index');
     }
 
     /**
@@ -93,7 +94,7 @@ abstract class Request
      */
     public function defaultAction()
     {
-        return \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_ACTION, 'index');
+        return Register::get(Consts::APP_DEFAULT_ACTION, 'index');
     }
 
     /**
@@ -226,7 +227,7 @@ abstract class Request
      *
      * @param mixed $name
      * @param mixed $value
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setParam($name, $value = null)
     {
@@ -261,7 +262,7 @@ abstract class Request
      * setParams
      *
      * @param array
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setParams($params)
     {
@@ -287,11 +288,11 @@ abstract class Request
      * setException
      *
      * @param Exception $exception
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setException($exception)
     {
-        if (is_object($exception)
+        if (!empty($exception)
             && ($exception instanceof Exception)
         ) {
             $this->_exception = $exception;
@@ -308,7 +309,7 @@ abstract class Request
      */
     public function getException()
     {
-        if (is_object($this->_exception)
+        if (!empty($this->_exception)
             && ($this->_exception instanceof Exception)
         ) {
             return $this->_exception;
@@ -354,7 +355,7 @@ abstract class Request
      * setModuleName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setModuleName($name)
     {
@@ -370,7 +371,7 @@ abstract class Request
      * setControllerName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return Request
      */
     public function setControllerName($name)
     {
@@ -386,7 +387,7 @@ abstract class Request
      * setActionName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setActionName($name)
     {
@@ -424,7 +425,7 @@ abstract class Request
      * setBaseUri
      *
      * @param string $baseUri
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setBaseUri($baseUri)
     {
@@ -450,7 +451,7 @@ abstract class Request
      * setRequestUri
      *
      * @param string $uri
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setRequestUri($uri)
     {
@@ -487,7 +488,7 @@ abstract class Request
      * setDispatched
      *
      * @param boolean $flag
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setDispatched($flag = true)
     {
@@ -501,9 +502,9 @@ abstract class Request
     /**
      * 设置dispatcher
      *
-     * @param Qii_Controller_Dispatcher $dispatcher
+     * @param Dispatcher $dispatcher
      */
-    public function setDispatcher(\Qii\Base\Dispatcher $dispatcher)
+    public function setDispatcher(Dispatcher $dispatcher)
     {
         $this->dispatcher = $dispatcher;
     }
@@ -523,7 +524,7 @@ abstract class Request
      * setRouted
      *
      * @param boolean $flag
-     * @return boolean | Qii_Request_Abstract
+     * @return boolean | Request
      */
     public function setRouted($flag = true)
     {
@@ -571,7 +572,7 @@ abstract class Request
             return true;
         } elseif ($requestUri && is_string($requestUri)) {
             $scriptFileName = $this->getServer('SCRIPT_FILENAME');
-
+            $basename = "";
             do {
                 if ($scriptFileName && is_string($scriptFileName)) {
                     $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));

+ 22 - 20
src/Base/Response.php

@@ -2,6 +2,9 @@
 
 namespace Qii\Base;
 
+use Qii\Response\Cli;
+use Qii\View\Intf;
+
 class Response
 {
     /**
@@ -32,7 +35,7 @@ class Response
     
     /**
      * Determine to send the headers or not
-     * @var unknown_type
+     * @var bool
      */
     protected $_sendHeader = false;
     
@@ -45,11 +48,11 @@ class Response
     
     /**
      * 设置页面渲染类
-     * @param \Qii\View\Intf $render 渲染类
+     * @param Intf $render 渲染类
      */
-    public function setRender(\Qii\View\Intf $render)
+    public function setRender(Intf $render)
     {
-        \Qii\Base\Response::$render = $render;
+        Response::$render = $render;
     }
     
     /**
@@ -57,7 +60,7 @@ class Response
      *
      * @param string $content
      * @param string $key
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     public function appendBody($body, $key = NULL)
     {
@@ -92,7 +95,7 @@ class Response
     /**
      * Clear headers
      *
-     * @return Qii\Response\Abstract
+     * @return Response
      */
     public function clearHeaders()
     {
@@ -129,7 +132,7 @@ class Response
      *
      * @param string $body
      * @param string $key
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     public function prependBody($body, $key = null)
     {
@@ -172,17 +175,17 @@ class Response
                     $body = $this->data['body'];
                     if (is_array($this->data['body'])) {
                         $body = '';
-                        if (isset($this->data['body']['render']) && $this->data['body']['render'] instanceof \Qii\View\Intf) {
-                            \Qii\Base\Response::$render = $this->data['body']['render'];
+                        if (isset($this->data['body']['render']) && $this->data['body']['render'] instanceof Intf) {
+                            Response::$render = $this->data['body']['render'];
                         }
                         
-                        if (\Qii\Base\Response::$render != null && \Qii\Base\Response::$render instanceof \Qii\View\Intf) {
+                        if (Response::$render != null && Response::$render instanceof Intf) {
                             $tplData = isset($this->data['body']['tplData']) ? $this->data['body']['tplData'] : [];
-                            \Qii\Base\Response::$render->assign($tplData);
-                            $body = \Qii\Base\Response::$render->fetch($this->data['body']['tpl']);
+                            Response::$render->assign($tplData);
+                            $body = Response::$render->fetch($this->data['body']['tpl']);
                         }
                     }
-                    echo(IS_CLI ? (new \Qii\Response\Cli())->stdout($body) : $body);
+                    echo(IS_CLI ? (new Cli())->stdout($body) : $body);
                     break;
             }
             return;
@@ -191,7 +194,7 @@ class Response
             $this->sendHeaders();
         }
         foreach ($this->body as $key => $body) {
-            echo IS_CLI ? new \Qii\Response\Cli($body) : $body;
+            echo IS_CLI ? new Cli($body) : $body;
         }
     }
     
@@ -205,7 +208,7 @@ class Response
      *
      * @param string $body
      * @param string $key
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     public function setBody($body, $key = NULL)
     {
@@ -225,7 +228,7 @@ class Response
      * @param string $name
      * @param string $value
      * @param boolean $replace
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     public function setHeader($name, $value, $replace = false)
     {
@@ -255,7 +258,7 @@ class Response
      * Sets Location header. Forces replacement of any prior redirects.
      *
      * @param string $url
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     public function setRedirect($url)
     {
@@ -290,8 +293,7 @@ class Response
     {
         $filtered = str_replace(array('-', '_'), ' ', (string)$name);
         $filtered = ucwords(strtolower($filtered));
-        $filtered = str_replace(' ', '-', $filtered);
-        return $filtered;
+        return str_replace(' ', '-', $filtered);
     }
     
     /**
@@ -301,7 +303,7 @@ class Response
      * If an {@link setHttpResponseCode() HTTP response code}
      * has been specified, it is sent with the first header.
      *
-     * @return Qii_Response_Abstract
+     * @return Response
      */
     protected function sendHeaders()
     {

+ 0 - 1
src/Base/Rules.php

@@ -34,7 +34,6 @@ class Rules
     public function __construct()
     {
         $this->validate = \_loadClass('\Qii\Library\Validate');
-        $this->constants();
         $this->clean();
     }
 

+ 38 - 7
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,26 +13,57 @@ class Apcu implements Qii_Cache_Intf
         }
     }
 
-    public function set($key, $value, $policy)
+    /**
+     * set
+     * @param string $id
+     * @param mixed $data
+     * @param array|null $policy
+     * @return array|bool
+     */
+    public function set($id, $data, array $policy = null)
     {
         if(is_array($policy))
         {
             $this->policy = array_merge($this->policy, $policy);
         }
-        return apcu_store($key, $value, $this->policy['life_time']);
+        return apcu_store($id, $data, $this->policy['life_time']);
     }
 
-    public function get($key)
+    /**
+     * get
+     * @param $id
+     * @return false|mixed
+     */
+    public function get($id)
     {
-        return apcu_fetch($key);
+        return apcu_fetch($id);
     }
 
+    /**
+     * exists
+     * @param $key
+     * @return bool|string[]
+     */
     public function exists($key)
     {
         return apcu_exists($key);
     }
-    
-    public function del($key)
+
+    /**
+     * clean
+     * @return bool
+     */
+    public function clean()
+    {
+        return apcu_clear_cache();
+    }
+
+    /**
+     * remove
+     * @param $key
+     * @return bool|string[]
+     */
+    public function remove($key)
     {
         return apcu_delete($key);
     }

+ 1 - 1
src/Cache/File.php

@@ -9,7 +9,7 @@
  * $this->cache->get(id);
  * $this->cache->remove(id);
  */
-class Qii_Cache_File implements Qii_Cache_Intf
+class Qii_Cache_File implements Qii\Cache\Intf
 {
     const VERSION = '1.2';
     public $policy = array('path' => 'tmp', 'life_time' => 3600, 'prefix' => 'file');//设置目录、过期时间、文件前缀

+ 8 - 6
src/Cache/Redis.php

@@ -1,6 +1,10 @@
 <?php
 namespace Qii\Cache;
 
+use Qii\Cache\Redis\Cluster;
+use Qii\Exceptions\Errors;
+use Qii\Exceptions\MethodNotFound;
+
 \Qii\Autoloader\Import::requires(array(dirname(__FILE__) . DS . 'Redis/Client.php', dirname(__FILE__) . DS . 'Redis/Cluster.php'));
 
 /**
@@ -32,7 +36,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 +48,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);
     }
 
     /**
@@ -59,11 +63,10 @@ class Redis implements Intf
         try {
             $res = $this->redis->hMset($id, $data);
             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;
     }
@@ -80,11 +83,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;

+ 10 - 10
src/Cache/Redis/Cluster.php

@@ -18,12 +18,12 @@ class Cluster
 {
   /**
    * Collection of \Qii\Cache\Redis\Client objects attached to Redis servers
-   * @var \Qii\Cache\Redis\Client[]
+   * @var Client[]
    */
   protected $clients;
   /**
    * If a server is set as master, all write commands go to that one
-   * @var \Qii\Cache\Redis\Client
+   * @var Client
    */
   protected $masterClient;
   /**
@@ -89,7 +89,7 @@ class Cluster
     foreach ($servers as $server)
     {
       if(is_array($server)){
-          $client = new \Qii\Cache\Redis\Client(
+          $client = new Client(
             $server['host'],
             $server['port'],
             isset($server['timeout']) ? $server['timeout'] : 2.5,
@@ -106,7 +106,7 @@ class Cluster
                 continue;
             }
           }
-      } elseif($server instanceof \Qii\Cache\Redis\Client){
+      } elseif($server instanceof Client){
         $client = $server;
       } else {
           throw new CredisException('Server should either be an array or an instance of \Qii\Cache\Redis\Client');
@@ -139,13 +139,13 @@ class Cluster
   }
 
   /**
-   * @param \Qii\Cache\Redis\Client $masterClient
+   * @param Client $masterClient
    * @param bool $writeOnly
    * @return Credis_Cluster
    */
-  public function setMasterClient(\Qii\Cache\Redis\Client $masterClient, $writeOnly=false)
+  public function setMasterClient(Client $masterClient, $writeOnly=false)
   {
-    if(!$masterClient instanceof \Qii\Cache\Redis\Client){
+    if(!$masterClient instanceof Client){
         throw new CredisException('Master client should be an instance of \Qii\Cache\Redis\Client');
     }
     $this->masterClient = $masterClient;
@@ -166,8 +166,8 @@ class Cluster
    * Get a client by index or alias.
    *
    * @param string|int $alias
-   * @throws CredisException
-   * @return \Qii\Cache\Redis\Client
+   * @return Client
+   *@throws CredisException
    */
   public function client($alias)
   {
@@ -183,7 +183,7 @@ class Cluster
   /**
    * Get an array of all clients
    *
-   * @return array|\Qii\Cache\Redis\Client[]
+   * @return array|Client[]
    */
   public function clients()
   {

+ 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)
     {

+ 2 - 2
src/Conf/namespace.php

@@ -17,7 +17,7 @@ return [
         ['Qii\Exceptions', true],
         ['Qii\Language', true],
         ['Qii\Library', true],
-        ['Qii\Loger', true],
+        ['Qii\Logger', true],
         ['Qii\Plugin', true],
         ['Qii\Request', false],
         ['Qii\Router', true],
@@ -39,7 +39,7 @@ return [
         ['Qii\Exceptions', Qii_DIR . DS . 'Exceptions'],
         ['Qii\Language', Qii_DIR . DS . 'Language'],
         ['Qii\Library', Qii_DIR . DS . 'Library'],
-        ['Qii\Loger', Qii_DIR . DS . 'Loger'],
+        ['Qii\Logger', Qii_DIR . DS . 'Logger'],
         ['Qii\Plugin', Qii_DIR . DS . 'Plugin'],
         ['Qii\Request', Qii_DIR . DS . 'Request'],
         ['Qii\Response', Qii_DIR . DS . 'Response'],

+ 13 - 13
src/Config/Arrays.php

@@ -53,9 +53,9 @@ class Arrays
 	/**
 	 * 直接从数组中获取指定key的值
 	 *
-	 * @param Array $data
-	 * @param String $key
-	 * @return Mix
+	 * @param array $data
+	 * @param string $key
+	 * @return mix
 	 */
 	public function getValueFromArray($data, $key)
 	{
@@ -87,9 +87,9 @@ class Arrays
 	/**
 	 * 实现PHP数组赋值
 	 *
-	 * @param String $key
-	 * @param Mix $value
-	 * @return Array
+	 * @param string $key
+	 * @param mix $value
+	 * @return array
 	 */
 	public function set($key, $value)
 	{
@@ -130,9 +130,9 @@ class Arrays
 	/**
 	 * 实现PHP数组赋值
 	 *
-	 * @param String $key
-	 * @param Mix $value
-	 * @return Array
+	 * @param string $key
+	 * @param mix $value
+	 * @return array
 	 */
 	public function setPrivate($key, $value)
 	{
@@ -142,8 +142,8 @@ class Arrays
 	/**
 	 * 获取通过setPrivate key对应的值
 	 *
-	 * @param String $key
-	 * @return Mix
+	 * @param string $key
+	 * @return mix
 	 */
 	public function get($key)
 	{
@@ -176,8 +176,8 @@ class Arrays
     /**
      * 获取通过setPrivate key对应的值
      *
-     * @param String $key
-     * @return Mix
+     * @param string $key
+     * @return mix
      */
 	public function getPrivate($key) {
 	    return $this->get($key);

+ 1 - 1
src/Config/Consts.php

@@ -8,7 +8,7 @@ namespace Qii\Config;
  */
 class Consts
 {
-	const VERSION = '1.2';
+	const VERSION = '1.3';
 	const APP_SYS = 'Qii';//框架名,用户保存框架实例化对象
 	const APP_ENVIRONS = 'QII_ENVIRONS';//配置文件使用环境
 	const APP_ENVIRON = 'QII_ENVIRON';//当前系统使用的环境

+ 1 - 1
src/Config/Mine.php

@@ -220,7 +220,7 @@ class Mine
      * 通过 $this->get{对应的文件类型} 来获取mineType,或者getAll获取所有的
      *
      * @param $name
-     * @return array|mixed|string
+     * @return array|string
      */
     public function __get($name)
     {

+ 30 - 25
src/Config/Register.php

@@ -17,7 +17,9 @@
  */
 namespace Qii\Config;
 
-use \Qii\Autoloader\Psr4;
+use Qii\Autoloader\Psr4;
+use Qii\Exceptions\InvalidFormat;
+use Qii\Exceptions\Variable;
 
 class Register
 {
@@ -25,7 +27,7 @@ class Register
 	/**
 	 * 存储键值的变量
 	 *
-	 * @var Array
+	 * @var array
 	 */
 	public static $_cache;
 
@@ -41,7 +43,7 @@ class Register
 	 *
 	 * @param String $key
 	 * @param String $val
-	 * @param Bool overwrite 是否覆盖之前保存的值,如果之前保存了值,要想再保存需要额外设置它为true,否则不让保存
+	 * @param bool overwrite 是否覆盖之前保存的值,如果之前保存了值,要想再保存需要额外设置它为true,否则不让保存
 	 */
 	public static function set($key, $val, $overwrite = true)
 	{
@@ -52,9 +54,10 @@ class Register
 	/**
 	 * 设置键
 	 *
-	 * @param String $index
-	 * @param String $key
-	 * @param String $val
+	 * @param string $index
+	 * @param string $key
+	 * @param string $val
+     * @throws
 	 */
 	public static function add($index, $key, $val)
 	{
@@ -65,7 +68,7 @@ class Register
 
 	/**
 	 * 移除某一个key
-	 * @param String $key
+	 * @param string $key
 	 */
 	public static function remove($key)
 	{
@@ -76,14 +79,14 @@ class Register
 	/**
 	 * 获取保存的值
 	 *
-	 * @param String $key
-	 * @param String $index
-	 * @param Mix $default
-	 * @return Mix
+	 * @param string $key
+	 * @param mixed $default
+	 * @return mixed
+     * @throws
 	 */
 	public static function get($key, $default = null)
 	{
-		if (!$key) throw new \Qii\Exceptions\Variable(\Qii::i(1003), __LINE__);
+		if (!$key) throw new Variable(\Qii::i(1003), __LINE__);
 		//优先调用存在的get方法
 		$method = 'get' . $key;
 		if (method_exists('\Qii\Config\Register', $method)) return Register::$method();
@@ -97,9 +100,10 @@ class Register
 	/**
 	 * 通过\Qii\Config\Register::$key($defaultVal)来获取内容
 	 *
-	 * @param String $method
-	 * @param Array $argvs
-	 * @return Mix
+	 * @param string $method
+	 * @param array $argvs
+	 * @return mix
+     * @throws
 	 */
 	public static function __callStatic($method, $argvs)
 	{
@@ -109,9 +113,9 @@ class Register
 
 	/**
 	 * 整理数组,将0.key 最后会整理到 [0][key]中
-	 * @param Array $array
-	 * @return multitype:
-	 */
+	 * @param array $array
+	 * @return array
+     */
 	public static function feval($array)
 	{
 		$data = array();
@@ -130,14 +134,15 @@ class Register
 	/**
 	 * 读取ini配置文件
 	 *
-	 * @param String $fileName
-	 * @return Array
+	 * @param string $fileName
+	 * @return array
+     * @throws
 	 */
 	public static function ini($fileName)
 	{
-		if (!$fileName) throw new \Qii\Exceptions\Variable(\Qii::i(1408), __LINE__);
+		if (!$fileName) throw new Variable(\Qii::i(1408), __LINE__);
 		$ini = parse_ini_file($fileName, true);
-		if (!$ini) throw new \Qii\Exceptions\InvalidFormat($fileName, __LINE__);
+		if (!$ini) throw new InvalidFormat($fileName, __LINE__);
 		$config = array();
 		foreach ($ini AS $namespace => $properties) {
 			$properties = Register::feval($properties);
@@ -159,7 +164,7 @@ class Register
                     if(stristr($space, '.')) {
                         if (isset($config[$space])) $config[$name] = $config[$space];
                     }else{
-                        $config[$name] = $config[$name] ?? [];
+                        $config[$name] = isset($config[$name]) && is_array($config[$name]) ? $config[$name] : [];
                         if (isset($config[$space])) $config[$name] = array_merge($config[$space], $config[$name], $properties);
                     }
 				}
@@ -354,7 +359,7 @@ class Register
 
 	/**
 	 * 获取当前系统环境
-	 * @return Ambigous <Mix, multitype:>
+	 * @return string
 	 */
 	public static function getAppEnviron()
 	{
@@ -363,7 +368,7 @@ class Register
                     : Consts::APP_DEFAULT_ENVIRON;
 	}
 
-	public function __call($method, $argvs)
+	public function __call($method, $args)
 	{
 	}
 }

+ 5 - 3
src/Driver/Base.php

@@ -1,6 +1,8 @@
 <?php
 
 namespace Qii\Driver;
+use Qii\Autoloader\Psr4;
+
 /**
  * Class Base
  * @package Qii\Driver
@@ -78,9 +80,9 @@ class Base
 
     public function __construct()
     {
-        $this->language = \Qii\Autoloader\Psr4::getInstance()->loadClass('Qii\Language\Loader');
-        $this->load = \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
-        $this->response = new \Qii\Driver\Response();
+        $this->language = Psr4::getInstance()->loadClass('Qii\Language\Loader');
+        $this->load = Psr4::getInstance()->loadClass('\Qii\Autoloader\Loader');
+        $this->response = new Response();
     }
     /**
      * 获取所有数据

+ 20 - 5
src/Driver/ConnBase.php

@@ -10,10 +10,17 @@ class ConnBase
 {
 	const VERSION = '1.2';
 	/**
-	 * @var $allowExec  读写对应的数据库配置文件
+	 * @var array $allowExec  读写对应的数据库配置文件
 	 */
 	public $allowExec = array("WRITE" => 'master', 'READ' => 'slave');
-
+    /**
+     * @var array 数据库相关配置
+     */
+    protected $_dbInfo = array();
+    /**
+     * @var array 用于存储数据库链接
+     */
+    public $_connections;
 	/**
 	 * 获取数据库配置中指定的key的值,不指定则获取全部
 	 * @param string key 数据库配置中指定的key
@@ -26,14 +33,12 @@ class ConnBase
 
 	/**
 	 * 通过sql语句判断是读还是写操作
-	 * @param $sql  读/写的sql语句
+	 * @param string $sql  读/写的sql语句
 	 */
 	protected function prepare($sql)
 	{
 		$default = "READ";
-		//$readMode = "/^SELECT\s/u";
 		$writeMode = "/^(UPDATE)|(REPLACE)|(DELETE)|(INSERT)\s/u";
-		//$isRead = preg_match($readMode, $sql);
 		$isWrite = preg_match($writeMode, $sql);
 		if ($isWrite) $default = "WRITE";
 		if (!isset($this->allowExec[$default])) $default = 'WRITE';
@@ -66,4 +71,14 @@ class ConnBase
         }
         return $connection;
 	}
+
+    public function getReadConnection()
+    {
+
+    }
+
+    public function getWriteConnection()
+    {
+
+    }
 }

+ 1 - 1
src/Driver/Pdo/Connection.php

@@ -31,7 +31,7 @@ class Connection extends \Qii\Driver\ConnBase implements \Qii\Driver\ConnIntf
 
 	/**
 	 * 获取读数据的连接资源
-	 * @return res
+	 * @return \PDO
 	 */
 	public function getReadConnection()
 	{

+ 7 - 7
src/Loger/Instance.php → src/Logger/Instance.php

@@ -11,7 +11,7 @@ class Instance
 	/**
 	 * loger writer
 	 */
-	private $logerHooker;
+	private $loggerHooker;
 
 	public function __construct(Writer $hooker)
 	{
@@ -23,7 +23,7 @@ class Instance
 	 */
 	public function setHooker(Writer $hooker)
 	{
-		$this->logerHooker = $hooker;
+		$this->loggerHooker = $hooker;
 	}
 
 	/**
@@ -31,18 +31,18 @@ class Instance
 	 */
 	public function setFileName($fileName)
 	{
-		if (method_exists($this->logerHooker, 'setFilename')) {
-			$this->logerHooker->setFilename($fileName);
+		if (method_exists($this->loggerHooker, 'setFilename')) {
+			$this->loggerHooker->setFilename($fileName);
 		}
 	}
 
 	/**
 	 * 调用写日志的方法
 	 */
-	public function writeLog($loger)
+	public function writeLog($logger)
 	{
-		if (method_exists($this->logerHooker, 'writeLog')) {
-			$this->logerHooker->writeLog($loger);
+		if (method_exists($this->loggerHooker, 'writeLog')) {
+			$this->loggerHooker->writeLog($logger);
 		}
 	}
 }

+ 2 - 2
src/Loger/Writer.php → src/Logger/Writer.php

@@ -1,8 +1,8 @@
 <?php
-namespace Qii\Loger;
+namespace Qii\Logger;
 
 interface Writer
 {
 	public function setFileName($fileName);
-	public function writeLog($loger);
+	public function writeLog($logger);
 }

+ 5 - 5
src/Qii.php

@@ -6,17 +6,17 @@ define('Qii_DIR', dirname(__FILE__));
 /**
  * DIRECTORY_SEPARATOR 的简写
  */
-define('DS', DIRECTORY_SEPARATOR);
+const DS = DIRECTORY_SEPARATOR;
 /**
  * 定义包含的路径分隔符
  */
-define('PS', PATH_SEPARATOR);
+const PS = PATH_SEPARATOR;
 /**
  * 定义操作系统类型
  */
 define('OS', strtoupper(substr(PHP_OS, 0, 3)));
 
-define('IS_CLI', php_sapi_name() == 'cli' ? true : false);
+define('IS_CLI', php_sapi_name() == 'cli');
 if (IS_CLI) {
     define('PATH_INFO', array_pop($argv));
 } else {
@@ -199,8 +199,8 @@ if (!function_exists('catch_fatal_error')) {
 $namespace = _include(Qii_DIR . DS . 'Conf' . DS . 'namespace.php');
 \Qii\Autoloader\Psr4::getInstance()
     ->register()
-    ->setUseNamespaces($namespace['setUseNamespace'] ?? [])
-    ->addNamespaces($namespace['addNamespace'] ?? []);
+    ->setUseNamespaces(isset($namespace['setUseNamespace']) && is_array($namespace['setUseNamespace']) ? $namespace['setUseNamespace'] : [])
+    ->addNamespaces(isset($namespace['addNamespace']) && is_array($namespace['addNamespace']) ? $namespace['addNamespace']: []);
 
 //加载默认语言包
 \Qii\Autoloader\Factory::getInstance('\Qii\Language\Loader')->load('error', Qii_DIR . DS . 'Language');