gulios / sxf-config
SXF 配置组件
1.1.1
2018-09-06 10:26 UTC
Requires
- php: ^7.1.3
- illuminate/config: 5.6.*
- nunomaduro/collision: 2.0.*
- symfony/console: 4.0.*
- symfony/filesystem: 4.0.*
- vlucas/phpdotenv: 2.5.*
Requires (Dev)
- filp/whoops: 2.1.*
- fzaninotto/faker: 1.7.*
- phpunit/phpunit: 7.0.*
README
基于 Illuminate 和 DotEnv 包的配置组件。(Laravel 概念)
功能
- 在 .env 中存储授权值
- 使用缓存的配置
- ENV 的默认值
- 嵌套配置结构
示例用法
-
使用 Composer 将 SXF Config 安装到您的项目中
composer require gulios/sxf-config
-
在代码中初始化
$basePath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR; $configuration = new Config(); $configuration->setConfigFilesPath($basePath . 'config/'); $configuration->setEnvFile($basePath . '.env'); $configuration->setCacheConfigFile($basePath . 'cache/configuration.php');
您可以通过以下方式获取配置
$configuration->getAll();
或
$configuration->get('app.debug')
等等
配置数据
包将扫描定义路径(setConfigFilesPath())中所有的 .php 文件。
示例文件 'config/app.php'
return [ 'first_test' => env('TEST', 'defaultvalue'), 'second_test' => [ 'key' => 'value' ] ];
如您所见,如果没有在 .env 文件中设置 TEST 值,它将获取 'defaultvalue'。
配置目录结构可以是嵌套的。
缓存
您只需执行
$configuration->clearCache(); or $configuration->createCache();
或者如果您使用 Symfony 控制台,可以使用两个命令
$config = new Config(); $config->setConfigFilesPath($basePath . 'config/'); $config->setEnvFile($basePath . '.env'); $config->setCacheConfigFile($basePath . 'cache/configuration.php'); $app = new Application(); $app->add( new ConfigCacheCommand( $config->getConfigFilesPath(), $config->getEnvFile(), $config->getCacheConfigFile() ) ); $app->add( new ConfigCacheClearCommand( $config->getConfigFilesPath(), $config->getEnvFile(), $config->getCacheConfigFile() ) ); $app->run();
然后运行
php bin/console config: Command "config:" is ambiguous. Did you mean one of these? config:create Create a cache file for faster configuration loading config:clear Remove the configuration cache file
它将创建包含所有配置的缓存文件。