2amigos / yii2-config-kit
Yii应用程序配置套件
Requires
- php: >=7.0
- league/container: ^2.2
- symfony/console: ~2.8|~3.0
- symfony/finder: ^3.1
- vlucas/phpdotenv: ^2.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpmd/phpmd: ^2.4
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^2.7
README
为了提高我们为基于Yii的项目构建模板的灵活性,我们构建了这个套件。从某种意义上说,这是广泛使用的YiiBootstrap
的最新一代,但它比之前提到的要结构化得多,复杂度也更低。
众所周知,Yii的启动过程配置非常繁琐。通常,main.php
脚本包含大量有关应用程序设置、组件、模块和参数的信息,很多时候我们发现自己正在处理一个包含大量设置的巨大文件,尽管这些设置按键排序,但由于行数众多,仍然难以理解。
ConfigKit试图解决这个问题,允许我们创建具有不同启动和配置构建过程的工程模板。它提出了以下配置文件夹结构
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
请注意,上述配置文件夹结构完全由您决定。ConfigKit
需要一个ConfigurationBuilder
,您负责开发,它可以采用上述建议或其他建议。有关ConfigurationBuilder
的示例,请访问https://github.com/sidekit/yii2-app-template/blob/master/src/App/Configuration/ConfigurationBuilder.php
配置文件示例
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'], ];
组件文件示例: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'), ];
环境设置覆盖
我们知道处理不同环境(如:测试、本地、预发布和生产环境)的设置有多么重要。处理它们的建议解决方案是添加一个名为所需环境的配置文件夹,其中包含与之前提到的文件夹相同的结构。我们从未发现自己需要克隆整个结构,所以您只需将其中的文件放置进去,这些文件包含您希望覆盖的设置。
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 ]
启动过程
我们相信应用程序的启动过程也应该像其配置一样结构化。这样,所有过程都更加清晰,更容易管理和扩展。在Yii 2建议的项目模板https://github.com/2amigos/yii2-app-template中,您可以看到ConfigKit
库加建议启动过程的实际示例。以下是一个Web应用程序的启动过程
<?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();
清洁代码
我们添加了一些开发工具,以便您用清洁的代码为库做出贡献
- PHP混乱检测器:接收一个给定的PHP源代码库,并查找该源中的多个潜在问题。
- PHP代码嗅探器:对PHP、JavaScript和CSS文件进行分词,并检测违反定义的编码标准。
- PHP代码修复器:分析一些PHP源代码,并尝试修复编码标准问题。
你应该按这个顺序使用它们。
使用php mess detector
所有选项都有的示例
./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode
使用code sniffer
./vendor/bin/phpcs -s --report=source --standard=PSR2 ./src
使用code fixer
我们添加了一个PHP代码修复器以标准化我们的代码。它包括Symfony、PSR2和某些贡献者规则。
./vendor/bin/php-cs-fixer fix ./src --config .php_cs.dist
测试
- 待办事项