MVC的简单搭建之配置类以及日志类

来源:互联网 发布:淘宝卖家装修教程 编辑:程序博客网 时间:2024/05/19 02:00

今天的内容是搭建MVC的配置类以及日志类

首先是配置类

   配置类呢我有两个文件

   第一个

            

<?phpnamespace core\lib;class conf{    static public $conf = array();    static public function get($name,$file)    {        /*         * 判断配置文件是否存在         * 判断配置是否存在         * 缓存配置         * */        if(isset(self::$conf[$file]))        {            return self::$conf[$file][$name];        }        else        {            $path = IMOOC.'/core/config/'.$file.'.php';            // p($file);exit;            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);            }        }    }}?>  还有一个是路由
<?phpreturn array(    'CTRL'=>'index',    'ACTION'=>'index')?>
想要测试配置是否成功的话,就在默认的控制器里加载这个类就可以了




接下来是日志类

日志类的存储方式分为两种,一种是文件方式,另一种是数据库方式,我做的是文件方式,


file.php 这是一个文件驱动
<?phpnamespace core\lib\drive\log;use core\lib\conf;class file{    public $path;  #日志的存储位置    public function __construct()    {        $conf= conf::get('OPTION','log');        $this->path = $conf['PATH'];    }    public function log($message,$file = 'log')    {        /*         * 确定文件存储位置是否存在         * 新建目录         * 写入日志         * */        //p($this->path);exit();        if(!is_dir($this->path.date('YmdH')))        {            mkdir($this->path.date('YmdH'),'0777',true);        }       //return file_put_contents($this->path.date('YmdH').$file.'.php',date('Y-m-d H:i:s').json_encode($message).PHP_EOL,FILE_APPEND);        return file_put_contents($this->path.date('YmdH').'/'.$file.'.php',date('Y-m-d H:i:s').json_encode($message).PHP_EOL,FILE_APPEND);    }}?>这个文件是按照时间去存储的当然不要忘记在imooc.php里面去启动它
\core\lib\log::init();\core\lib\log::log($_SERVER,'server');




config\log.php 这就是写一个路径
<?phpreturn array(    'DRIVE'=>'file',    'OPTION' => array(        'PATH' => IMOOC.'/log/'    ));?>




最后在lib\log.php
<?phpnamespace core\lib;use core\lib\conf;class log{    static $class;    /*     * 确定日志贮存方式     *     * 写日志     * */    static public function init()    {        //确定储存方式        $drive = conf::get('DRIVE','log');        $class = '\core\lib\drive\log\\'.$drive;        self::$class = new $class;    }    static public function log($name,$file = 'log')    {        self::$class ->log($name,$file);    }}?>


好了,以上就是我今天关于MVC的学习了




0 0