sidekit / config
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
库和提出的引导过程的示例。
<?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
测试
- 待办事项