https://github.com/zelenin/zend-expressive-config
Zend Expressive config manager
https://github.com/zelenin/zend-expressive-config
annotations config expressive manager yaml zend
Last synced: over 1 year ago
JSON representation
Zend Expressive config manager
- Host: GitHub
- URL: https://github.com/zelenin/zend-expressive-config
- Owner: zelenin
- License: mit
- Created: 2016-02-21T18:48:39.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-08-29T13:01:06.000Z (almost 9 years ago)
- Last Synced: 2025-02-14T00:03:49.363Z (over 1 year ago)
- Topics: annotations, config, expressive, manager, yaml, zend
- Language: PHP
- Size: 36.1 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Zend Expressive config manager [](https://travis-ci.org/zelenin/zend-expressive-config) [](https://coveralls.io/github/zelenin/zend-expressive-config?branch=master)
## Installation
### Composer
The preferred way to install this extension is through [Composer](http://getcomposer.org/).
Either run
```
php composer.phar require zelenin/zend-expressive-config "~2.1.0"
```
or add
```
"zelenin/zend-expressive-config": "~2.1.0"
```
to the require section of your ```composer.json```
## Usage
### Config providers
- PHP
- Yaml
- Arrays
- Collections
- Module config objects
### Example
```php
'bar']),
new FooModuleConfig(),
];
if ($productionMode) {
$providers = [new CacheProvider(__DIR__ . '/../data/cache/app-config.php', $providers)];
}
$manager = new ConfigManager($providers);
$config = $manager->getConfig();
```
Module config example:
```php
namespace Zelenin\FooModule\Config;
use Zelenin\Zend\Expressive\Config\Provider\ModuleConfigProvider;
use Zelenin\Zend\Expressive\Config\Provider\CollectionProvider;
use Zelenin\Zend\Expressive\Config\Provider\PhpProvider;
use Zelenin\Zend\Expressive\Config\Provider\YamlProvider;
final class FooModuleConfig extends ModuleConfigProvider
{
/**
* @return array
*/
public function getConfig()
{
return [
'foo' => 'bar'
];
// or
return require_once 'fooModuleConfig.php';
// or
return (new CollectionProvider([
new PhpProvider(__DIR__ . '/../Resources/config/*.global.php')),
new PhpProvider(__DIR__ . '/../Resources/config/*.local.php')),
new YamlProvider(__DIR__ . '/../Resources/config/*.global.yml'))
new YamlProvider(__DIR__ . '/../Resources/config/*.local.yml'))
]))->getConfig();
}
}
```
### Variables in YAML
You can resolve a variables like in the example below.
Config:
```yml
dependencies:
factories:
Zend\Stratigility\FinalHandler: 'Zend\Expressive\Container\TemplatedErrorHandlerFactory'
Zend\Expressive\Template\TemplateRendererInterface: 'Zend\Expressive\Twig\TwigRendererFactory'
twig:
cache_dir: '%rootDir%/data/cache/twig'
assets_url: '/'
assets_version: 1
globals: []
extensions: []
templates:
extension: 'html.twig'
paths:
application:
- '%moduleRootDir%/Resources/views'
```
Provider:
```php
resolveVariables(
(new CollectionProvider([
new YamlProvider(__DIR__ . '/../Resources/config/*.global.yml'),
new YamlProvider(__DIR__ . '/../Resources/config/*.local.yml'),
]))->getConfig()
);
}
/**
* @param array $config
*
* @return array
*/
private function resolveVariables(array $config): array
{
$variableRegistry = $this->getVariablesRegistry();
array_walk_recursive($config, function (&$value, $key) use ($variableRegistry) {
if (is_string($value)) {
if (preg_match('/%(.+)%/', $value, $matches)) {
$variable = $matches[1];
if (isset($variableRegistry[$variable])) {
$value = preg_replace('/%(.+)%/', $variableRegistry[$variable], $value);
}
}
}
});
return $config;
}
/**
* @return array
*/
private function getVariablesRegistry(): array
{
return [
'rootDir' => realpath(__DIR__ . '/../../..'),
'moduleRootDir' => realpath(__DIR__ . '/..'),
];
}
}
```
### Annotations
Supported annotations:
- ```@Factory(id=Service::class)```
- ```@Invokable(id=InvokableService::class)```
- ```@Inject```
- ```@Middleware(path="/path")```
- ```@Route(path="/path", methods={"GET", "POST"}, name="route-name")```
NB: ```@Middleware``` and ```@Route``` works only with [```programmatic_pipeline=false```](https://docs.zendframework.com/zend-expressive/features/container/factories/)
Usage: see examples in [Tests](https://github.com/zelenin/zend-expressive-config/tree/master/tests/Resources)
## Author
[Aleksandr Zelenin](https://github.com/zelenin/), e-mail: [aleksandr@zelenin.me](mailto:aleksandr@zelenin.me)