PHP code example of zelenin / zend-expressive-config

1. Go to this page and download the library: Download zelenin/zend-expressive-config library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zelenin / zend-expressive-config example snippets




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();

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 


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__ . '/..'),
        ];
    }
}