b484763a7a
This is mainly intended for complex configurations to ease the work you have with normalizing different configuration formats (YAML, XML, and PHP). First, you have to set-up a config tree: $treeBuilder = new TreeBuilder(); $tree = $treeBuilder ->root('security_config', 'array') ->node('access_denied_url', 'scalar')->end() ->normalize('encoder') ->node('encoders', 'array') ->key('class') ->prototype('array') ->before()->ifString()->then(function($v) { return array('algorithm' => $v); })->end() ->node('algorithm', 'scalar')->end() ->node('encode_as_base64', 'scalar')->end() ->node('iterations', 'scalar')->end() ->end() ->end() ->end() ->buildTree() ; This tree and the metadata attached to the different nodes is then used to intelligently transform the passed config array: $normalizedConfig = $tree->normalize($config);
64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Symfony\Component\DependencyInjection\Configuration;
|
|
|
|
abstract class BaseNode implements NodeInterface
|
|
{
|
|
protected $name;
|
|
protected $parent;
|
|
protected $beforeTransformations;
|
|
protected $afterTransformations;
|
|
protected $nodeFactory;
|
|
|
|
public function __construct($name, NodeInterface $parent = null, $beforeTransformations = array(), $afterTransformations = array())
|
|
{
|
|
if (false !== strpos($name, '.')) {
|
|
throw new \InvalidArgumentException('The name must not contain ".".');
|
|
}
|
|
|
|
$this->name = $name;
|
|
$this->parent = $parent;
|
|
$this->beforeTransformations = $beforeTransformations;
|
|
$this->afterTransformations = $afterTransformations;
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function getPath()
|
|
{
|
|
$path = $this->name;
|
|
|
|
if (null !== $this->parent) {
|
|
$path = $this->parent->getPath().'.'.$path;
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
public final function normalize($value)
|
|
{
|
|
// run before transformations
|
|
foreach ($this->beforeTransformations as $transformation) {
|
|
$value = $transformation($value);
|
|
}
|
|
|
|
// validate type
|
|
$this->validateType($value);
|
|
|
|
// normalize value
|
|
$value = $this->normalizeValue($value);
|
|
|
|
// run after transformations
|
|
foreach ($this->afterTransformations as $transformation) {
|
|
$value = $transformation($value);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
abstract protected function validateType($value);
|
|
abstract protected function normalizeValue($value);
|
|
} |