配置类的制作
3:接下来,我们可以测试下这个配置加载类对不对,我门在控制器里面写上
接下,我门说下这个日志类
2:我门写个日志的配置文件,新建个文件夹
3:再建一个文件,这个是日志的存储位置
好了!就这么简单,大家如果配置中有什么的问题的话,可以留言,我会一一解决的!
1:先创建个conf.php,写入
<?php
namespace core\lib;
class conf{
static public $conf=array();
/*
* 1:判断配置文件是否存在
* 2:判断配置是否存在
* 3:缓存配置
*/
static public function get($name,$file)
{
if (isset(self::$conf[$file])){
return self::$conf[$file][$name];
}else{
$path = IMOOC . '\core\config\\' . $file . '.php';
if (is_file($path)) {
$conf = include $path;
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{
$path = IMOOC . '\core\config\\' . $file . '.php';
if (is_file($path)) {
$conf = include $path;
self::$conf[$file]=$conf;
return $conf;
} else {
throw new \Exception('找不到配置文件'.$file);
}
}
}
}
?>
2:接下来,新建一个路由的配置文件,log.php,
<?php
return array(
'DRIVE'=>'file',
'OPTION'=>array(
'PATH'=>IMOOC.'/log/'
),
);
3:接下来,我们可以测试下这个配置加载类对不对,我门在控制器里面写上
$tem=\core\lib\conf::get('CTRL','route');
$tem=\core\lib\conf::get('ACTION','route');
print_r($tem);
4:新建个文件,mysql.php,我门把数据库里面的配置也写成配置的
<?php
return array(
'database_type' => 'mysql',
'database_name' => 'siji',
'server' => 'localhost',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8'
);
?>
5:我门在model里面写上
$database=conf::all('database');
try{
parent::__construce($database['DSN'],$database['username'],$database['password']);
}catch(\PDOException $e){
p($e->getMessage());
}
接下,我门说下这个日志类
1:先建个log.php,写入
<?php
namespace core\lib;
use core\lib\conf;
class log{
/*
* 1::确定日志的存储方式
* 2写日志
*/
static $class;
static public function index(){
//确定储存方式
$drive=conf::get('DRIVE','log');
$class='\core\lib\drive\log\\'.$drive;
// echo $class;
self::$class=new $class;
}
static public function log($name,$file='log'){
self::$class->log($name,$file);
}
}
2:我门写个日志的配置文件,新建个文件夹
<?php
return array(
'DRIVE'=>'file',
'OPTION'=>array(
'PATH'=>IMOOC.'/log/'
),
);
3:再建一个文件,这个是日志的存储位置
<?php
namespace core\lib\drive\log;
//文件系统
use core\lib\conf;
class file{
public $path;//日志存储位置
public function __construct()
{
$ber=conf::Get('OPTION','log');
$this->path=$ber['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($this->path.date('YmdH').'/'.$file.'.php');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);
}
}
好了!就这么简单,大家如果配置中有什么的问题的话,可以留言,我会一一解决的!