Просмотр исходного кода

Update: add static path and fix some bugs

朱金辉 1 год назад
Родитель
Сommit
7ae049bcfb

+ 25 - 4
src/Application.php

@@ -52,6 +52,7 @@ class Application
      */
     public static $paths = array('configure', 'controller', 'model', 'middleware', 'helper', 'view', 'plugins', 'tmp');
 
+    public static $staticPath = array();
     /**
      * 系统注册的 router
      * @var array
@@ -190,7 +191,20 @@ class Application
     {
         return self::$workspace;
     }
-
+    /**
+     * 静态文件目录不做处理,直接返回404
+     *
+     * @param array $path
+     * @return void
+     */
+    public function setStaticPath($path) {
+        if(!is_array($path)) {
+            self::$staticPath[] = $path;
+            return $this;
+        }
+        self::$staticPath = array_merge(self::$staticPath, $path);
+        return $this;
+    }
     /**
      * 获取网站的配置文件
      * @return Config\Mix
@@ -375,11 +389,10 @@ class Application
         $loader = new Loader($engine);
         return $loader->initialization($policy);
     }
-
     /**
      * 获取缓存的策略
-     * @param String $cache 缓存的内容
-     * @return multitype:multitype:Ambigous <>
+     * @param string $cache 缓存的内容
+     * @return array
      */
     public function getCachePolicy($cache)
     {
@@ -545,6 +558,14 @@ class Application
         if ($next === false) {
             return $this;
         }
+        //静态文件夹直接返回错误,不做任何处理
+        if($this->request->getRequestUri() == '/favicon.ico' || count(self::$staticPath) > 0
+            && in_array(explode('\\', $this->request->controller,  2)[0], self::$staticPath)) {
+            header("HTTP/1.1 404 Not Found");
+            header("Status: 404 Not Found");
+            die('404 Not Found');
+        }
+
         //route middleware
         $next = call_user_func(
             array_reduce(['Route'], function($carry, $item){

+ 0 - 5
src/Base/Action.php

@@ -9,11 +9,6 @@ class Action extends Controller
     public $controllerId;
     public $actionId;
     public $response;
-    public function __construct()
-    {
-        parent::__construct();
-    }
-
     public function __call($method, $args)
     {
     	return call_user_func_array(array($this->controller, $method), $args);

+ 16 - 22
src/Base/Request.php

@@ -285,20 +285,18 @@ abstract class Request
      *
      * @param mixed $name
      * @param mixed $value
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setParam($name, $value = null)
     {
         if (is_null($value)) {
             if (is_array($name)) {
                 $this->params = array_merge($this->params, $name);
-                return $this;
             }
         } elseif (is_string($name)) {
             $this->params[$name] = $value;
-            return $this;
         }
-        return false;
+        return $this;
     }
 
     /**
@@ -319,16 +317,15 @@ abstract class Request
     /**
      * setParams
      *
-     * @param array
-     * @return boolean | Qii_Request_Abstract
+     * @param array $params
+     * @return $this
      */
     public function setParams($params)
     {
         if (is_array($params)) {
             $this->params = $params;
-            return $this;
         }
-        return false;
+        return $this;
     }
 
     /**
@@ -346,7 +343,7 @@ abstract class Request
      * setException
      *
      * @param Exception $exception
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setException($exception)
     {
@@ -354,9 +351,8 @@ abstract class Request
             && ($exception instanceof Exception)
         ) {
             $this->_exception = $exception;
-            return $this;
         }
-        return false;
+        return $this;
     }
 
     /**
@@ -413,13 +409,13 @@ abstract class Request
      * setModuleName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setModuleName($name)
     {
         if (!is_string($name)) {
             trigger_error('Expect a string module name', E_USER_WARNING);
-            return false;
+            return $this;
         }
         $this->module = $name;
         return $this;
@@ -429,7 +425,7 @@ abstract class Request
      * setControllerName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setControllerName($name)
     {
@@ -445,13 +441,13 @@ abstract class Request
      * setActionName
      *
      * @param string $name
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setActionName($name)
     {
         if (!is_string($name)) {
             trigger_error('Expect a string action name', E_USER_WARNING);
-            return false;
+            return $this;
         }
         $this->action = $name;
         return $this;
@@ -483,15 +479,14 @@ abstract class Request
      * setBaseUri
      *
      * @param string $baseUri
-     * @return boolean | Qii\Request\Abstract
+     * @return $this
      */
     public function setBaseUri($baseUri)
     {
         if ($baseUri && is_string($baseUri)) {
             $this->_baseUri = $baseUri;
-            return $this;
         }
-        return false;
+        return $this;
     }
 
     /**
@@ -509,15 +504,14 @@ abstract class Request
      * setRequestUri
      *
      * @param string $uri
-     * @return boolean | Qii_Request_Abstract
+     * @return $this
      */
     public function setRequestUri($uri)
     {
         if (is_string($uri)) {
             $this->uri = $uri;
-            return $this;
         }
-        return false;
+        return $this;
     }
 
     /**

+ 4 - 2
src/Base/Route.php

@@ -155,7 +155,9 @@ class Route
             if(!isset($subRoute[$method]) && !isset($subRoute['ANY'])) {
                 continue;
             }
-            $routes = array_merge((array) $subRoute[$method], (array) $subRoute['ANY']);
+            $subRoute['ANY'] = isset($subRoute['ANY']) ? (array) $subRoute['ANY'] : array();
+            $subRoute[$method] = isset($subRoute[$method]) ? (array) $subRoute[$method] : array();
+            $routes = array_merge($subRoute[$method], $subRoute['ANY']);
             $callable = '';
 
             foreach ($routes as $routeUri) {
@@ -171,7 +173,7 @@ class Route
         if (!$callable || count($middleware) == 0) {
             return $next($request);
         }
-        //命中后,执行相应程序后就不网下周
+        //命中后,执行相应程序后就不往下执行
         $middleware = array_reverse(array_unique($middleware));
         $result = array_reduce($middleware, function ($carry, $item){
             return function () use ($carry, $item){

+ 1 - 1
src/Base/Rules.php

@@ -484,7 +484,7 @@ class Rules
                 continue;
             }
             if(count($value) == 1) {
-                $value[1] = $value[0] .'格式不正确';
+                $value[1] = (isset($value[0]) ? $value[0] : '') .'格式不正确';
                 $value[2] = true;
             }else if(count($value) == 2) {
                 $value[2] = true;

+ 3 - 3
src/Config/Register.php

@@ -41,9 +41,9 @@ class Register
 	/**
 	 * 设置键值
 	 *
-	 * @param String $key
-	 * @param String $val
-	 * @param Bool overwrite 是否覆盖之前保存的值,如果之前保存了值,要想再保存需要额外设置它为true,否则不让保存
+	 * @param string $key
+	 * @param mixed $val
+	 * @param bool overwrite 是否覆盖之前保存的值,如果之前保存了值,要想再保存需要额外设置它为true,否则不让保存
 	 */
 	public static function set($key, $val, $overwrite = true)
 	{

+ 1 - 1
src/Driver/Base.php

@@ -669,7 +669,7 @@ class Base
         if (ini_get("magic_quotes_gpc")) {
             return $word;
         }
-        if(in_array(gettype($val), array("object", "resource","resource (closed)"))) {
+        if(in_array(gettype($word), array("object", "resource","resource (closed)"))) {
             throw new \Exception('期待参数为数组或字符串,获取到的是:'. gettype($word)."(". json_encode($word) .")");
         }
         return is_array($word) ? array_map('addslashes', $word) : addslashes($word);

+ 4 - 4
src/Driver/Entity/Base.php

@@ -73,7 +73,7 @@ class Base {
      */
     public function getWhereHooker() {
         $where = $this->properties();
-        if($this->hooker['where'] && is_callable($this->hooker['where']['func'])) {
+        if(isset($this->hooker['where']) && $this->hooker['where'] && is_callable($this->hooker['where']['func'])) {
             $where = call_user_func($this->hooker['where']['func'], $this->properties(), $this->hooker['where']['args']);
         }
         return $where;
@@ -108,7 +108,7 @@ class Base {
      * @return int|mixed
      */
     final public function getLimitHooker() {
-        if($this->hooker['limit'] && is_callable($this->hooker['limit']['func'])) {
+        if(isset($this->hooker['limit']) && $this->hooker['limit'] && is_callable($this->hooker['limit']['func'])) {
             return call_user_func($this->hooker['limit']['func'], [], $this->hooker['limit']['args']);
         }
         return [];
@@ -133,7 +133,7 @@ class Base {
      * @return mixed|null[]
      */
     final public function getCacheHooker() {
-        if($this->hooker['cache'] && is_callable($this->hooker['cache']['func'])) {
+        if(isset($this->hooker['cache']) && $this->hooker['cache'] && is_callable($this->hooker['cache']['func'])) {
             return call_user_func($this->hooker['cache']['func'], [], $this->hooker['cache']['args']);
         }
         return [null, null, null];
@@ -195,7 +195,7 @@ class Base {
      * @return mixed|string
      */
     final public function getFieldsHooker() {
-        if($this->hooker['fields'] && is_callable($this->hooker['fields']['func'])) {
+        if(isset($this->hooker['fields']) && $this->hooker['fields'] && is_callable($this->hooker['fields']['func'])) {
             return call_user_func($this->hooker['fields']['func'], [], $this->hooker['fields']['args']);
         }
         return '*';

+ 1 - 1
src/Qii.php

@@ -25,7 +25,7 @@ define('OS', strtoupper(substr(PHP_OS, 0, 3)));
 
 define('IS_CLI', php_sapi_name() == 'cli' ? true : false);
 if (IS_CLI) {
-    $args = is_array($argv) && count($argv) > 0 ? $argv : array();
+    $args = isset($argv) && is_array($argv) && count($argv) > 0 ? $argv : array();
     if(count($args) == 0 && isset($_SERVER['argv'])
         && is_array($_SERVER['argv']) && count($_SERVER['argv']) > 0) {
         $args = $_SERVER['argv'];