3-1
配置加载类:
配置加载类可以提高我们框架的健壮性,之前我们所有的配置都是单一存放在文件当中,这次,我们把配置都放在一个文件当中,通过这个文件调用我们所需的配置,这样可以简化我们的框架,
1. 新建一个conf类,再建一个配置文件,在这个配置文件中定义我们默认加载的控制器名和方法名,在conf类中,首先定义一个数组,用来存放我们的所要用到的配置,接下来,判断对应的配置会否存在,最后,如果存在,把这个配置缓存在数组中
namespace core\lib;
class conf{
static public $conf = array();
static public function get($name,$file){
/*1
1,判断配置文件是否存在
2. 判断对应的配置是否存在
3. 缓存配置
2*/
if(isset(self::$conf[$file])){
return self::$conf[$file][$name];
}else {
//p(1);
$path = LTY . '\core\config\\' . $file . '.php';
if (is_file($path)) {
$conf = include $path;
//p($conf[$name]);die;
if (isset($conf[$name])) {
self::$conf[$file] = $conf; //缓存配置
return $conf[$name]; //缓存成功后返回配置项
} else {
throw new \Exception('没有这个配置项'.$name);
}
} else {
throw new \Exception('找不到配置文件'.$file);
}
}
}
static public function all($file)
{
if (isset(self::$conf[$file])) {
return self::$conf[$file];
} else {
//p(1);
$path = LTY . '\core\config\\' . $file . '.php';
if (is_file($path)) {
$conf = include $path;
self::$conf[$file] = $conf;
return $conf;
} else {
throw new \Exception('找不到配置文件' . $file);
}
}
}
}
其中all方法是加载的所有的配置。
3-2
日志类
一般的日志会存在文件中或者数据库当中,先创建日志类,
首先,先判断 日志的存储方式,接下来就是写日志,
1. 建一个驱动,drive/log/log.php,用来定义我们的日志以及存放的位置
class file{
public $path; #日志存储位置
public function __construct(){
$conf = conf::get('OPTION','log');
$this->path = $conf['PATH'];
}
public function log($message,$file='log'){
/*
* 1. 确定文件存储位置是否存在
* 新建目录
* 2. 写入日志
* */
//p($this->path);die;
if(!is_dir($this->path.date('YmdH'))){
mkdir($this->path.date('YmdH'),'0777',true);
}
//p($message);die;
return file_put_contents($this->path.date('YmdH').'/'.$file.'.php',date('Y-m-d H:i:s').json_encode($message).PHP_EOL,FILE_APPEND);
}
}
在建立log文件夹,撰文存放日志,以每小时为单位的日志名称来创建文件。