php自定义配置文件的读取
admin
2023-07-04 23:04:10
0

自定义配置文件config.php

return array(

       'c_type' => 1,

       'spage'=>array(
          't'=>2,
       ),

)

访问形式 Util_Tool::config('config.spage.t') 获取t的值

 

/**
    *  配置文件数组值的获取
    *  $host = Util_Tool::config('database.default.connection.hostname')
    *  @param  $str string
    *  @param  array
    *
    */
   public static function config($group){
       static $config;

       if (strpos($group, '.') !== FALSE)
       {
           // Split the config group and path
           list ($group, $path) = explode('.', $group, 2);
       }
       //return $group;
       $config =  Zwp_Config::get_config($group);

       if ( ! isset($config))
       {
           // Load the config group into the cache
           $config = array();
       }

       if (isset($path))
       {
           return self::path($config, $path, NULL, '.');
       }
       else
       {
           return $config;
       }
   }
   /**
    * Gets a value from an array using a dot separated path.
    *
    *     // Get the value of $array['foo']['bar']
    *     $value = Util_Tool::path($array, 'foo.bar');
    *
    * Using a wildcard "*" will search intermediate arrays and return an array.
    *
    *     // Get the values of "color" in theme
    *     $colors = Util_Tool::path($array, 'theme.*.color');
    *
    *     // Using an array of keys
    *     $colors = Util_Tool::path($array, array('theme', '*', 'color'));
    *
    * @param   array   array to search
    * @param   mixed   key path string (delimiter separated) or array of keys
    * @param   mixed   default value if the path is not set
    * @param   string  key path delimiter
    * @return  mixed
    */
   public static function path($array, $path, $default = NULL, $delimiter = NULL)
   {
       if (!is_array($array))
       {
           // This is not an array!
           return $default;
       }

       if (is_array($path))
       {
           // The path has already been separated into keys
           $keys = $path;
       }
       else
       {
           if (array_key_exists($path, $array))
           {
               // No need to do extra processing
               return $array[$path];
           }

           if ($delimiter === NULL)
           {
               // Use the default delimiter
               $delimiter = '.';
           }

           // Remove starting delimiters and spaces
           $path = ltrim($path, "{$delimiter} ");

           // Remove ending delimiters, spaces, and wildcards
           $path = rtrim($path, "{$delimiter} *");

           // Split the keys by delimiter
           $keys = explode($delimiter, $path);
       }

       do
       {
           $key = array_shift($keys);

           if (ctype_digit($key))
           {
               // Make the key an integer
               $key = (int) $key;
           }

           if (isset($array[$key]))
           {
               if ($keys)
               {
                   if (is_array($array[$key]))
                   {
                       // Dig down into the next part of the path
                       $array = $array[$key];
                   }
                   else
                   {
                       // Unable to dig deeper
                       break;
                   }
               }
               else
               {
                   // Found the path requested
                   return $array[$key];
               }
           }
           elseif ($key === '*')
           {
               // Handle wildcards

               $values = array();
               foreach ($array as $arr)
               {
                   if ($value = self::path($arr, implode('.', $keys)))
                   {
                       $values[] = $value;
                   }
               }

               if ($values)
               {
                   // Found the values requested
                   return $values;
               }
               else
               {
                   // Unable to dig deeper
                   break;
               }
           }
           else
           {
               // Unable to dig deeper
               break;
           }
       }
       while ($keys);

       // Unable to find the value requested
       return $default;
   }



/**
    *  获取配置文件的值
    *  @copyright liwan 2013-10-06
    *  @param $key 标识config文件名 配置文件里统一使用return array的形式
    */
   public static function get_config($key)
   {
       static $new_config;
       if (isset($new_config[$key])) return $new_config[$key];
       if (is_file(CONFIG_DIR . '/' . $key . '.php')) {
           $new_config[$key] = require CONFIG_DIR . '/' . $key . '.php';
       } else return false;
       return $new_config[$key];
   }

相关内容

热门资讯

我国科学家为细胞信号“导航”开... 新华社济南5月31日电(记者张力元)人体细胞犹如一座精密的通信城市,每天都有大量“指令”穿梭传递,调...
极端大风突袭哈尔滨!过山车停摆... 极目新闻记者 詹钘5月31日,受强对流天气影响,哈尔滨国际会展中心体育场相关设施受到损坏,原计划当晚...
三原电缆取得电缆接头连接用防护... 国家知识产权局信息显示,上海三原电缆附件有限公司取得一项名为“一种电缆接头连接用防护结构”的专利,授...
原创 识... 还是那句话,机圈苦大屏久已…… 虽然大屏有大屏的美,但是小屏也有小屏的俏。在大屏旗舰占据主流的手机市...
玄戒技术取得分频电路专利,实现... 国家知识产权局信息显示,北京玄戒技术有限公司取得一项名为“分频电路、分频器、射频芯片和电子设备”的专...
为什么今年香会基调明显变了 5月29日—31日在新加坡举行的第23届香格里拉对话会(简称“香会”),见证着元首引领下大国关系继续...
成本几毛钱、假驱蚊液香精兑水,... 入夏升温,蚊虫进入活跃期,驱蚊防护成为民生刚需,《财经调查》持续接到消费者投诉,他们买到的多款网红驱...
越来越多80后90后,正在丧失... 六一儿童节到来之际,朋友圈里开始出现一种熟悉的热闹。有人晒出零食礼包,有人半开玩笑地向伴侣讨礼物,还...
洋保电子取得用于低温环境的电气... 国家知识产权局信息显示,洋保电子(太仓)有限公司取得一项名为“一种用于低温环境的电气柜”的专利,授权...
中日韩飞手争霸宁波!2026无... 潮新闻客户端 记者 陈冲 通讯员 朱凝 5月31日,2026小遛·无人机竞速世界杯(中国·宁波鄞州站...