ironedge/config

一个简单的对象,具有便于访问配置值的实用方法。

dev-master / 1.0.x-dev 2016-01-31 20:45 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:52:56 UTC


README

Build Status

此组件提供了一个简单的API来处理配置参数。它可以处理不同的 读取器写入器,以便从任何存储中读取和写入。

当前支持的读取器和写入器

用法

要开始使用配置对象,您只需要实例化它。默认读取器是 file,这意味着它默认从文件中加载配置。默认 写入器 也是 file,这意味着它将数据保存到文件。当然,您可以设置您想要的读取器和写入器,正如您稍后将看到的。

基本用法

在以下示例中,我们将数组设置为配置对象的初始数据,并通过我们 IronEdge\Component\Config\Config 类提供的API开始访问其数据。我们使用我们组件 ironedge/common-utils 提供的 DataTrait 暴露以下方法

<?php

namespace Your\Namespace;

use IronEdge\Component\Config\Config;

// Create a config instance. You can set initial data and options
// using its constructor.

$data = [
    'user'          => [
        'username'      => 'admin',
        'groups'        => [
            'primary'       => 'administrator',
            'secondary'     => ['development']
        ]
    ]
];

$config = new Config($data);

// Obtain the username. Returns: 'admin'

$config->get('user.username');

// Obtain the primary group. Returns: 'administrator'

$config->get('user.groups.primary');

// This attribute does not exist, so we return the default value used as the second argument: 'defaultProfile'

$config->get('user.profile.name', 'defaultProfile');

// Returns true because this attribute exists

$config->has('user.username');

// Returns false because this attribute does not exist

$config->has('user.profile.name');

// Sets the missing attribute of the last example. Note that it creates any missing attribute, at any depth.

$config->set('user.profile.name', 'myProfile');

// Obtain the newly created attribute. Returns: 'myProfile'

$config->get('user.profile.name');

从读取器加载配置数据

遵循上一个示例,假设您想从以下内容的 YML 文件中加载数据

user:
  profile:
    name:           newProfile
  createdAt:      2010-01-01 12:00:01
cacheDir:       %root_path%/cache

要加载此文件,请使用以下代码

<?php

// ...
// Include here the code of the last examples
// ...

// Load the YML file

$config->load(['file' => '/path/to/config.yml']);

// Returns: 'newProfile'

$config->get('user.profile.name');

// Returns: '2010-01-01 12:00:01'

$config->get('user.createdAt');

// Returns 'administrator'

$config->get('user.groups.primary');

load 方法将文件 /path/to/config.yml 的加载委托给 file 读取器。此读取器加载了 YML 文件,然后配置对象使用当前数据和从 YML 文件加载的数据执行了 array_replace_recursive

替换模板变量

您注意到参数 cacheDir 的值,%root_dir%/cache 吗?配置对象允许使用模板变量替换配置数据中找到的所有字符串值。

默认情况下,我们不提供任何模板变量,但您可以使用以下示例中的 templateVariables 选项设置它们

<?php

// ...
// Include here the code of the last examples
// ...

// This returns '%root_dir%/cache' since we didn't configure
// any template variables yet

$config->get('cacheDir');

// Now we set the template variable

$config->setOptions(['templateVariables' => ['%root_dir%' => '/my/root/dir']]);

// Now, it will return '/my/root/dir/cache'

$config->get('cacheDir');

保存配置数据

由于默认写入器是 file,如果调用 save 方法,它将配置数据保存到文件

<?php

// ...
// Include here the code of the last examples
// ...

// Save configuration data to file '/path/to/my_config.yml'

$config->save(['file' => '/path/to/my_config.yml']);

附加功能

这是此组件提供的附加功能。

将数据加载到特定键

有时您希望将配置数据加载到特定键而不是替换整个数据数组。您可以使用以下代码执行此操作:}

<?php

namespace Your\Namespace;

use IronEdge\Component\Config\Config;

$config = new Config(
    [
        'user'      => [
            'username' => 'myUser'
        ]
    ]
);

// Load profiles from file /path/to/profiles.yml
//
// Suppose this file has the following contents
//
// profiles:
//   - administrator
//   - development

$config->load(
    ['file'      => '/path/to/profiles.yml'],
    ['loadInKey' => 'user.profiles']
);

// This would return: ['administrator', 'development']

$config->get('user.profiles');

合并、替换、递归合并、递归替换

您可以使用以下方法执行这些操作中的任何一个

<?php

namespace Your\Namespace;

use IronEdge\Component\Config\Config;

$config = new Config(
    [
        'user' => [
            'username' => 'admin',
            'groups' => [
                'administrator'
            ]
        ]
    ]
);

// Internally it uses "array_merge".
// Config data will end like: ['user' => 'myUser', 'groups' => ['myGroup']]

$config->merge(['user' => 'myUser', 'groups' => ['myGroup']]);

// Internally it uses "array_merge_recursive".
// Config data will end like: ['user' => 'myUser', 'groups' => ['myGroup', 'myOtherGroup']]

$config->mergeRecursive(['user' => 'myUser', 'groups' => ['myOtherGroup']]);

// Internally it uses "array_replace".
// Config data will end like: ['user' => 'myOtherUser', 'groups' => ['myGroup']]

$config->replace(['user' => 'myOtherUser', 'groups' => ['myGroup']]);

// Internally it uses "array_replace_recursive".
// Config data will end like: ['user' => 'myUser', 'groups' => ['myOtherGroup']]

$config->replaceRecursive(['user' => 'myUser', 'groups' => ['myOtherGroup']]);

自定义读取器和写入器

您可以使用以下方式定义自己的读取器和写入器

<?php

namespace Your\Namespace;

use IronEdge\Component\Config\Config;

// This must implement IronEdge\Component\Config\Reader\ReaderInterface

$myReader = new MyReader();

// This must implement IronEdge\Component\Config\Writer\WriterInterface

$myWriter = new MyWriter();

$config = new Config(
    [],
    [
        'reader'        => $myReader,
        'writer'        => $myWriter
    ]
);

// Reader method **read** will receive values from **readerOptions**

$config->load(['readerOptions' => ['myReaderOption' => 'myReaderOptionValue']]);

// Writer method **writer** will receive values from **writerOptions**

$config->save(['writerOptions' => ['myWriterOption' => 'myWriterOptionValue']]);

当然,您可以使用任何读取器/写入器组合。例如,您可以从文件中读取配置,但将其写入数据库。