ITPub博客

首页 > IT职业 > IT生活 > php自定义配置文件的读取

php自定义配置文件的读取

IT生活 作者:jockeyw 时间:2021-09-09 09:17:51 0 删除 编辑

自定义配置文件config.php

return array(

       'c_type' => 1,

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

)

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

 


   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;
       }
   }
   
   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;
   }




   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];
   }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2063/viewspace-2801860/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    73
  • 访问量
    554