Vars是一个易于使用、轻量级且易于扩展的配置加载器,具有适用于 ENV、INI、JSON、PHP、Toml、XML 和 YAML 文件类型的内置加载器。它还内置了对 Silex 的支持,以及即将推出的更多框架(Symfony、Laravel 等)。
有时您被迫对配置文件使用不同的格式,而Vars目标之一是通过支持最常见的配置格式来使这对您来说更简单,这样您就不必切换库来处理不同的格式。
另一个目标是支持不同的框架,因此在处理不同的框架时您不必切换库。目前仅使用服务提供商支持 Silex,很快就会支持 Laravel 和 Symfony。
通过简单的 API 和直观的加载选项, Vars尝试使配置加载和提供尽可能简单。
Vars需要 PHP 版本5.3+ 。
如果您想使用 YAML,则需要symfony/yaml库,同样,您需要yosymfony/toml来使用 Toml 文件,需要m1/env来使用 Env 文件。
通过作曲家
$ composer require m1/ Vars // load single file
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
// load from dir
$ Vars = new Vars ( __DIR__ . ' /config ' );
// load from array
$ Vars = new Vars ( array (
__DIR__ . ' /config/config.yml ' ,
__DIR__ . ' /config/sub ' ,
));这可以通过多种方式完成,您可以将$ Vars变量视为普通数组,也可以以面向对象的方式使用它
// All return the same thing
$ Vars -> get ( ' db.password ' )
$ Vars [ ' db.password ' ];
$ Vars [ ' db ' ][ ' password ' ]也可以用同样的方式设置值
// All do the same thing
$ Vars -> set ( ' db.password ' , ' test ' )
$ Vars [ ' db.password ' ] = ' test ' ;
$ Vars [ ' db ' ][ ' password ' ] = ' test ' ;您还可以从getenv()获取变量
// All do the same thing
$ Vars -> toEnv ();
getenv ( ' db.password ' );有关这方面的更多信息,请检查环境变量部分
您可以轻松地将配置相对和绝对导入到其他配置中,这些配置因配置文件类型而异,因此请检查 /tests/mocks/ 文件夹中的示例
# example_1.yml
test_key_1 : test_value_1
imports : example_2.yml
# example_2.yml
test_key_2 : test_value_2将返回:
[
" test_key_1 " => " test_value_1 " ,
" test_key_2 " => " test_value_2 "
]默认情况下,导入是相对于密钥导入的,例如:
test_key_1 :
imports : example_2.yml将返回:
[
" test_key_1 " => [
" test_key_2 " => " test_value_2 "
]
]但是,您可以通过多种方式更改此设置:
# example 1
test_key_1 :
imports :
- {resource: example.yml, relative: false}
# example 2
test_key_2 :
imports :
resource : example.yml
relative : false如果导入各种文件并且您想要设置所有文件的相对性,您可以执行以下操作:
test_key_1 :
imports :
relative : false
resource :
- example_2.yml
- example_3.yml上述所有内容都会导致example_2.yml和example_3.yml变量对配置文件成为绝对变量:
[
" test_key_1 " => []
" test_key_2 " => " test_value_2 " // from example_2.yml
"test_key_3" => "test_value_3" // from example_3.yml
]您还可以使用上述所有语法导入目录:
test_key_1 :
imports : sub/默认情况下,导入目录不是递归的,并且不会搜索文件夹内的文件夹,您可以通过添加递归切换来更改此设置:
test_key_1 :
imports :
resource : sub/
recursive : true或者通过添加递归标志:
test_key_1 :
imports :
resource : sub/*与加载文件一样,您可以通过一个递归切换来批量导入目录:
test_key_1 :
imports :
recursive : false
resource :
- sub/
- sub1/目录的导入依赖于加载器和加载器支持的扩展。有关更多详细信息,请参阅加载器部分。
导入时您可以使用各种标志。
if else 标志?:使得如果第一个文件存在,则使用该文件 - 否则使用其他定义的文件,例如:
imports : " example_1.yml ?: example_2.yml "注意:您需要将字符串括在引号中才能使 if else 标志起作用
抑制异常标志@ ——抑制文件未找到异常。例如:
imports : @file_does_not_exist.yml递归标志使得在目录中的目录中搜索文件。例如:
imports :
resource : sub/*您还可以组合上述标志,因此如果 else 文件选项不存在,则不会抛出异常,例如:
imports : " example_1.yml ?: @example_2.yml "您可以获得单独的文件或资源:
// All return the same thing
$ Vars -> getResource ( ' example_2.yml ' )-> get ( ' test_key_2 ' );
$ Vars -> getResource ( ' example_2.yml ' )[ ' test_key_2 ' ];Vars有多种选项
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
// this will affect how you getResource() and will default to the path
// of the first resource you initiate
' path ' => __DIR__ . ' /config ' ,
// to cache or not -- defaults to true
' cache ' => true ,
// where the cache is stored -- If not set will default to the base path
' cache_path ' => __DIR__ . ' /config/ ' ,
// How long the cache lasts for -- Defaults to 300 seconds (5 minutes)
' cache_expire ' => 300 ,
// Replacement variables -- see variables section for more detail
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
// Merge globals -- see globals section for more detail
' merge_globals ' => true ,
// The file loaders to load the configs -- see loader section for more detail
' loaders ' => [
' default '
]
]);path是$ Vars ->getResource($filename)中$filename计算方式。例如:
如果您将path设置为__DIR__.'/config'并导入了__DIR__.'/app/test_1.yml' :
# example_1.yml
imports : example_2.yml那么example_1.yml和example_2.yml $filename将分别是../app/test_1.yml和../app/test_1.yml 。
如果没有设置path ,则第一个文件资源路径将用作path ,例如:
// example 1
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
// example 2
$ Vars = new Vars ([
__DIR__ . ' /config/config.yml ' ,
__DIR__ . ' /sub/config.yml ' ,
]);都将使用__DIR__.'/config'作为path
您可以在Vars中使用 3 种类型的变量: Replacements 、 In-file和Environment ,语法为:
| 变量类型 | 句法 |
|---|---|
| 替换品 | %VARIABLE% |
| 文件内 | %$VARIABLE% |
| 环境 | %^VARIABLE% |
为了更好的可读性,您还可以在变量名称和前缀/后缀之间放置空格,如下所示:
replacement_variable : % VARIABLE %
infile_variable : %$ VARIABLE %
env_variable : %^ VARIABLE % 替换变量是从外部加载的Vars ,因此它通常用于PHP函数/逻辑,例如__dir__ :
test_key_1 : %foo%
test_key_2 : /bar/%foobar%/bar $ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
]);输出:
[
" test_key_1 " => " bar " ,
" test_key_2 " => " /bar/barfoo/foobar/ "
]您的替换内容必须以%为前缀和后缀
您还可以从文件加载变量:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' replacements ' => __DIR__ . ' /config/variables.yml '
]);您还可以使用文件中已定义的键中的变量,例如:
test_key_1 : hello
test_key_2 : /bar/%$test_key_1%/bar输出:
[
" test_key_1 " => " bar " ,
" test_key_2 " => " /bar/hello/foobar/ "
]您的替换内容必须以%$为前缀并以%为后缀。
对于in-file和replacements ,您可以使用点符号语法来获取数组,例如:
test_key_1 :
test_key_2 : hello
test_key_3 : /bar/%$test_key_1.test_key_2%/bar输出:
[
" test_key_1 " => array (
" test_key_2 " => " hello "
),
" test_key_2 " => " /bar/hello/foobar/ "
]您还可以使用环境变量进行替换:
test_key_1 : %^DATABASE_USERNAME%
test_key_2 : %^DATABASE_PASSWORD% # nginx config example
location @site {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root /index.php;
# env variables
fastcgi_param DATABASE_USERNAME test_username;
fastcgi_param DATABASE_PASSWORD test_password;
}输出:
[
" test_key_1 " => " test_username " ,
" test_key_2 " => " test_password "
]您的环境变量必须以%^为前缀并以%为后缀
您还可以这样做,以便您的配置数组可用于getenv() :
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' );
$ Vars -> toEnv ();注意:为此,您的配置将被展平为点表示法,例如:
test_key_1 :
test_key_2 : value将由以下人员访问:
getenv ( ' test_key_1.test_key_2 ' ); // value Vars中的Globals指的是这样定义的变量:
_globals :
test_key_1 : test_value_1基本上它们只是封装在一个_globals数组中——使用它们是为了让你可以从Vars getGlobals()访问它们
默认操作是将它们合并到其他文件内容中,以便:
_globals :
test_key_1 : test_value_1
test_key_2 : test_value_2变成:
[
' test_key_1 ' => ' test_value_1 ' ,
' test_key_2 ' => ' test_value_2 ' ,
]但您可以通过选项将merge_globals更改为false来覆盖此设置。
如果这没有意义,那么您可能根本不需要使用全局变量,但它们对于使用封装了$app下所有内容的框架非常有用,并且您希望能够访问一些键 => 值,例如所以: $app['test_key_1'] 。有关更多示例,请参阅 Silex 提供程序部分。
Vars会自动缓存资源 5 分钟,您可以通过将cache选项设置为false来关闭此功能。
如果未设置,则将cache_path设置为path设置的内容。 cache_path必须是可写的。
要使缓存无效,只需删除cache_path中名为Vars文件夹即可,例如: rm -rf /var/www/application/app/cache/ Vars
由于 opcache 的额外加速,缓存文件是一个 .php 文件。
如果您使用 Silex 提供程序,则在调试模式下将不会使用和设置缓存。
加载器使Vars能够读取不同的文件类型(默认为 Ini、Json、Php、Toml、Xml 和 Yaml)。
您可以通过以下选项启用和禁用加载程序:
默认加载所有默认加载器:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => ' default '
]);
// You can load individual loaders:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' ini ' ,
' json '
[
]);
//You can also create and load custom loaders:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' FooBarCustomFooBarLoader ' ,
' ini ' ,
' json '
]
]);要创建您自己的自定义加载程序,您必须扩展M1 Vars LoaderAbstractLoader ,在public static $supported数组中具有受支持的扩展,并具有加载文件内容的public function load() 。
这是加载 .txt 文件的原始示例:
namespace M1 Foo Bar Loader ;
use M1 Vars Loader AbstractLoader ;
class TextLoader extends AbstractLoader
{
public static $ supported = array ( ' txt ' );
public function load ()
{
$ content = [];
foreach ( file ( $ this -> entity ) as $ line ) {
list ( $ key , $ value ) = explode ( ' : ' , $ line , 2 );
$ content [ trim ( $ key )] = trim ( $ value );
}
$ this -> content = $ content ;
return $ this ;
}
}然后要使用这个加载器,您只需使用:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
' loaders ' => [
' M1FooBarLoaderTextLoader ' ,
]
]);注意:不要真正使用这个加载器,它纯粹是为了演示目的
将此库与 Silex 一起使用非常简单,只需在注册其他服务提供商时注册它即可:
$ app -> register ( new M1 Vars Provider Silex Vars ServiceProvider ( ' example.yml ' ), [
' Vars .path ' => __DIR__ . ' /../../app/config/test/ ' ,
' Vars .options ' => [
' cache ' => true ,
' cache_path ' => __DIR__ . ' /../../app/config/cache/ ' ,
' cache_expire ' => 500 ,
' replacements ' => [
' test ' => ' test_replacement '
],
' loaders ' => [
' yml ' ,
' json '
],
' merge_globals ' => true ,
' replacements ' => __DIR__ . ' /../../app/config/replacements.json ' ,
]]);然后你可以从$app[' Vars ']访问你的配置
注意:如果$app['debug'] = true那么缓存将不会被使用。
您还可以使用点符号从 $app 访问配置值,例如:
test_key_1 :
test_key_2 : value
test_key_3 : value您可以使用点符号得到上面的内容,如下所示:
$ app [ ' Vars ' ][ ' test_key_1.test_key_2 ' ]; // value
$ app [ ' Vars ' ][ ' test_key_3 ' ]; // value您还可以将全局变量合并到$app中,如下所示:
# example.yml
_globals :
monolog.logfile : log.log
test_key_1 : test_value_2 $ app -> register ( new M1 Vars Provider Silex Vars ServiceProvider ( ' example.yml ' ));
// register monolog here and other service providers
$ app [ ' Vars .merge ' ]();请注意$app[' Vars .merge']() ——这会覆盖服务提供者的默认设置,因此在此示例中monolog将使用Vars配置中定义的日志文件。
在调用您在配置中为其提供配置值的服务提供商后,您必须调用Vars .merge 。
您还可以通过$app[' Vars .test_key_1']访问test_key_1 ,如果需要,您也可以类似地访问全局变量$app['monolog.logfile'] 。
Vars ($resource, $options = array())创建新Vars配置的构造函数:
$ Vars = new Vars ( __DIR__ . ' /config/config.yml ' , [
// this will affect how you getResource() and will default to the path
// of the first resource you initiate
' path ' => __DIR__ . ' /config ' ,
// to cache or not -- defaults to true
' cache ' => true ,
// where the cache is stored -- If not set will default to the base path
' cache_path ' => __DIR__ . ' /config/ ' ,
// How long the cache lasts for -- Defaults to 300 seconds (5 minutes)
' cache_expire ' => 300 ,
// Replacement variables -- see variables section for more detail
' replacements ' => [
' foo ' => ' bar ' ,
' foobar ' => ' barfoo '
],
// The file loaders to load the configs -- see loader section for more detail
' loaders ' => [
' default '
]
]);getContent()返回所有配置的解析内容。
getResource($resource)获取指定资源,返回文件资源,如果资源不存在则返回 false。
$resource名称基于基本路径和文件名中定义的路径。
# example.yml
imports : example2.yml
test_1 : value
# example2.yml
test_2 : value $ Vars = new Vars ( ' example.yml ' );
$ Vars -> getResource ( ' example2.yml ' ); // FileResource
$ Vars -> getResource ( ' example2.yml ' )-> getContent ();
# output:
# [
# "test_2" => "value"
# ] getResources()返回所有导入的资源,它们将是FileResource对象。
toEnv()使得配置可以通过getenv()获得:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> toEnv ();
getenv ( ' test_1 ' ); // value toDots()使配置扁平化为点表示法数组
test_value_1 :
test_value_2 : value
test_value_3 : value $ Vars = new Vars ( ' example.yml ' );
$ Vars -> toDots ();
# output:
# [
# "test_value_1.test_value_2" => "value",
# "test_value_1.test_value_3" => "value
# ] getGlobals()获取_globals中定义的值
set($key, $value)设置配置键:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> set ( ' test_key_1 ' , ' value_2 ' );get($key)获取配置密钥:
$ Vars = new Vars ( ' example.yml ' );
$ Vars -> get ( ' test_key_1 ' ); // valuegetRawContent()从文件中获取原始的、未解析的内容
# example.yml
test_value_1 :
imports : example2.yml
test_value_2 : %root%/foo/%dir% $ Vars = new Vars ( ' example.yml ' );
$ Vars -> getResource ( ' example.yml ' )-> getRawContent ();
# output:
# [
# test_value_1:
# imports: example2.yml
# test_value_2: %root%/foo/%dir%
# ] getContent()请参阅 getContent()
get($key)参见 get()
请参阅变更日志以了解最近更改的更多信息。
$ composer test 详细信息请参阅贡献。
如果您发现任何与安全相关的问题,请发送电子邮件至 [email protected],而不是使用问题跟踪器。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。