Laravel集成Yaconf扩展
前言
Yaconf扩展是大神鸟哥的作品,下面将讲解如何集成到Laravel。
环境
php 版本:7.2.6
Laravel 版本:7.30.6
具体步骤
Laravel有两个访问入口,一个是通过访问public/index.php,另一个则是通过命令行访问php artisan,所以需要改动两个加载配置的地方。分别是:
app/Http/Kernel.php和app/Console/Kernel.php,将两个类在父类中$bootstrappers属性覆盖成我们自定义的配置加载类。如下图:
首先安装yaconf扩展,这部分可自行百度完成。
然后在php.ini中配置存放配置文件的目录下,将项目config目录下的各个配置文件按照yaconf能读取的格式转成类似app.ini、auth.ini等。下面是app.ini部分配置内容(适用于beta环境):
[beta]
name=Laravel
env=beta
debug=false
url="http://test.com"
asset_url="http://test.com"
timezone=PRC
local=en
fallback_local=en
faker_local=en_US
key="base64:kmC2L0ZjhkzZJdDmtrAqT4T1z2od3Ty/3tV9oWDitMI="
cipher="AES-256-CBC"
providers.0="Illuminate\Auth\AuthServiceProvider"
providers.1="Illuminate\Broadcasting\BroadcastServiceProvider"
providers.2="Illuminate\Bus\BusServiceProvider"
providers.3="Illuminate\Cache\CacheServiceProvider"
providers.4="Illuminate\Foundation\Providers\ConsoleSupportServiceProvider"
providers.5="Illuminate\Cookie\CookieServiceProvider"
providers.6="Illuminate\Database\DatabaseServiceProvider"
providers.7="Illuminate\Encryption\EncryptionServiceProvider"
providers.8="Illuminate\Filesystem\FilesystemServiceProvider"
providers.9="Illuminate\Foundation\Providers\FoundationServiceProvider"
providers.10="Illuminate\Hashing\HashServiceProvider"
providers.11="Illuminate\Mail\MailServiceProvider"
providers.12="Illuminate\Notifications\NotificationServiceProvider"
providers.13="Illuminate\Pagination\PaginationServiceProvider"
providers.14="Illuminate\Pipeline\PipelineServiceProvider"
providers.15="Illuminate\Queue\QueueServiceProvider"
providers.16="\App\Providers\DeferServiceProvider"
providers.17="Illuminate\Auth\Passwords\PasswordResetServiceProvider"
providers.18="Illuminate\Session\SessionServiceProvider"
providers.19="Illuminate\Translation\TranslationServiceProvider"
providers.20="Illuminate\Validation\ValidationServiceProvider"
providers.21="Illuminate\View\ViewServiceProvider"
providers.22="App\Providers\AppServiceProvider"
providers.23="App\Providers\AuthServiceProvider"
providers.24="App\Providers\EventServiceProvider"
providers.25="App\Providers\RouteServiceProvider"
.env文件中增加配置项,指明要读取的配置文件名
YACONF_FILES=app,auth,broadcasting,cache,cors,database,filesystems,hashing,logging,mail,queue,services,session,view
这时我们就可以写自定义的配置加载类了。
<?php
namespace App\Http\Services\Lib;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
class LoadConfiguration
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
$items = $this->loadConfigurationYaconfFiles();
// Next we will spin through all of the configuration files in the configuration
// directory and load each one into the repository. This will make all of the
// options available to the developer for use in various parts of this app.
$app->instance('config', $config = new Repository($items));
// Finally, we will set the application's environment based on the configuration
// values that were loaded. We will pass a callback which will be used to get
// the environment in a web context where an "--env" switch is not present.
$app->detectEnvironment(function () use ($config) {
return $config->get('app.env', 'production');
});
date_default_timezone_set($config->get('app.timezone', 'UTC'));
mb_internal_encoding('UTF-8');
}
/**
* Load the configuration items from all of the yaconf files.
*
* @return array
*/
protected function loadConfigurationYaconfFiles() {
$env = env('APP_ENV', 'test');
$config_files = explode(',', env('YACONF_FILES', ''));
$result = [];
foreach ($config_files as $config_file) {
$result[$config_file] = \Yaconf::get($config_file.'.'.$env);
}
return $result;
}
}
最后,分别在app/Http/Kernel.php和app/Console/Kernel.php替换成我们自定义的类:
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The bootstrap classes for the application.
*
* @var array
*/
protected $bootstrappers = [
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
//\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
//自定义配置加载类
\App\Http\Services\Lib\LoadConfiguration::class,
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
\Illuminate\Foundation\Bootstrap\RegisterFacades::class,
\Illuminate\Foundation\Bootstrap\RegisterProviders::class,
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];
测试如果报错,则需要检查.ini配置文件中的格式是否正确。
参考
总结
不满足于当下,追求更多可能。
本作品采用《CC 协议》,转载必须注明作者和本文链接
这样就不符合12factor设计标准了。