@@ -124,6 +124,9 @@ This example returns the output: `bar`. The filters in the queue are applied in
124
124
This example illustrates basic usage of ` Zend\Config\Processor\Token ` :
125
125
126
126
``` php
127
+ use Zend\Config\Config;
128
+ use Zend\Config\Processor\Token as TokenProcessor;
129
+
127
130
// Provide the second parameter as boolean true to allow modifications:
128
131
$config = new Config(['foo' => 'Value is TOKEN'], true);
129
132
$processor = new TokenProcessor();
@@ -136,7 +139,7 @@ echo $config->foo;
136
139
137
140
This example returns the output: ` Value is TOKEN,Value is bar ` .
138
141
139
- As of version 3.1.0, you can also tell the ` Constant ` processor to process keys:
142
+ As of version 3.1.0, you can also tell the ` Token ` processor to process keys:
140
143
141
144
``` php
142
145
// At instantiation:
@@ -148,6 +151,44 @@ $processor->enableKeyProcessing();
148
151
149
152
When enabled, any token values found in keys will also be replaced.
150
153
154
+ ### Using Token processor as a simple environment processor
155
+
156
+ Token processor can be utilized to populate config values using common
157
+ format ` %env(ENV_VAR)% ` with values from environment by setting Token
158
+ processor ` $prefix ` and ` $suffix ` parameters to ` %env( ` and ` )% ` respectively:
159
+
160
+ ``` php
161
+ use Zend\Config\Config;
162
+ use Zend\Config\Processor\Token as TokenProcessor;
163
+
164
+ putenv('AMQP_PASSWORD=guest');
165
+
166
+ // Populate list if tokens to replace from environment:
167
+ $processor = new TokenProcessor(getenv(), '%env(', ')%');
168
+
169
+ // Provide the second parameter as boolean true to allow modifications:
170
+ $config = new Config([
171
+ 'host' => '127.0.0.1',
172
+ 'port' => 5672,
173
+ 'username' => '%env(AMQP_USER)%',
174
+ 'password' => '%env(AMQP_PASSWORD)%',
175
+ 'vhost' => '/',
176
+ ], true);
177
+
178
+ $processor->process($config);
179
+ print_r($config->toArray());
180
+ // Array
181
+ // (
182
+ // [host] => 127.0.0.1
183
+ // [port] => 5672
184
+ // [username] => %env(AMQP_USER)%
185
+ // [password] => guest
186
+ // [vhost] => /
187
+ // )
188
+ ```
189
+ Do note, however, that only values present in environment will be replaced.
190
+ This allows multiple fallback processors to be provided as a queue.
191
+
151
192
## Zend\\ Config\\ Processor\\ Translator
152
193
153
194
### Using Zend\\ Config\\ Processor\\ Translator
0 commit comments