diff --git a/CHANGELOG.md b/CHANGELOG.md index 43e6d5c..f781f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. +## 3.3.1 - 2018-06-15 + +### Added + +- Added Env processor to allows users to read the value of the getenv() function in static configuration files. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.3.0 - TBD ### Added diff --git a/docs/book/processor.md b/docs/book/processor.md index 335339f..506a4ec 100644 --- a/docs/book/processor.md +++ b/docs/book/processor.md @@ -7,6 +7,7 @@ defining two methods: `process()` and `processValue()`. zend-config provides the following concrete implementations: - `Zend\Config\Processor\Constant`: manage PHP constant values. +- `Zend\Config\Processor\Env`: manage PHP getenv() function values. - `Zend\Config\Processor\Filter`: filter the configuration data using `Zend\Filter`. - `Zend\Config\Processor\Queue`: manage a queue of operations to apply to configuration data. - `Zend\Config\Processor\Token`: find and replace specific tokens. @@ -58,9 +59,39 @@ $processor = new Zend\Config\Processor\Constant(true, '', '', true); // Or later, via a method call: $processor->enableKeyProcessing(); ``` - When enabled, any constant values found in keys will also be replaced. +## Zend\\Config\\Processor\\Env + +### Using Zend\\Config\\Processor\\Env + +This example illustrates the basic usage of `Zend\Config\Processor\Env`: + +```php +putenv('AMQP_PASSWORD=guest'); + +use Zend\Config\Config; +use Zend\Config\Factory; +use Zend\Config\Processor\Env as EnvProcessor; + +$config = new Config([ + 'host' => '127.0.0.1', + 'port' => 5672, + 'username' => 'guest', + 'password' => 'env(AMQP_PASSWORD)', + 'vhost' => '/', + ], true); + +$processor = new EnvProcessor; +$processor->process($config); +$config->setReadOnly(); + +echo $config->amqp->password; +``` + +This example returns the output: `guest`. + + ## Zend\\Config\\Processor\\Filter ### Using Zend\\Config\\Processor\\Filter @@ -180,4 +211,4 @@ $processor->process($config); echo "Italian: {$config->animal}"; ``` -This example returns the output: `English: dog,Italian: cane`. +This example returns the output: `English: dog,Italian: cane`. \ No newline at end of file diff --git a/src/Processor/Env.php b/src/Processor/Env.php new file mode 100644 index 0000000..d3d3cae --- /dev/null +++ b/src/Processor/Env.php @@ -0,0 +1,50 @@ +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, [$this, 'parseEnvRecursive'], $input); + } +} diff --git a/test/Processor/EnvTest.php b/test/Processor/EnvTest.php new file mode 100644 index 0000000..6e97947 --- /dev/null +++ b/test/Processor/EnvTest.php @@ -0,0 +1,45 @@ + '127.0.0.1', + 'port' => 5672, + 'username' => 'env(AMQP_USERNAME)', + 'password' => 'env(AMQP_PASSWORD)', + 'vhost' => '/', + ]; + } + + public function testCanResolveEnvValues() + { + $config = new Config($this->getEnvProvider(), true); + + $processor = new EnvProcessor(); + $processor->process($config); + + $this->assertEquals('127.0.0.1', $config->get('host')); + $this->assertEquals(5672, $config->get('port')); + $this->assertEquals('guest', $config->get('username')); + $this->assertEquals('guest', $config->get('password')); + $this->assertEquals('/', $config->get('vhost')); + } +}