Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

What do you think about adding env processor for static files ? #50

Open
2 tasks done
eguvenc opened this issue Jun 9, 2018 · 1 comment
Open
2 tasks done

What do you think about adding env processor for static files ? #50

eguvenc opened this issue Jun 9, 2018 · 1 comment

Comments

@eguvenc
Copy link

eguvenc commented Jun 9, 2018

  • I was not able to find an open or closed issue matching what I'm seeing.
  • This is not a question. (Questions should be asked on chat (Signup here) or our forums.)

Code to reproduce the issue

# amqp.yaml
# 
amqp:
    host: 127.0.0.1
    port: 5672
    username: 'env(AMQP_USERNAME)'
    password: 'env(AMQP_PASSWORD)'
    vhost: /

namespace Zend\Config\Processor;

use Zend\Config\Config;
use Zend\Config\Exception;
use Zend\Config\Processor\Token;
use Zend\Config\Processor\ProcessorInterface;

class Env extends Token implements ProcessorInterface
{
    protected function doProcess($value, array $replacements)
    {
        if (! is_string($value)) {
            return parent::doProcess($value, $replacements);
        }

        if (false === strpos($value, 'env(')) {
            return parent::doProcess($value, $replacements);
        }

        $value = $this->parseEnvRecursive($value);

        return $value;
    }

    /**
     * Parse env variables
     * 
     * @param mixed $input input
     * @return string
     */
    protected function parseEnvRecursive($input)
    {
        $regex = '/env\((.*?)\)/';
        if (is_array($input)) {
            $input = getenv($input[1]);
        }
        return preg_replace_callback($regex, array($this, 'parseEnvRecursive'), $input);
    }
}

require 'vendor/autoload.php';

use Symfony\Component\Yaml\Yaml as SymfonyYaml;
use Zend\Config\Config;
use Zend\Config\Factory;
use Zend\Config\Processor;
use Zend\Config\Reader\Yaml as YamlConfig;

Factory::registerReader('yaml', new YamlConfig([SymfonyYaml::class, 'parse']));

putenv('AMQP_USERNAME=guest');

$config = Factory::fromFile(dirname(__FILE__).'/config/amqp.yaml');

$config = new Config($config, true);
$processor = new Processor\Env();
$processor->process($config);
$config->setReadOnly();

echo $config->amqp->username; // guest

Expected results

// guest

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-config; a new issue has been opened at laminas/laminas-config#3.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants