snapshotpl/zf-snap-var-config

该包最新版本(4.1.0)没有可用的许可证信息。

数组中的变量

4.1.0 2021-06-14 14:18 UTC

README

数组中的变量。

为Laminas配置提供额外的模块。

用法

有时你希望在配置文件的其他地方使用相同的值。你怎么办?

<?php
$myIp = '127.0.0.1';
return [
    'db' => $myIp,
    'memcache' => $myIp,
];

当然它是有效的,但当你想在不同的配置文件之间共享 $myIp 时,这会是一个挑战!ZfSnapVarConfig在这里发挥了魔法作用!

$data = [
    'ips' => [
        'local' => '127.0.0.1',
        'memcache' => '127.0.0.2',
        'smtp' => '127.0.0.3'
    ],
    'email' => new ZfSnapVarConfig\Value\Env('ADMIN_EMAIL'),
    'db' => ZfSnapVarConfig\Value\Path::fromString('ips/local'),
    'memcache' => ZfSnapVarConfig\Value\Path::fromString('ips|memcache', '|'),
    'email' => [
        'smtp' => ZfSnapVarConfig\Value\Path::fromArray(['ips', 'smtp']),
        'default-mail' => ZfSnapVarConfig\Value\Path::fromString('email'),
        'reply-to' => ZfSnapVarConfig\Value\Path::fromString('email'),
        'other-address' => new ZfSnapVarConfig\Value\Path('ips', 'smtp'),
    ],
];

$service = new ZfSnapVarConfig\VarConfigService();
$replaced = $service->replace($data); // or $service($data);

assertSame([
    'ips' => [
        'local' => '127.0.0.1',
        'memcache' => '127.0.0.2',
        'smtp' => '127.0.0.3'
    ],
    'email' => 'your@email.com',
    'db' => '127.0.0.1',
    'memcache' => '127.0.0.2',
    'email' => [
        'smtp' => '127.0.0.3',
        'default-mail' => 'your@email.com',
        'reply-to' => 'your@email.com',
        'other-address' => '127.0.0.3',
    ],
], $replaced);

此时你可以使用选择器

  • ZfSnapVarConfig\Value\Path
  • ZfSnapVarConfig\Value\Env
  • ZfSnapVarConfig\Value\Callback

你可以编写自己的值操作器。只需实现 ZfSnapVarConfig\Value 并在配置中创建实例。

如何安装?

通过 composer.json

composer require snapshotpl/zf-snap-var-config

如果你想将此库作为Laminas模块使用,请将 ZfSnapVarConfig 添加到您的 application.config.php 文件中。