gulios/sxf-config

SXF 配置组件

1.1.1 2018-09-06 10:26 UTC

This package is auto-updated.

Last update: 2024-09-06 23:57:01 UTC


README

基于 Illuminate 和 DotEnv 包的配置组件。(Laravel 概念)

功能

  • 在 .env 中存储授权值
  • 使用缓存的配置
  • ENV 的默认值
  • 嵌套配置结构
示例用法
  1. 使用 Composer 将 SXF Config 安装到您的项目中

    composer require gulios/sxf-config
  2. 在代码中初始化

$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

它将创建包含所有配置的缓存文件。