From 76716a689739b10b3845392b9150b8258c444402 Mon Sep 17 00:00:00 2001 From: moling <365024424@qq.com> Date: Thu, 15 Sep 2016 11:15:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=95=B4=E5=90=88=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=B1=BB=E7=9A=84get=E5=92=8Call=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原来的get方法有bug,当缓存过的文件但配置项的名不对时,没有正确的返回false,而get和all的代码重复率太高,完全可以整合重用。 --- core/ppphp/conf.php | 47 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/core/ppphp/conf.php b/core/ppphp/conf.php index ec2723c..f70dfc2 100755 --- a/core/ppphp/conf.php +++ b/core/ppphp/conf.php @@ -14,46 +14,29 @@ class conf static public $conf = array(); /** - * 加载系统配置,如果之前已经加载过,那么就直接返回 - * @param string $name 配置名 + * 加载系统配置文件,如果之前已经加载过,那么就直接返回所有配置或其中一个配置 * @param string $file 文件名 - * @return mix + * @param string $name 配置名 + * @return mixed 找不到配置时返回false,当$name为空时返回文件所有配置,否则只返回$name这配置项 */ - static public function get($name,$file='conf') + static public function get($file='conf', $name=null) { - if(isset(self::$conf[$file][$name])) { - return self::$conf[$file][$name]; - } else { - $conf = CORE.'config/'.$file.'.php'; - if(is_file($conf)) { - self::$conf[$file] = include $conf; - return isset(self::$conf[$file][$name])?self::$conf[$file][$name]:false; - } else { + // 判断配置没有缓存的话就导入配置文件并缓存 + if (!isset(self::$conf[$file])) + // 判断配置文件是否存在 + $conf = CORE.'config/' . $file . '.php'; + if (!is_file($conf)) { return false; } + + self::$conf[$file] = include $conf; } - } - - /** - * 加载系统配置文件(直接加载整个配置文件),如果之前已经加载过,那么就直接返回 - * @param string $name 配置名 - * @param string $file 文件名 - * @return mix - */ - static public function all($file) - { - if(isset(self::$conf[$file])) { + // 如果$name为null则返回所有的设置 + if (is_null($name)) { return self::$conf[$file]; - } else { - $conf = CORE.'config/'.$file.'.php'; - if(is_file($conf)) { - self::$conf[$file] = include $conf; - return self::$conf[$file]; - } else { - return false; - } } + // 判断配置项是否存在并返回 + return isset(self::$conf[$file][$name]) ? self::$conf[$file][$name] : false; } -} \ No newline at end of file From 622f3af6509298f781c712ce771c900203a655b7 Mon Sep 17 00:00:00 2001 From: moling <365024424@qq.com> Date: Thu, 15 Sep 2016 11:20:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?conf=E7=B1=BB=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/ppphp/route.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/ppphp/route.php b/core/ppphp/route.php index 4bcadd8..2bf84e7 100755 --- a/core/ppphp/route.php +++ b/core/ppphp/route.php @@ -13,7 +13,7 @@ class route public $route; public function __construct() { - $route = conf::all('route'); + $route = conf::get('route'); if(isset($_SERVER['REQUEST_URI'])) { $pathstr = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['REQUEST_URI']); //丢掉?以及后面的参数 @@ -56,8 +56,8 @@ public function __construct() } } else { - $this->ctrl = conf::get('DEFAULT_CTRL','route'); - $this->action = conf::get('DEFAULT_ACTION','route'); + $this->ctrl = conf::get('route', 'DEFAULT_CTRL'); + $this->action = conf::get('route', 'DEFAULT_ACTION'); } } @@ -69,4 +69,4 @@ public function urlVar($num,$default = false) return $default; } } -} \ No newline at end of file +}