2amigos/yii2-config-kit 问题修复 & 功能扩展

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

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

2amigos/yii2-config-kit

最新稳定版本:2.0.1

Composer 安装命令:

composer require 2amigos/yii2-config-kit

包简介

Configuration kit for Yii applications

README 文档

README

Packagist Version Latest Stable Version Total Downloads

In order to provide ourselves a higher flexibility with the way we build templates for our Yii based projects, we built this kit. It is, in a way, the newest generation of the widely used YiiBootstrap, but it is so much better structured and less complex than the previously mentioned was.

As we all know, Yii has a very cumbersome array configuration for its bootstrap process. The main.php script normally contains a lot of information in it regarding application settings, components, modules and parameters, and many times we find ourselves dealing with a huge file with settings that, even though they are ordered by keys, fails to be clear due to the amount of lines within it.

ConfigKit tries to solve that issue, allowing us to create project templates with a different bootstrap and configuration building process. It proposes the following configuration folder structure:

config 
├── codeception [ application name that contains its configuration ]
|   ├── app.php
├── console [ application name ]
|   | 
|   ├── components 
|   ├── params 
|   └──app.php [ app.php contains simple attribute configuration ]
|
└── web [ application name ]
    | 
    ├── components [ main section on configuration ]
    |   ├── cache.php [ the name of the file, is the name of the component ]
    |   ├── db.php  [ the contents of the file, are the settings of the component ]
    |   └── log.php
    ├── params  [ main section on app configuration ]
    |   └── mail.php 
    └── app.php
    

Please, keep in mind that the above configuration folder structure is really up to you. ConfigKit requires a ConfigurationBuilder that you are responsible to develop and it can have the above recommendation or another one. For an example of a ConfigurationBuilder please visit https://github.com/sidekit/yii2-app-template/blob/master/src/App/Configuration/ConfigurationBuilder.php

Config File Examples

Example of app.php

<?php

use Da\Config\Configuration;

return [

    /*
     * --------------------------------------------------------------------------
     * Application
     * --------------------------------------------------------------------------
     *
     * Base class for all application classes. Here we configure the attributes
     * that do not hold any object configuration such as "components" or
     * "modules". The configuration of those properties are within submodules of
     * the same name.
     */

    'id' => 'application-id',

    'basePath' => Configuration::app()->getBasePath(),

    'vendorPath' => Configuration::app()->getVendorPath(),

    'runtimePath' => Configuration::app()->getRuntimePath(),

    'language' => Configuration::env()->get('APP_LANGUAGE'),

    'bootstrap' => ['log'],
];

Example of a component file: db.php

<?php

use Da\Config\Configuration;

return [

    /*
     * --------------------------------------------------------------------------
     * Connection
     * --------------------------------------------------------------------------
     *
     * Represents a connection to a database via PDO.
     */

    'class' => 'yii\db\Connection',

    'dsn' => Configuration::env()->get('DATABASE_DSN'),

    'username' => Configuration::env()->get('DATABASE_USER'),

    'password' => Configuration::env()->get('DATABASE_PASSWORD'),

    'charset' => Configuration::env()->get('DATABASE_CHARSET'),

    'tablePrefix' => Configuration::env()->get('DATABASE_TABLE_PREFIX'),
];

Environment Settings Overrides

We know how important settings for different environments are , such as: test, local, stage and prod environments. The proposed solution to dealing with them is as simple as adding a config folder with the name of the environment needed that has the same structure within, as the previously mentioned the folder. We never found ourselves having to clone the entire structure, so you can just simply place in it the files that contains the settings you wish to override.

env
|
└── local [ the environment name ]
    └── web [ the application which settings we need to override ]
        | 
        ├── components
        |   └── db.php  [ the component (filename) and its settings that we wish to override ]
        └── params
            └── mail.php [ the parameters to override ]
        

Bootstrapping

We believe that application bootstrapping should also be as structured as its configuration. That way, all processes are much clear and easier to manage and scale. In the Yii 2 proposed project's template https://github.com/2amigos/yii2-app-template you can see a working sample of ConfigKit library + a proposed bootstrapping process. The following is the startup process of a web application:

<?php

/*
 * --------------------------------------------------------------------------
 * Register auto loaders
 * --------------------------------------------------------------------------
 *
 * Add registered class loaders required for our application.
 *
 */

require __DIR__ . '/../bootstrap/autoload.php';

/*
 * --------------------------------------------------------------------------
 * Initialize SideKit library
 * --------------------------------------------------------------------------
 *
 * This step is required *prior* adding the application script.
 *
 */

require __DIR__ . '/../bootstrap/sidekit.php';

/*
 * --------------------------------------------------------------------------
 * Initialize custom aliases
 * --------------------------------------------------------------------------
 *
 * Add custom aliases to the application. Added after sidekit to take
 * advantage of its loaded configuration values
 */

require __DIR__ . '/../bootstrap/aliases.php';

/*
 * --------------------------------------------------------------------------
 * Configure and Go!
 * --------------------------------------------------------------------------
 *
 * Bootstrap the configuration processes and get and Application ready to use.
 * Applying configuration details in a different file allow us to free up
 * unnecessary code on the entry script.
 */

$app = require __DIR__ . '/../bootstrap/web.php';

$app->run();

Clean code

We have added some development tools for you to contribute to the library with clean code:

  • PHP mess detector: Takes a given PHP source code base and look for several potential problems within that source.
  • PHP code sniffer: Tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
  • PHP code fixer: Analyzes some PHP source code and tries to fix coding standards issues.

And you should use them in that order.

Using php mess detector

Sample with all options available:

 ./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode

Using code sniffer

 ./vendor/bin/phpcs -s --report=source --standard=PSR2 ./src

Using code fixer

We have added a PHP code fixer to standardize our code. It includes Symfony, PSR2 and some contributors rules.

./vendor/bin/php-cs-fixer fix ./src --config .php_cs.dist

Testing

  • TODO

2amigOS!

2amigos/yii2-config-kit 适用场景与选型建议

2amigos/yii2-config-kit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.41k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2017 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 2amigos/yii2-config-kit 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 14.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 14
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 20
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-06-14