snapshotpl / zf-snap-var-config
Variables in array
Installs: 73 874
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: ^7.3 || ^8.0
Requires (Dev)
- laminas/laminas-modulemanager: ^2.3
- laminas/laminas-servicemanager: ^3.4
- phpunit/phpunit: ^9.3.8
Suggests
- laminas/laminas-modulemanager: ^2.3 Provides variables in configs after merge
- laminas/laminas-servicemanager: ^3.4 Provides service instance
This package is auto-updated.
Last update: 2024-10-14 21:47:26 UTC
README
Variables in array.
Additional provides module for Laminas for configurations.
Usage
Sometimes you want to use same values in other places in your config. What are you doing?
<?php $myIp = '127.0.0.1'; return [ 'db' => $myIp, 'memcache' => $myIp, ];
Of course it's working, but when you want to share $myIp
between separate config files thats challange! ZfSnapVarConfig make a magic here!
$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);
On this moment you can use selectors:
ZfSnapVarConfig\Value\Path
ZfSnapVarConfig\Value\Env
ZfSnapVarConfig\Value\Callback
You can write your own value manipulator. Just implement ZfSnapVarConfig\Value
and create instance in your config.
How to install?
Via composer.json
composer require snapshotpl/zf-snap-var-config
If you want to use this library as Laminas module add ZfSnapVarConfig
to your application.config.php
file.