sergeymakinen / yii2-config
适用于 Yii 2 的多才多艺的配置加载器
Requires
- sergeymakinen/yii2-php-file-cache: ^2.1
- yiisoft/yii2: ~2.0.13
Requires (Dev)
- sergeymakinen/yii2-tests: ^2.2.1
- symfony/yaml: ^2.8 || ^3.2 || ^4.0
Suggests
- symfony/yaml: Required to load YAML config files (^2.8 || ^3.2 || ^4.0).
This package is auto-updated.
Last update: 2021-02-15 21:12:36 UTC
README
适用于 Yii 2 的多才多艺的配置加载器。您可以在您喜欢的语言中定义单个配置定义文件,该文件将定义所有应用程序层的配置(console、backend、frontend 等)。
目录
安装
安装此扩展的最佳方式是通过 composer。
运行
composer require "sergeymakinen/yii2-config:^2.0"
或添加
"sergeymakinen/yii2-config": "^2.0"
到您的 composer.json 文件的 require 部分。
使用
首先,您需要定义您的配置:它可能是一个 PHP 数组,直接位于您计划包含它的文件中,但最好将其放置在一个可以以任何 支持格式 存储的文件中。就像在 示例 中所做的那样。
然后,您需要修改入口脚本来加载配置。它对于 Yii 2 基本项目模板 中的 console 层 yii 文件(将 层 视为 类型)可能是这样的
#!/usr/bin/env php <?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/yiisoft/yii2/Yii.php'; $config = sergeymakinen\yii\config\Config::fromFile(__DIR__ . '/config/config.php', ['tier' => 'console']); $application = new yii\console\Application($config); $exitCode = $application->run(); exit($exitCode);
对于 Yii 2 高级应用程序模板 中的 backend/web/index.php 层 backend/web/index.php 文件
<?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require __DIR__ . '/../../vendor/autoload.php'; require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php'; $config = sergeymakinen\yii\config\Config::fromFile(__DIR__ . '/../../common/config/config.php', ['tier' => 'backend']); (new yii\web\Application($config))->run();
示例配置
考虑这个 配置
<?php return [ 'configDir' => __DIR__, 'cacheDir' => dirname(__DIR__) . '/runtime/config', 'enableCaching' => YII_ENV_PROD, 'dirs' => [ '', '{env}', ], 'files' => [ [ 'class' => 'sergeymakinen\yii\config\PhpBootstrapLoader', 'path' => 'bootstrap.php', ], 'common.php', '{tier}.php', 'web:@components.urlManager.rules' => 'routes.php', '@components.log.targets' => 'logs.php', '@params' => 'params.php', ], ];
Config 将在 CONFIG_DIR 和 CONFIG_DIR/ENV 目录中寻找以下配置文件
bootstrap.php和bootstrap-local.php用于 PHP 代码common.php和common-local.phpTIER.php和TIER-local.php- 当层为
web时,routes.php和routes-local.php将合并为
[
'components' => [
'urlManager' => [
'rules' => [
// routes.php and routes-local.php contents
]
]
]
]
logs.php和logs-local.php将合并为
[
'components' => [
'log' => [
'targets' => [
// logs.php and logs-local.php contents
]
]
]
]
- 当层为
web时,params.php和params-local.php将合并为
[
'params' => [
// params.php and params-local.php contents
]
]
快捷方式
如 示例部分 所示,有不同的方式来指定配置文件配置。为了能写得更少,一些常见选项可以写成单个字符串,而不是数组。
'TIER:ENV@KEY' => 'PATH' 将解析为(你可以省略你不需要的部分)
[
'tier' => 'TIER',
'env' => 'ENV',
'key' => 'KEY',
'path' => 'PATH',
]
示例
| 快捷方式 | 结果 |
|---|---|
'bar' |
[
'path' => 'bar',
] |
'foo' => 'bar' |
[
'env' => 'foo',
'path' => 'bar',
] |
'foo@baz' => 'bar' |
[
'env' => 'foo',
'key' => 'baz',
'path' => 'bar',
] |
'loren:foo@baz' => 'bar' |
[
'tier' => 'loren',
'env' => 'foo',
'key' => 'baz',
'path' => 'bar',
] |
支持的配置格式
INI
扩展名: ini
加载类: sergeymakinen\yii\config\IniLoader
示例:
[config] class = yii\db\Connection dsn = "mysql:host=localhost;dbname=yii2basic" username = root password = "" charset = utf8
JSON
扩展名: json
加载类: sergeymakinen\yii\config\JsonLoader
示例:
{
"class": "yii\\db\\Connection",
"dsn": "mysql:host=localhost;dbname=yii2basic",
"username": "root",
"password": "",
"charset": "utf8"
}
PHP数组
扩展名: php
加载类: sergeymakinen\yii\config\PhpArrayLoader
示例:
<?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2basic', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ];
PHP引导
扩展名: php
加载类: sergeymakinen\yii\config\PhpBootstrapLoader
注意: 您需要显式设置类名以使用此加载器
[
'class' => 'sergeymakinen\config\PhpBootstrapLoader',
'path' => 'mybootstrapfile.php',
// ...
]
示例:
<?php Yii::$container->set(yii\grid\GridView::class, function ($container, $params, $config) { if (Yii::$app->controller instanceof yii\debug\controllers\DefaultController) { $defaults = []; } else { $defaults = [ 'layout' => '<div class="table-responsive">{items}</div><div class="grid-view-footer clearfix"><div class="pull-left">{summary}</div><div class="pull-right">{pager}</div></div>', 'tableOptions' => ['class' => 'table table-striped'], ]; } return new yii\grid\GridView(array_merge($defaults, $config)); });
YAML
扩展名: yml, yaml
加载类: sergeymakinen\yii\config\YamlLoader
注意: 您需要安装Symfony YAML库
运行
composer require "symfony/yaml:^2.8 || ^3.2"
或添加
"symfony/yaml": "^2.8 || ^3.2"
到您的 composer.json 文件的 require 部分。
示例:
class: 'yii\db\Connection' dsn: 'mysql:host=localhost;dbname=yii2basic' username: root password: '' charset: utf8
扩展
例如,让我们尝试编写一个简单的XML加载器
use yii\helpers\Json; class XmlLoader extends sergeymakinen\yii\config\ArrayLoader { /** * {@inheritdoc} */ public function loadFile($path) { $xml = simplexml_load_string(file_get_contents($path), 'SimpleXMLElement', LIBXML_NOCDATA); return Json::decode(Json::encode($xml)); } }
如果您希望自动将加载器用于XML文件,请将以下条目添加到Config的loaders属性数组中
'xml' => 'XmlLoader'