codemix/yii2-configloader 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codemix/yii2-configloader

Composer 安装命令:

composer require codemix/yii2-configloader

包简介

Build configuration arrays from config files and env vars.

README 文档

README

Build Status Latest Stable Version Latest Unstable Version Total Downloads License

Build configuration arrays from config files and environment variables.

Features

You can use this extension to solve some or all of the following tasks:

  • Build Yii2 configuration arrays for web and console application
  • Initialize Yii environment (YII_DEBUG, YII_ENV) from environment variables
  • Load environment variables from a .env file
  • Get config options from environment variables
  • Load local configuration overrides
  • Streamline config initialization and Yii 2 bootstrapping

Installation

Install the package with composer:

composer require codemix/yii2-configloader

Description

We mainly use this extension to configure our dockerized yii2 applications. It's good practice to build your docker applications in such a way, that the runtime configuration in productive mode happens solely via environment variables.

But during local development we can loosen these strict requirement a little as we sometimes have to add debug options or the like that should not be part of the main configuration. Here the extension helps to override settings with local configuration files that live outside of version control.

You have several options how to use this extension:

  1. Use only the Yii environment initialization
  2. Use only the configuration loader
  3. Use both

We first show how to use the first two options "standalone" and then a third, combined way that includes all features.

1. Initializing Yii environment

This will set the YII_DEBUG and YII_ENV variables according to the respective environment variables if those are set. It can also load them from a .env file.

In debug mode error_reporting() will also be set to E_ALL.

<?php
use codemix\yii2confload\Config;

Config::initEnv('/path/to/app');
$setting = Config::env('MY_SETTING', 'default');

If you leave away the application path, no .env file will be loaded.

2. Loading configuration

If want to load your configuration with this extenstion, the following naming scheme must be followed:

  • config/web.php - Web configuration
  • config/console.php - Console configuration
  • config/local.php - Local overrides for the web configuration (optional)
  • config/local-console.php - Local overrides for the console configuration (optional)

If you only want to load configuration from files but whithout initializing the Yii environments as shown above, you'd create a Config instance and pass the application base directory and, as second argument, false to the constructor:

<?php
use codemix\yii2confload\Config;
$config = new Config('/path/to/app', false);
// Reads configuration from config/web.php
$webConfig = $config->web();

2.1 Local configuration

By default local configuration files local.php and local-console.php are not loaded. To activate this feature you can either set the ENABLE_LOCALCONF environment variable (either in your server environment or in .env):

ENABLE_LOCALCONF=1

Now the methods will return the corresponding merged results:

  • web(): config/web.php + config/local.php
  • console(): config/console.php + config/local-console.php

Alternatively you can explicitely ask for local configuration:

<?php
use codemix\yii2confload\Config;
$config = new Config('/path/to/app', false);
// Merges configuration from config/web.php and config/local.php if present
$webConfig = $config->web([], true);
// Merges configuration from config/console.php and config/local-console.php if present
$consoleConfig = $config->console([], true);

2.2 Merging custom configuration

You can also inject some other configuration when you fetch the web or console config:

<?php
use codemix\yii2confload\Config;
$config = new Config('/path/to/app', false);
$webConfig = $config->web(['id' => 'test'], true);

3. Initialize Yii environment and load configuration

Let's finally show a full example that demonstrates how to use all the mentioned features in one go. A typical setup will use the following files:

.env

Here we define the Yii environment and DB credentials. You'd add more config options in the same manner:

YII_DEBUG=1
YII_ENV=dev

DB_DSN=mysql:host=db.example.com;dbname=web
DB_USER=user
DB_PASSWORD='**secret**'

config/web.php

This file is later included in the scope of codemix\yii2confload\Config, so you can easily access instance and class methods:

<?php
/* @var codemix\yii2confload\Config $this */
return [
    'components' => [
        'db' => [
            'dsn' => self::env('DB_DSN', 'mysql:host=db;dbname=web'),
            'username' => self::env('DB_USER', 'web'),
            'password' => self::env('DB_PASSWORD', 'web'),
        ],

config/console.php

Having access to the config instance allows for example to reuse parts of your web configuration in your console config.

<?php
/* @var codemix\yii2confload\Config $this */

$web = $this->web();
return [
    // ...
    'components' => [
        'db' => $web['components']['db'],

web/index.php

We've streamlined the process of setting up a Config object and loading the Yii 2 bootstrap file into a single method Config::boostrap() which only receives the application directory as argument.

<?php
use codemix\yii2confload\Config;

require(__DIR__ . '/../vendor/autoload.php');
$config = Config::bootstrap(__DIR__ . '/..');
Yii::createObject('yii\web\Application', [$config->web()])->run();

This makes sure that things are loaded in the right order. If you prefer a more verbose version, the code above is equivalent to:

<?php
use codemix\yii2confload\Config;

require(__DIR__ . '/../vendor/autoload.php');
$config = new Config(__DIR__ . '/..');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

Yii::createObject('yii\web\Application', [$config->web()])->run();

yii

The same approach is used for the console application:

<?php
use codemix\yii2confload\Config;

require(__DIR__ . '/vendor/autoload.php');
$config = Config::bootstrap(__DIR__);
$application = Yii::createObject('yii\console\Application', [$config->console()]);
exit($application->run());

codemix/yii2-configloader 适用场景与选型建议

codemix/yii2-configloader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 127.63k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2017 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「config」 「environment」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codemix/yii2-configloader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codemix/yii2-configloader 我们能提供哪些服务?
定制开发 / 二次开发

基于 codemix/yii2-configloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 127.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 60
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 60
  • Watchers: 12
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-28