Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

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

Closed
2 tasks done
weierophinney opened this issue Dec 31, 2019 · 1 comment
Closed
2 tasks done

Comments

@weierophinney
Copy link
Member

  • 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


Originally posted by @eguvenc at zendframework/zend-config#50

@weierophinney
Copy link
Member Author

This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steering Committee.
If you have a security issue, please follow our security reporting guidelines.
If you wish to take on the role of maintainer, please nominate yourself

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

1 participant