Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bef2bb0

Browse files
committed
Add feature allowing change of Path Separator
1 parent 7e5f1cf commit bef2bb0

11 files changed

+98
-17
lines changed

src/Symfony/Component/Config/Definition/ArrayNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public function setIgnoreExtraKeys($boolean, $remove = true)
159159
*/
160160
public function setName($name)
161161
{
162+
$this->assertNameIsValid($name);
163+
162164
$this->name = $name;
163165
}
164166

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
*/
2424
abstract class BaseNode implements NodeInterface
2525
{
26+
const DEFAULT_PATH_SEPARATOR = '.';
27+
28+
private $pathSeparator;
29+
2630
protected $name;
2731
protected $parent;
2832
protected $normalizationClosures = array();
@@ -35,13 +39,18 @@ abstract class BaseNode implements NodeInterface
3539
/**
3640
* Constructor.
3741
*
38-
* @param string $name The name of the node
39-
* @param NodeInterface $parent The parent of this node
42+
* @param string $name The name of the node
43+
* @param NodeInterface $parent The parent of this node
44+
* @param string $pathSeparator The Path Separator that is used
4045
*
41-
* @throws \InvalidArgumentException if the name contains a period.
46+
* @throws \InvalidArgumentException if the name contains path separator.
4247
*/
43-
public function __construct($name, NodeInterface $parent = null)
48+
public function __construct($name, NodeInterface $parent = null, $pathSeparator = self::DEFAULT_PATH_SEPARATOR)
4449
{
50+
$this->pathSeparator = $pathSeparator;
51+
52+
$this->assertNameIsValid($name);
53+
4554
$this->name = $name;
4655
$this->parent = $parent;
4756
}
@@ -194,13 +203,11 @@ public function getName()
194203
*/
195204
public function getPath()
196205
{
197-
$path = $this->name;
198-
199206
if (null !== $this->parent) {
200-
$path = $this->parent->getPath().'.'.$path;
207+
return $this->parent->getPath().$this->pathSeparator.$this->name;
201208
}
202209

203-
return $path;
210+
return $this->name;
204211
}
205212

206213
/**
@@ -349,4 +356,18 @@ abstract protected function mergeValues($leftSide, $rightSide);
349356
* @return mixed The finalized value
350357
*/
351358
abstract protected function finalizeValue($value);
359+
360+
/**
361+
* Assert Given Node name is valid
362+
*
363+
* @param string $name
364+
*
365+
* @throws \InvalidArgumentException if name contains path separator.
366+
*/
367+
protected function assertNameIsValid($name)
368+
{
369+
if (false !== strpos($name, $this->pathSeparator)) {
370+
throw new \InvalidArgumentException('The name must not contain "' . $this->pathSeparator . '".');
371+
}
372+
}
352373
}

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ protected function getNodeBuilder()
409409
protected function createNode()
410410
{
411411
if (null === $this->prototype) {
412-
$node = new ArrayNode($this->name, $this->parent);
412+
$node = new ArrayNode($this->name, $this->parent, $this->getPathSeparator());
413413

414414
$this->validateConcreteNode($node);
415415

@@ -420,7 +420,7 @@ protected function createNode()
420420
$node->addChild($child->getNode());
421421
}
422422
} else {
423-
$node = new PrototypedArrayNode($this->name, $this->parent);
423+
$node = new PrototypedArrayNode($this->name, $this->parent, $this->getPathSeparator());
424424

425425
$this->validatePrototypeNode($node);
426426

src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Config\Definition\Builder;
1313

14+
use Symfony\Component\Config\Definition\BaseNode;
1415
use Symfony\Component\Config\Definition\NodeInterface;
1516
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
1617

@@ -19,8 +20,9 @@
1920
*
2021
* @author Johannes M. Schmitt <[email protected]>
2122
*/
22-
abstract class NodeDefinition implements NodeParentInterface
23+
abstract class NodeDefinition implements NodeParentInterface, PathSeparatorAwareInterface
2324
{
25+
private $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR;
2426
protected $name;
2527
protected $normalization;
2628
protected $validation;
@@ -340,4 +342,26 @@ protected function normalization()
340342
* @throws InvalidDefinitionException When the definition is invalid
341343
*/
342344
abstract protected function createNode();
345+
346+
/**
347+
* @param string $separator
348+
*/
349+
public function setPathSeparator($separator)
350+
{
351+
if ($this instanceof ParentNodeDefinitionInterface) {
352+
foreach ($this->children() as $child) {
353+
$child->setPathSeparator($separator);
354+
}
355+
}
356+
357+
$this->pathSeparator = $separator;
358+
}
359+
360+
/**
361+
* @return string
362+
*/
363+
protected function getPathSeparator()
364+
{
365+
return $this->pathSeparator;
366+
}
343367
}

src/Symfony/Component/Config/Definition/Builder/ParentNodeDefinitionInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
interface ParentNodeDefinitionInterface
2020
{
21+
/**
22+
* @return NodeDefinition[]
23+
*/
2124
public function children();
2225

2326
public function append(NodeDefinition $node);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Definition\Builder;
13+
14+
/**
15+
* An interface that must be implemented by nodes which knows about path separators.
16+
*/
17+
interface PathSeparatorAwareInterface
18+
{
19+
/**
20+
* @param string $separator
21+
*/
22+
public function setPathSeparator($separator);
23+
}

src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
*/
2121
class TreeBuilder implements NodeParentInterface
2222
{
23+
/**
24+
* @var NodeInterface|null
25+
*/
2326
protected $tree;
27+
28+
/**
29+
* @var NodeDefinition|null
30+
*/
2431
protected $root;
25-
protected $builder;
2632

2733
/**
2834
* Creates the root node.

src/Symfony/Component/Config/Definition/Builder/VariableNodeDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VariableNodeDefinition extends NodeDefinition
2727
*/
2828
protected function instantiateNode()
2929
{
30-
return new VariableNode($this->name, $this->parent);
30+
return new VariableNode($this->name, $this->parent, $this->getPathSeparator());
3131
}
3232

3333
/**

src/Symfony/Component/Config/Definition/EnumNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class EnumNode extends ScalarNode
2222
{
2323
private $values;
2424

25-
public function __construct($name, NodeInterface $parent = null, array $values = array())
25+
public function __construct($name, NodeInterface $parent = null, array $values = array(), $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
2626
{
2727
$values = array_unique($values);
2828
if (empty($values)) {
2929
throw new \InvalidArgumentException('$values must contain at least one element.');
3030
}
3131

32-
parent::__construct($name, $parent);
32+
parent::__construct($name, $parent, $pathSeparator);
3333
$this->values = $values;
3434
}
3535

src/Symfony/Component/Config/Definition/NumericNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class NumericNode extends ScalarNode
2323
protected $min;
2424
protected $max;
2525

26-
public function __construct($name, NodeInterface $parent = null, $min = null, $max = null)
26+
public function __construct($name, NodeInterface $parent = null, $min = null, $max = null, $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
2727
{
28-
parent::__construct($name, $parent);
28+
parent::__construct($name, $parent, $pathSeparator);
2929
$this->min = $min;
3030
$this->max = $max;
3131
}

src/Symfony/Component/Config/Definition/VariableNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function setAllowEmptyValue($boolean)
6969
*/
7070
public function setName($name)
7171
{
72+
$this->assertNameIsValid($name);
73+
7274
$this->name = $name;
7375
}
7476

0 commit comments

Comments
 (0)