activecollab/configrepository

1.0.0 2015-12-17 12:42 UTC

This package is auto-updated.

Last update: 2024-09-05 01:22:23 UTC


README

Build Status

此包提供了一种方式来组合配置仓库,从多个源读取和写入选项。

现有适配器

  1. DotEnvAdapter - 从.env文件加载环境变量(只读),
  2. EtcdAdapter - 从etcd集群读取和写入选项,
  3. PhpConstantsAdapter - 从充满常量的常规PHP文件加载选项。请注意,此文件不会被包含,但会静态解析,因此您无需担心这些常量会在您的代码中定义。

要编写自己的适配器,只需实现ActiveCollab\ConfigRepository\AdapterInterface

数据处理

仓库提供了一些方便的方法来检索和设置数据

<?php

use ActiveCollab\ConfigRepository\ConfigRepository;
use ActiveCollab\ConfigRepository\Adapter\PhpConstantsAdapter;

$repository = new ConfigRepository(new PhpConstantsAdapter('/path/to/config.php'));

// Check if option exists in any of the adapters
$repository->exists('CONFIG_NAME');

// Get value from first adapter that has it, or return default value if none of the adapters have it
$repository->get('CONFIG_NAME', 'default if not found');

// Get value from first adapter that has it, or throw an exception if it is not found in any adapter
$repository->mustGet('CONFIG_NAME'); // Throws an exception if option is not fpund

// Set value in all adapters where this option exists. Read-only adapters that have this option will throw an exception
$repository->set('CONFIG_NAME', 'value to set');

要处理特定适配器中的数据,找到它并使用相同的方法

<?php

use ActiveCollab\ConfigRepository\ConfigRepository;
use ActiveCollab\ConfigRepository\Adapter\PhpConstantsAdapter;

$repository = new ConfigRepository(new PhpConstantsAdapter('/path/to/config.php'));

$repository->getAdapter(PhpConstantsAdapter::class)->exists('CONFIG_NAME');
$repository->getAdapter(PhpConstantsAdapter::class)->get('CONFIG_NAME', 'default if not found');
$repository->getAdapter(PhpConstantsAdapter::class)->mustGet('CONFIG_NAME'); // Throws an exception if option is not fpund
$repository->getAdapter(PhpConstantsAdapter::class)->set('CONFIG_NAME', 'value to set');

适配器组合

在构建仓库实例时,您可以指定一个适配器列表

<?php

use ActiveCollab\ConfigRepository\ConfigRepository;
use ActiveCollab\ConfigRepository\Adapter\DotEnvAdapter;
use ActiveCollab\ConfigRepository\Adapter\PhpConstantsAdapter;

$repository = new ConfigRepository(new PhpConstantsAdapter(__DIR__ . '/Resources/config.simple.php'), new DotEnvAdapter(__DIR__ . '/Resources', '.env'));

以这种方式添加的适配器将按其类名索引,因此不能添加相同类的实例。为此,提供一个数组,其中键是适配器名称,值是适配器实例(如果键缺失,库将使用适配器的类名作为适配器名称)

<?php

use ActiveCollab\ConfigRepository\ConfigRepository;
use ActiveCollab\ConfigRepository\Adapter\DotEnvAdapter;
use ActiveCollab\ConfigRepository\Adapter\PhpConstantsAdapter;

$repository = new ConfigRepository([
    new PhpConstantsAdapter(__DIR__ . '/Resources/config.simple.php'),
    'second' => new PhpConstantsAdapter(__DIR__ . '/Resources/config.simple.php'),
], new DotEnvAdapter(__DIR__ . '/Resources', '.env'));

$repository->getAdapter(PhpConstantsAdapter::class); // Returns PhpConstantsAdapter instance
$repository->getAdapter('second');                   // Returns PhpConstantsAdapter instance
$repository->getAdapter(DotEnvAdapter::class);       // Returns DotEnvAdapter instance

也可以在任何时候使用addAdapter()方法添加适配器

<?php

use ActiveCollab\ConfigRepository\ConfigRepository;
use ActiveCollab\ConfigRepository\Adapter\DotEnvAdapter;
use ActiveCollab\ConfigRepository\Adapter\PhpConstantsAdapter;

$repository = new ConfigRepository(new PhpConstantsAdapter(__DIR__ . '/Resources/config.simple.php'));
$repository->addAdapter(new DotEnvAdapter($repository->get('DOT_ENV_DIR_PATH')));