Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49435
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 142
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-16 21:18
文章分类

全部博文(12)

文章存档

2015年(1)

2014年(1)

2013年(10)

我的朋友

分类: PHP

2013-09-20 00:54:15

  接下来就是config类了,这个类的作用就是能够对配置项进行修改,覆盖,提取,别的功能其实也不太需要了.对了,这里还有一个是否启用分组的功能,其实很简单,就是一个二维数组,启用分组就是分组又是一个数组,而具体的配置项就是二维数组,很简单吧.

点击(此处)折叠或打开

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. //该类负责参数配置类,使用该类可以动态的配置参数.
  3. class CI_Config {

  4.     var $config = array();
  5.     //已经加载的配置
  6.     var $is_loaded = array();
  7.     //配置文件的路径形成的数组.
  8.     var $_config_paths = array(APPPATH);

  9.     /**
  10.     构造函数,把config.php里的配置作为一个类的变量赋给$config.
  11.     然后修正base_url这个变量.
  12.      */
  13.     function __construct()
  14.     {
  15.         $this->config =& get_config();
  16.         log_message('debug', "Config Class Initialized");

  17.         //如果没有提供base_url,则自动设置它.
  18.         if ($this->config['base_url'] == '')
  19.         {
  20.             if (isset($_SERVER['HTTP_HOST']))
  21.             {
  22.                 //判断是http还是https.
  23.                 $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  24.                 //然后加上主机的名称.
  25.                 $base_url .= '://'. $_SERVER['HTTP_HOST'];
  26.                 //再加上脚本的地址.
  27.                 $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  28.             }

  29.             else
  30.             {
  31.                 $base_url = '';
  32.             }

  33.             $this->set_item('base_url', $base_url);
  34.         }
  35.     }

  36.     
  37.     /**
  38.      * 加载配置文件.
  39.      第一个参数 为字符串类型 为配置文件的名字,
  40.      第二个参数 布尔类型 是否启用分段的形式,即一个二维数组,
  41.      不过这里的一个问题就是这个二维数组的格式是给定的,不能自由的变化.
  42.      第三个参数 布尔类型 为true则出错时只返回false,为false时则显示具体信息.
  43.      返回值为布尔类型,说明文件是否成功加载.
  44.      */
  45.     function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  46.     {
  47.     //如果文件名为空,则使用config.php文件.注意这里都统一去掉了后缀.
  48.         $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
  49.         //设置一个是否加载成功的变量.
  50.         $loaded = FALSE;

  51.         foreach ($this->_config_paths as $path)
  52.         {    
  53.         //对于每一个路径,得到相应的文件路径.
  54.             $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
  55.             //如果已经加载,则直接返回.
  56.             if (in_array($file_path, $this->is_loaded, TRUE))
  57.             {
  58.                 $loaded = TRUE;
  59.                 continue;
  60.             }
  61.             //如果文件不存在.
  62.             if ( ! file_exists($file_path))
  63.             {
  64.                 //首先向日志里写入错误信息.然后尝试使用全局的配置文件.
  65.                 log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
  66.                 $file_path = $path.'config/'.$file.EXT;
  67.                 
  68.                 if ( ! file_exists($file_path))
  69.                 {
  70.                     continue;
  71.                 }
  72.             }
  73.             //走到这一步说明文件未加载,且文件存在,先把它包含进来.        
  74.             include($file_path);
  75.             //如果$config不存在或者它不是数组,判断是否需要给出错误.
  76.             if ( ! isset($config) OR ! is_array($config))
  77.             {
  78.                 if ($fail_gracefully === TRUE)
  79.                 {
  80.                     return FALSE;
  81.                 }
  82.                 show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  83.             }
  84.             //如果使用了段的形式,则加载该段的配置参数.
  85.             if ($use_sections === TRUE)
  86.             {
  87.                 if (isset($this->config[$file]))
  88.                 {
  89.                     $this->config[$file] = array_merge($this->config[$file], $config);
  90.                 }
  91.                 else
  92.                 {
  93.                     $this->config[$file] = $config;
  94.                 }
  95.             }
  96.             else
  97.             {
  98.                 $this->config = array_merge($this->config, $config);
  99.             }
  100.             //记录该配置文件的路径,把它记录入相应的数组中去.
  101.             $this->is_loaded[] = $file_path;
  102.             //把$config清空,
  103.             unset($config);
  104.             //配置加载成功,注意这里的加载是指的是加载到了该类的属性数组里去.
  105.             $loaded = TRUE;
  106.             log_message('debug', 'Config file loaded: '.$file_path);
  107.         }

  108.         //判断是否加载成功,然后根据要求的格式给出加载的状态分析.
  109.         if ($loaded === FALSE)
  110.         {
  111.             if ($fail_gracefully === TRUE)
  112.             {
  113.                 return FALSE;
  114.             }
  115.             show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
  116.         }
  117.         
  118.         return TRUE;
  119.     }

  120.     
  121.     /**
  122.      取出一个配置项.
  123.      第一个参数 字符串 配置项的名称.即$config数组的下标.
  124.      第二个参数 字符串 索引名称.其实是$config这个二维数组里的第二个.
  125.      返回值为混合类型,如果有相应数据,则返回字符串,如果没有相应数据,则返回false.
  126.      */
  127.     function item($item, $index = '')
  128.     {
  129.         //如果没有索引,
  130.         if ($index == '')
  131.         {
  132.             if ( ! isset($this->config[$item]))
  133.             {
  134.                 return FALSE;
  135.             }

  136.             $pref = $this->config[$item];
  137.         }
  138.         else
  139.         {
  140.             if ( ! isset($this->config[$index]))
  141.             {
  142.                 return FALSE;
  143.             }

  144.             if ( ! isset($this->config[$index][$item]))
  145.             {
  146.                 return FALSE;
  147.             }

  148.             $pref = $this->config[$index][$item];
  149.         }

  150.         return $pref;
  151.     }

  152.     
  153.     /**
  154.      在配置项的后面加上斜线
  155.      有必要单独列出来吗????
  156.      */
  157.     function slash_item($item)
  158.     {
  159.         if ( ! isset($this->config[$item]))
  160.         {
  161.             return FALSE;
  162.         }

  163.         return rtrim($this->config[$item], '/').'/';
  164.     }

  165.     
  166.     /**
  167.      * 设置url.
  168.     第一个参数为uri字符串.
  169.     返回值为字符串类型.
  170.      */
  171.     function site_url($uri = '')
  172.     
  173.         //如果$uri为空,则直接在基路径和首页进行组合得到相应的url.
  174.         if ($uri == '')
  175.         {
  176.             return $this->slash_item('base_url').$this->item('index_page');
  177.         }

  178.         if ($this->item('enable_query_strings') == FALSE)
  179.         {
  180.             if (is_array($uri))
  181.             {
  182.                 $uri = implode('/', $uri);
  183.             }

  184.             $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
  185.             $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
  186.             return $this->slash_item('base_url').$index.trim($uri, '/').$suffix;
  187.         }
  188.         else
  189.         {
  190.             if (is_array($uri))
  191.             {
  192.                 $i = 0;
  193.                 $str = '';
  194.                 foreach ($uri as $key => $val)
  195.                 {
  196.                     $prefix = ($i == 0) ? '' : '&';
  197.                     $str .= $prefix.$key.'='.$val;
  198.                     $i++;
  199.                 }

  200.                 $uri = $str;
  201.             }

  202.             return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
  203.         }
  204.     }

  205.     
  206.     //得到系统的url.
  207.     function system_url()
  208.     {
  209.         $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
  210.         return $this->slash_item('base_url').end($x).'/';
  211.     }

  212.     
  213.     /**
  214.      设置一个配置项.
  215.     第一个参数为配置数组的下标,第二个为配置数组的值.
  216.     无返回值.
  217.      */
  218.     function set_item($item, $value)
  219.     {
  220.         $this->config[$item] = $value;
  221.     }

  222.     
  223.     /**
  224.      分配配置项,其实也就是多次设置单个的配置项而已.
  225.      在前台的控制器实例化了配置类之后,这个函数会用index.php里的数据覆盖它.
  226.      */
  227.     function _assign_to_config($items = array())
  228.     {
  229.         if (is_array($items))
  230.         {
  231.             foreach ($items as $key => $val)
  232.             {
  233.                 $this->set_item($key, $val);
  234.             }
  235.         }
  236.     }
  237. }

阅读(1010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~