Giter VIP home page Giter VIP logo

zend-expressive-config's Introduction

Zend Expressive config manager Build Status Coverage Status

Installation

Composer

The preferred way to install this extension is through Composer.

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

use Zelenin\FooModule\Config\FooModuleConfig;
use Zelenin\Zend\Expressive\Config\ConfigManager;
use Zelenin\Zend\Expressive\Config\Provider\ArrayProvider;
use Zelenin\Zend\Expressive\Config\Provider\CacheProvider;
use Zelenin\Zend\Expressive\Config\Provider\PhpProvider;
use Zelenin\Zend\Expressive\Config\Provider\YamlProvider;
use Zelenin\Zend\Expressive\Config\Provider\AnnotationProvider;

$productionMode = true; // environment variable

$providers =  [
    new PhpProvider(__DIR__ . '/../config/autoload/*.global.php'),
    new PhpProvider(__DIR__ . '/../config/autoload/*.local.php'),
    new YamlProvider(__DIR__ . '/../config/autoload/*.global.yml'),
    new YamlProvider(__DIR__ . '/../config/autoload/*.local.yml'),
    new AnnotationProvider(__DIR__ . '/../src', __DIR__ . '/../data/cache/factories'),
    new ArrayProvider(['foo' => '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:

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:

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
declare(strict_types=1);

namespace Zelenin\Application\Config;

use ArrayObject;
use Zelenin\Zend\Expressive\Config\Provider\CollectionProvider;
use Zelenin\Zend\Expressive\Config\Provider\ModuleConfigProvider;
use Zelenin\Zend\Expressive\Config\Provider\YamlProvider;

final class Provider extends ModuleConfigProvider
{
    /**
     * @return array
     */
    public function getConfig(): array
    {
        return $this->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

Usage: see examples in Tests

Author

Aleksandr Zelenin, e-mail: [email protected]

zend-expressive-config's People

Contributors

zelenin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

zend-expressive-config's Issues

i have dependency conflict

when i try to require zelenin/zend-expressive-config "~1.2.0"
i get dependency conflict:

$ composer require zelenin/zend-expressive-config "~1.2.0"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for zelenin/zend-expressive-config ~1.2.0 -> satisfiable by zelenin/zend-expressive-config[1.2.0].
    - Conclusion: remove symfony/yaml v3.2.3
    - Conclusion: don't install symfony/yaml v3.2.3
    - zelenin/zend-expressive-config 1.2.0 requires symfony/yaml ~2.8.0 || ~3.0.0 -> satisfiable by symfony/yaml[v2.8.0, v2.8.1, v2.8.10, v2.8.11, v2.8.12, v2.8.13, v2.8.14, v2.8.15, v2.8.16, v2.8.17, v2.8.2, v2.8.3, v2.8.4, v2.8.5, v2.8.6, v2.8.7, v2.8.8, v2.8.9, v3.0.0, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.0.7, v3.0.8, v3.0.9].
    - Can only install one of: symfony/yaml[v2.8.0, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.1, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.10, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.11, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.12, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.13, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.14, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.15, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.16, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.17, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.2, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.3, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.4, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.5, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.6, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.7, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.8, v3.2.3].
    - Can only install one of: symfony/yaml[v2.8.9, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.0, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.1, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.2, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.3, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.4, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.5, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.6, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.7, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.8, v3.2.3].
    - Can only install one of: symfony/yaml[v3.0.9, v3.2.3].
    - Installation request for symfony/yaml (locked at v3.2.3) -> satisfiable by symfony/yaml[v3.2.3].


Installation failed, reverting ./composer.json to its original content.

may be because i have installed phpunit 5.7.13 (with "symfony/yaml": "~2.1|~3.0")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.