hello-motto / config-service-provider
用于配置 silex ~2.0 的服务提供者,支持 yaml、json 和 php 文件
This package is not auto-updated.
Last update: 2024-09-29 02:29:00 UTC
README
这是一个服务提供者,允许在 Silex 应用中使用 YAML、JSON 和 PHP 配置文件。
要求
- silex/silex: 2.X
建议
- symfony/yaml: 3.X
安装
使用 Composer 安装
$ composer require hello-motto/config-service-provider dev-master
或者将以下内容添加到 composer.json 中
"require": { "hello-motto/config-service-provider": "dev-master" }
使用方法
此提供者将解析您的 YAML、JSON 和 PHP 文件以设置配置变量。这些配置变量通过 $app['config']['myVariable'] 可用。您还可以定义一些特殊变量(参数变量),也通过 $app['parameters'] 可用。所有包含在各个配置文件中的变量都将递归合并到一个数组中。
##添加文件
您可以将一个或多个文件传递给 ServiceProvider 的 'config.files' 参数。无论它是 json、yaml 还是 php 文件都可以
$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [ 'config.files' => [ PATH_TO_FIRST_FILE, PATH_TO_SECOND_FILE, PATH_TO_THIRD_FILE, etc. ] ]);
其中 PATH_TO_CONFIG_FILE 是配置文件(YML、JSON 或 PHP)的位置,例如
__DIR__. "/config/config.yml", __DIR__. "/config/config.json", __DIR__. "/config/config.php",
文件示例
config/config.php
<?php return [ 'parameters' => [ 'dbhost' => 'localhost', 'dbuser' => 'user', 'dbpass' => 't@rt1fl3tt3', 'dbport' => '~' // tilde will be replaced by null value ], 'twig' => [ 'twig.form.templates' => [ 'formage.html.twig' ] ] ];
config/config.json
{
"parameters": {
"dbhost": "localhost",
"dbuser": "user",
"dbpass": "t@rt1fl3tt3",
"dbport": "~"
},
"twig": {
"twig.form.templates": [
"formage.html.twig"
]
}
}
config/config.yml
parameters: dbhost: "localhost" dbuser: "user" dbpass: "t@rt1fl3tt3" dbport: "~" #tilde will be replaced by null value twig: twig.form.templates: -"formage.html.twig"
##添加常量
您还可以将常量参数传递给 ServiceProvider 以替换配置文件中的内容。这允许在配置文件中使用 PHP 函数。常量名称必须以 % 字符开始和结束。您可以在 'config.constants' 数组中设置常量
$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [ 'config.files' => [], 'config.constants' => [ '%web.dir%' => __DIR__.'/../web', '%need%' => 'tartiflette', etc. ] ]);
config/config.json
{
"mySubDirectory": "%web.dir%/reblochon",
"ourMotto": "In %need% we trust"
}
config/config.yml
mySubDirectory: "%web.dir%/reblochon", ourMotto: "In %need% we trust"
app['config']['ourMotto'] 变量将包含 In tartiflette we trust。
##添加闭包
YAML 和 JSON 文件不允许使用动态代码。但是,一些提供者(如安全提供者)使用闭包参数时更强大。这就是为什么可以添加一些闭包,使用 'config.closures' 参数。要使用闭包参数,您的数组必须具有相同的树结构。
$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [ 'config.files' => [], 'config.constants' => [], 'config.closures' => [ 'security' => [ 'security.firewalls' => [ 'main' => [ 'users' => function() { return new UserProvider(); } ] ] ] ] ]);
##导入文件
config/config_dev.json
{
"imports": [
{
"resource": "config.json"
},
{
"resource": "parameters.json"
}
]
}
config/config_dev.yml
imports: - { resource: config.yml } - { resource: parameters.yml } # config.yml and parameters.yml are in the same directory as config_dev.yml
#特性
###config() 方法将此特性添加到您的应用程序中,您可以像对象一样使用配置变量。使用 $app['config'] 时,可以使用 $app->config()。您还可以通过将参数传递给方法来访问数组的第一个维度。$app['config']['twig'] 通过 $app->config('twig') 可用。
###parameters() 方法此快捷方式也适用于参数变量。$app['parameters']['dbhost'] 与 $app->parameters('dbhost') 相同
###loadFile() 方法此快捷方式使用 ConfigLoader 将 JSON、YAML 或 PHP 文件解析到数组中,替换已设置的常量。
###loadManyFiles() 方法此方法与 loadFile() 类似,只是它有一个文件数组作为参数。