[李景山php]每天TP5-20161210|Config.php

來源:互聯網
上載者:User

標籤:thinkphp5

<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------

namespace think;

class Config
{
   // 配置參數
   private static $config = [];// 這裡的 static 設定的 比較出神入化
   // 參數範圍
   private static $range = ‘_sys_‘;// 同上

   // 設定配置參數的範圍
   public static function range($range)
   {
       self::$range = $range;// 初始化, 範圍
       if (!isset(self::$config[$range])) {// 如果不存在,就 初始化為空白
           self::$config[$range] = [];// 預設設定 初始化 為空白
       }
   }

   /**
    * 解析設定檔或內容
    * @param string    $config 設定檔路徑或內容
    * @param string    $type 配置解析類型
    * @param string    $name 配置名(如設定即表示二級配置)
    * @param string    $range  範圍
    * @return mixed
    */
   public static function parse($config, $type = ‘‘, $name = ‘‘, $range = ‘‘)
   {// 解析 設定檔
       $range = $range ?: self::$range;// 預設 檔案 配置 選項
       if (empty($type)) {// 如果為空白
           $type = pathinfo($config, PATHINFO_EXTENSION);// 解析 配置 檔案
       }
       $class = false !== strpos($type, ‘\\‘) ? $type : ‘\\think\\config\\driver\\‘ . ucwords($type);
       // 擷取 解析 類檔案 分別對應為 ini \ json \ xml 類檔案 方式 解析 檔案
       return self::set((new $class())->parse($config), $name, $range);// 解析設定檔
   }

   /**
    * 載入設定檔(PHP格式)
    * @param string    $file 設定檔名
    * @param string    $name 配置名(如設定即表示二級配置)
    * @param string    $range  範圍
    * @return mixed
    */
   public static function load($file, $name = ‘‘, $range = ‘‘)
   {// 載入 配置 檔案
       $range = $range ?: self::$range;// 設定檔類型 初始化
       if (!isset(self::$config[$range])) {// 沒有設定過,直接初始化
           self::$config[$range] = [];
       }
       if (is_file($file)) {// 如果是檔案,
           $type = pathinfo($file, PATHINFO_EXTENSION);// 檔案類型
           if (‘php‘ != $type) {
               return self::parse($file, $type, $name, $range);// 解析 預設的 檔案 ini xml json
           } else {
               return self::set(include $file, $name, $range);// 擷取檔案
           }
       } else {
           return self::$config[$range];// 直接返回資料
       }
   }

   /**
    * 檢測配置是否存在
    * @param string    $name 配置參數名(支援二級配置 .號分割)
    * @param string    $range  範圍
    * @return bool
    */
   public static function has($name, $range = ‘‘)
   {
       $range = $range ?: self::$range;// 檔案 類型

       if (!strpos($name, ‘.‘)) {
           return isset(self::$config[$range][strtolower($name)]);
       } else {
           // 二維數組設定和擷取支援
           $name = explode(‘.‘, $name);
           return isset(self::$config[$range][strtolower($name[0])][$name[1]]);
       }
   }// 檢測 是否 有這個 配置 選項

   /**
    * 擷取配置參數 為空白則擷取所有配置
    * @param string    $name 配置參數名(支援二級配置 .號分割)
    * @param string    $range  範圍
    * @return mixed
    */
   public static function get($name = null, $range = ‘‘)
   {// 擷取 配置 參數 為空白 則擷取所有的配置
       $range = $range ?: self::$range;// 配置 類型
       // 無參數時擷取所有
       if (empty($name) && isset(self::$config[$range])) {
           return self::$config[$range];
       }// 如果為空白,直接返回

       if (!strpos($name, ‘.‘)) {// 如果 沒有 這個
           $name = strtolower($name);// 返回資料
           return isset(self::$config[$range][$name]) ? self::$config[$range][$name] : null;
       } else {
           // 二維數組設定和擷取支援
           $name = explode(‘.‘, $name);
           $name[0] = strtolower($name[0]);
           return isset(self::$config[$range][$name[0]][$name[1]]) ? self::$config[$range][$name[0]][$name[1]] : null;
       }// 返回二維 數組
   }

   /**
    * 設定配置參數 name為數組則為大量設定
    * @param string|array  $name 配置參數名(支援二級配置 .號分割)
    * @param mixed         $value 配置值
    * @param string        $range  範圍
    * @return mixed
    */
   public static function set($name, $value = null, $range = ‘‘)
   {
       $range = $range ?: self::$range;// 進行 設定 範圍圈定,預設的是 _sys_ 系統設定項目
       if (!isset(self::$config[$range])) {// 如果設定了 self::$config 沒有設定這個,
           self::$config[$range] = [];// 預設的  初始化 為空白
       }
       if (is_string($name)) {// 如果是字串 預設就是key
           if (!strpos($name, ‘.‘)) {// 如果沒有.
               self::$config[$range][strtolower($name)] = $value;// self::
           } else {
               // 二維數組設定和擷取支援
               // 最大支援到二維數組
               $name                                                 = explode(‘.‘, $name);
               self::$config[$range][strtolower($name[0])][$name[1]] = $value;
           }
           return;
       } elseif (is_array($name)) {
           // 大量設定
           if (!empty($value)) {// 如果 value 有數值 此處的 value 變成了 key
               self::$config[$range][$value] = isset(self::$config[$range][$value]) ?
               array_merge(self::$config[$range][$value], $name) :
               self::$config[$range][$value] = $name;
               return self::$config[$range][$value];// 並且 返回 他,這個應該是個非常不科學的決定
           } else {
               return self::$config[$range] = array_merge(self::$config[$range], array_change_key_case($name));
               // 返回合并的資料 如果不調用,沒有任何意義
           }
       } else {
           // 為空白直接返回 已有配置
           return self::$config[$range];// 很喜歡 這個風格 跟調調,哈哈
       }
   }

   /**
    * 重設配置參數
    */
   public static function reset($range = ‘‘)
   {// 這個是 個重量級的 函數啊,
       $range = $range ?: self::$range;// 查看是否存在範圍
       if (true === $range) {// 如果 傳入了 真 清空一切
           self::$config = [];
       } else {
           self::$config[$range] = [];// 否則  清空 指定對象
       }
   }
}


本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1859512

[李景山php]每天TP5-20161210|Config.php

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.