katoba86 / yii2-configuration
Yii应用配置套件
Requires
- php: >=7.1
- league/container: ^3.3
- symfony/console: ^5.0
- vlucas/phpdotenv: ^4.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-10-02 11:38:21 UTC
README
Yii2 ConfigKit
Yii2灵活配置工具
对Yii2-ConfigKit 1.0进行了轻微修改
已将Composer的依赖关系适应到league/container的最新版本。
还实现了一个简单的基本测试。覆盖率不完整。可能很快就会完成。
为了提高我们构建基于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 SideKit\Config\ConfigKit; 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' => ConfigKit::config()->getBasePath(), 'vendorPath' => ConfigKit::config()->getVendorPath(), 'runtimePath' => ConfigKit::config()->getRuntimePath(), 'language' => ConfigKit::env()->get('APP_LANGUAGE'), 'bootstrap' => ['log'], ];
组件文件的示例:db.php
<?php use SideKit\Config\ConfigKit; return [ /* * -------------------------------------------------------------------------- * Connection * -------------------------------------------------------------------------- * * Represents a connection to a database via PDO. */ 'class' => 'yii\db\Connection', 'dsn' => ConfigKit::env()->get('DATABASE_DSN'), 'username' => ConfigKit::env()->get('DATABASE_USER'), 'password' => ConfigKit::env()->get('DATABASE_PASSWORD'), 'charset' => ConfigKit::env()->get('DATABASE_CHARSET'), 'tablePrefix' => ConfigKit::env()->get('DATABASE_TABLE_PREFIX'), ];
环境设置覆盖
我们知道不同环境(如:测试、本地、阶段和prod环境)的设置非常重要。处理这些设置的解决方案非常简单,只需添加一个名为所需环境的config文件夹,其中包含与之前提到的文件夹相同的结构。我们从未需要克隆整个结构,因此您只需简单地将包含您希望覆盖的设置的文件放入其中即可。
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/sidekit/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问题检测器
具有所有可用选项的示例
./vendor/bin/phpmd ./src text codesize,unusedcode,naming,design,controversial,cleancode
使用代码检查器
./vendor/bin/phpcs -s --report=source --standard=PSR2 ./src
使用代码修复器
我们添加了一个PHP代码修复器来标准化我们的代码。它包括Symfony、PSR2和一些贡献者规则。
./vendor/bin/php-cs-fixer fix ./src --config .php_cs
测试
- 待办