Zend\Config\Processor

Zend\Config\Processor provides the ability to perform operations on a Zend\Config\Config object. Zend\Config\Processor is itself an interface that defining two methods: process() and processValue().

zend-config provides the following concrete implementations:

What gets processed?

Typically, you will process configuration values. However, there are use cases for supplying constant and/or token keys; one common one is for using class-based constants as keys to avoid using magic "strings":

{
    "Acme\\Compoment::CONFIG_KEY": {}
}

As such, as of version 3.1.0, the Constant and Token processors can optionally also process the keys of the Config instance provided to them, by calling enableKeyProcessing() on their instances, or passing a boolean true value for the fourth constructor argument.

Zend\Config\Processor\Constant

Using Zend\Config\Processor\Constant

This example illustrates the basic usage of Zend\Config\Processor\Constant:

define ('TEST_CONST', 'bar');

// Provide the second parameter as boolean true to allow modifications:
$config = new Zend\Config\Config(['foo' => 'TEST_CONST'], true);
$processor = new Zend\Config\Processor\Constant();

echo $config->foo . ',';
$processor->process($config);
echo $config->foo;

This example returns the output: TEST_CONST,bar.

As of version 3.1.0, you can also tell the Constant processor to process keys:

// At instantiation:
$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\Filter

Using Zend\Config\Processor\Filter

This example illustrates basic usage of Zend\Config\Processor\Filter:

use Zend\Filter\StringToUpper;
use Zend\Config\Processor\Filter as FilterProcessor;
use Zend\Config\Config;

// Provide the second parameter as boolean true to allow modifications:
$config = new Config(['foo' => 'bar'], true);
$upper = new StringToUpper();

$upperProcessor = new FilterProcessor($upper);

echo $config->foo . ',';
$upperProcessor->process($config);
echo $config->foo;

This example returns the output: bar,BAR.

Zend\Config\Processor\Queue

Using Zend\Config\Processor\Queue

This example illustrates basic usage of Zend\Config\Processor\Queue:

use Zend\Filter\StringToLower;
use Zend\Filter\StringToUpper;
use Zend\Config\Processor\Filter as FilterProcessor;
use Zend\Config\Processor\Queue;
use Zend\Config\Config;

// Provide the second parameter as boolean true to allow modifications:
$config = new Config(['foo' => 'bar'], true);
$upper  = new StringToUpper();
$lower  = new StringToLower();

$lowerProcessor = new FilterProcessor($lower);
$upperProcessor = new FilterProcessor($upper);

$queue = new Queue();
$queue->insert($upperProcessor);
$queue->insert($lowerProcessor);
$queue->process($config);

echo $config->foo;

This example returns the output: bar. The filters in the queue are applied in FIFO (First In, First Out) order .

Zend\Config\Processor\Token

Using Zend\Config\Processor\Token

This example illustrates basic usage of Zend\Config\Processor\Token:

// Provide the second parameter as boolean true to allow modifications:
$config = new Config(['foo' => 'Value is TOKEN'], true);
$processor = new TokenProcessor();

$processor->addToken('TOKEN', 'bar');
echo $config->foo . ',';
$processor->process($config);
echo $config->foo;

This example returns the output: Value is TOKEN,Value is bar.

As of version 3.1.0, you can also tell the Constant processor to process keys:

// At instantiation:
$processor = new Zend\Config\Processor\Token($tokens, '', '', true);

// Or later, via a method call:
$processor->enableKeyProcessing();

When enabled, any token values found in keys will also be replaced.

Zend\Config\Processor\Translator

Using Zend\Config\Processor\Translator

This example illustrates basic usage of Zend\Config\Processor\Translator:

use Zend\Config\Config;
use Zend\Config\Processor\Translator as TranslatorProcessor;
use Zend\I18n\Translator\Translator;

// Provide the second parameter as boolean true to allow modifications:
$config = new Config(['animal' => 'dog'], true);

/*
 * The following mapping is used for the translation
 * loader provided to the translator instance:
 *
 * $italian = [
 *     'dog' => 'cane'
 * ];
 */

$translator = new Translator();
// ... configure the translator ...
$processor = new TranslatorProcessor($translator);

echo "English: {$config->animal}, ";
$processor->process($config);
echo "Italian: {$config->animal}";

This example returns the output: English: dog,Italian: cane.