Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Config] Allow overriding array nodes and prototyped array nodes #57873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Allow overriding prototyped array nodes with using `!`

7.1
---

Expand Down
9 changes: 9 additions & 0 deletions 9 src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class ArrayNode extends BaseNode implements PrototypeNodeInterface
{
private const OVERRIDE_OPERATOR = '!';

protected array $xmlRemappings = [];
protected array $children = [];
protected bool $allowFalse = false;
Expand Down Expand Up @@ -344,6 +346,13 @@ protected function mergeValues(mixed $leftSide, mixed $rightSide): mixed
}

foreach ($rightSide as $k => $v) {
if (str_starts_with($k, self::OVERRIDE_OPERATOR)) {
$keyWithoutOverrideOperator = ltrim($k, self::OVERRIDE_OPERATOR);

$leftSide[$keyWithoutOverrideOperator] = $v;
continue;
}

// no conflict
if (!\array_key_exists($k, $leftSide)) {
if (!$this->allowNewKeys) {
Expand Down
10 changes: 10 additions & 0 deletions 10 src/Symfony/Component/Config/Definition/PrototypedArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class PrototypedArrayNode extends ArrayNode
{
private const OVERRIDE_OPERATOR = '!';

protected PrototypeNodeInterface $prototype;
protected ?string $keyAttribute = null;
protected bool $removeKeyAttribute = false;
Expand Down Expand Up @@ -259,6 +261,14 @@ protected function mergeValues(mixed $leftSide, mixed $rightSide): mixed

$isList = array_is_list($rightSide);
foreach ($rightSide as $k => $v) {
// key starts with the '!' operator, so we override the left side
if (str_starts_with($k, self::OVERRIDE_OPERATOR)) {
$keyWithoutOverrideOperator = ltrim($k, self::OVERRIDE_OPERATOR);

$leftSide[$keyWithoutOverrideOperator] = $v;
continue;
}

// prototype, and key is irrelevant there are no named keys, append the element
if (null === $this->keyAttribute && $isList) {
$leftSide[] = $v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;
Expand Down Expand Up @@ -354,6 +355,70 @@ public function testPrototypedArrayNodeMerge(array $left, array $right, array $e
self::assertSame($result, $expected);
}

public function testOverridingPrototypedArrayNodeWithBangOperator(): void
{
$treeBuilder = new TreeBuilder('root');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->arrayNode('workflows')
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('from')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
;

$builtTree = $treeBuilder->buildTree();

$this->assertSame(
[
'workflows' => [
'workflow_a' => [
'from' => ['a', 'b'],
],
'workflow_b' => [
'from' => ['b', 'c'],
],
'workflow_c' => [
'from' => ['x', 'd'],
],
],
],
$builtTree->merge(
[
'workflows' => [
'workflow_a' => [
'from' => ['a', 'b'],
],
'workflow_b' => [
'from' => ['c', 'd'],
],
'workflow_c' => [
'from' => ['d', 'e'],
],
],
],
[
'workflows' => [
'!workflow_b' => [
'from' => ['b', 'c'],
],
'workflow_c' => [
'!from' => ['x', 'd'],
],
],
],
),
);
}

public static function getPrototypedArrayNodeDataToMerge(): array
{
return [
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.