Skip to content

Commit c896546

Browse files
committed
Merge branch '6.4' into 7.1
* 6.4: fix tests [DependencyInjection] Add tests for repeating `#[Autoconfigure]` attributes [DependencyInjection] Fix handling of repeated `#[Autoconfigure]` attributes [SecurityBundle] Make security schema deterministic [Translations][Core] Fix security Italian translation. clean up PHP version checks Support the `unique_proxy_open` event
2 parents e226093 + be65ed4 commit c896546

9 files changed

+203
-4
lines changed

Loader/XmlFileLoader.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,33 @@ private function parseFileToDOM(string $file): \DOMDocument
458458
try {
459459
$dom = XmlUtils::loadFile($file, $this->validateSchema(...));
460460
} catch (\InvalidArgumentException $e) {
461-
throw new InvalidArgumentException(sprintf('Unable to parse file "%s": ', $file).$e->getMessage(), $e->getCode(), $e);
461+
$invalidSecurityElements = [];
462+
$errors = explode("\n", $e->getMessage());
463+
foreach ($errors as $i => $error) {
464+
if (preg_match("#^\[ERROR 1871] Element '\{http://symfony\.com/schema/dic/security}([^']+)'#", $error, $matches)) {
465+
$invalidSecurityElements[$i] = $matches[1];
466+
}
467+
}
468+
if ($invalidSecurityElements) {
469+
$dom = XmlUtils::loadFile($file);
470+
471+
foreach ($invalidSecurityElements as $errorIndex => $tagName) {
472+
foreach ($dom->getElementsByTagNameNS('http://symfony.com/schema/dic/security', $tagName) as $element) {
473+
if (!$parent = $element->parentNode) {
474+
continue;
475+
}
476+
if ('http://symfony.com/schema/dic/security' !== $parent->namespaceURI) {
477+
continue;
478+
}
479+
if ('provider' === $parent->localName || 'firewall' === $parent->localName) {
480+
unset($errors[$errorIndex]);
481+
}
482+
}
483+
}
484+
}
485+
if ($errors) {
486+
throw new InvalidArgumentException(sprintf('Unable to parse file "%s": ', $file).implode("/n", $errors), $e->getCode(), $e);
487+
}
462488
}
463489

464490
$this->validateExtensions($dom, $file);
@@ -855,6 +881,6 @@ private function loadFromExtensions(\DOMDocument $xml): void
855881
*/
856882
public static function convertDomElementToArray(\DOMElement $element): mixed
857883
{
858-
return XmlUtils::convertDomElementToArray($element);
884+
return XmlUtils::convertDomElementToArray($element, false);
859885
}
860886
}

Loader/YamlFileLoader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,9 @@ private function parseDefinition(string $id, array|string|null $service, string
455455
return $return ? $alias : $this->container->setAlias($id, $alias);
456456
}
457457

458+
$changes = [];
458459
if (null !== $definition) {
459-
// no-op
460+
$changes = $definition->getChanges();
460461
} elseif ($this->isLoadingInstanceof) {
461462
$definition = new ChildDefinition('');
462463
} elseif (isset($service['parent'])) {
@@ -479,7 +480,7 @@ private function parseDefinition(string $id, array|string|null $service, string
479480
$definition->setAutoconfigured($defaults['autoconfigure']);
480481
}
481482

482-
$definition->setChanges([]);
483+
$definition->setChanges($changes);
483484

484485
if (isset($service['class'])) {
485486
$definition->setClass($service['class']);

Tests/Compiler/RegisterAutoconfigureAttributesPassTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
use Symfony\Component\DependencyInjection\Reference;
2121
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureAttributed;
2222
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfiguredInterface;
23+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeated;
24+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeatedBindings;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeatedCalls;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeatedOverwrite;
27+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeatedProperties;
28+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AutoconfigureRepeatedTag;
2329
use Symfony\Component\DependencyInjection\Tests\Fixtures\LazyAutoconfigured;
2430
use Symfony\Component\DependencyInjection\Tests\Fixtures\LazyLoaded;
2531
use Symfony\Component\DependencyInjection\Tests\Fixtures\MultipleAutoconfigureAttributed;
@@ -80,6 +86,99 @@ public function testAutoconfiguredTag()
8086
$this->assertEquals([AutoconfiguredInterface::class => $expected], $container->getAutoconfiguredInstanceof());
8187
}
8288

89+
public function testAutoconfiguredRepeated()
90+
{
91+
$container = new ContainerBuilder();
92+
$container->register('foo', AutoconfigureRepeated::class)
93+
->setAutoconfigured(true);
94+
95+
(new RegisterAutoconfigureAttributesPass())->process($container);
96+
97+
$expected = (new ChildDefinition(''))
98+
->setLazy(true)
99+
->setPublic(true)
100+
->setShared(false);
101+
102+
$this->assertEquals([AutoconfigureRepeated::class => $expected], $container->getAutoconfiguredInstanceof());
103+
}
104+
105+
public function testAutoconfiguredRepeatedOverwrite()
106+
{
107+
$container = new ContainerBuilder();
108+
$container->register('foo', AutoconfigureRepeatedOverwrite::class)
109+
->setAutoconfigured(true);
110+
111+
(new RegisterAutoconfigureAttributesPass())->process($container);
112+
113+
$expected = (new ChildDefinition(''))
114+
->setLazy(true)
115+
->setPublic(false)
116+
->setShared(true);
117+
118+
$this->assertEquals([AutoconfigureRepeatedOverwrite::class => $expected], $container->getAutoconfiguredInstanceof());
119+
}
120+
121+
public function testAutoconfiguredRepeatedTag()
122+
{
123+
$container = new ContainerBuilder();
124+
$container->register('foo', AutoconfigureRepeatedTag::class)
125+
->setAutoconfigured(true);
126+
127+
(new RegisterAutoconfigureAttributesPass())->process($container);
128+
129+
$expected = (new ChildDefinition(''))
130+
->addTag('foo', ['priority' => 2])
131+
->addTag('bar');
132+
133+
$this->assertEquals([AutoconfigureRepeatedTag::class => $expected], $container->getAutoconfiguredInstanceof());
134+
}
135+
136+
public function testAutoconfiguredRepeatedCalls()
137+
{
138+
$container = new ContainerBuilder();
139+
$container->register('foo', AutoconfigureRepeatedCalls::class)
140+
->setAutoconfigured(true);
141+
142+
(new RegisterAutoconfigureAttributesPass())->process($container);
143+
144+
$expected = (new ChildDefinition(''))
145+
->addMethodCall('setBar', ['arg2'])
146+
->addMethodCall('setFoo', ['arg1']);
147+
148+
$this->assertEquals([AutoconfigureRepeatedCalls::class => $expected], $container->getAutoconfiguredInstanceof());
149+
}
150+
151+
public function testAutoconfiguredRepeatedBindingsOverwrite()
152+
{
153+
$container = new ContainerBuilder();
154+
$container->register('foo', AutoconfigureRepeatedBindings::class)
155+
->setAutoconfigured(true);
156+
157+
(new RegisterAutoconfigureAttributesPass())->process($container);
158+
159+
$expected = (new ChildDefinition(''))
160+
->setBindings(['$arg' => new BoundArgument('bar', false, BoundArgument::INSTANCEOF_BINDING, realpath(__DIR__.'/../Fixtures/AutoconfigureRepeatedBindings.php'))]);
161+
162+
$this->assertEquals([AutoconfigureRepeatedBindings::class => $expected], $container->getAutoconfiguredInstanceof());
163+
}
164+
165+
public function testAutoconfiguredRepeatedPropertiesOverwrite()
166+
{
167+
$container = new ContainerBuilder();
168+
$container->register('foo', AutoconfigureRepeatedProperties::class)
169+
->setAutoconfigured(true);
170+
171+
(new RegisterAutoconfigureAttributesPass())->process($container);
172+
173+
$expected = (new ChildDefinition(''))
174+
->setProperties([
175+
'$foo' => 'bar',
176+
'$bar' => 'baz',
177+
]);
178+
179+
$this->assertEquals([AutoconfigureRepeatedProperties::class => $expected], $container->getAutoconfiguredInstanceof());
180+
}
181+
83182
public function testMissingParent()
84183
{
85184
$container = new ContainerBuilder();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
7+
#[Autoconfigure(public: true, shared: false)]
8+
#[Autoconfigure(lazy: true)]
9+
class AutoconfigureRepeated
10+
{
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
7+
#[Autoconfigure(bind: ['$arg' => 'foo'])]
8+
#[Autoconfigure(bind: ['$arg' => 'bar'])]
9+
class AutoconfigureRepeatedBindings
10+
{
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
7+
#[Autoconfigure(calls: [['setBar', ['arg2']]])]
8+
#[Autoconfigure(calls: [['setFoo', ['arg1']]])]
9+
class AutoconfigureRepeatedCalls
10+
{
11+
public function setFoo(string $arg)
12+
{
13+
}
14+
15+
public function setBar(string $arg)
16+
{
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
7+
#[Autoconfigure(public: true, shared: false)]
8+
#[Autoconfigure(lazy: true, shared: true, public: false)]
9+
class AutoconfigureRepeatedOverwrite
10+
{
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
6+
7+
#[Autoconfigure(properties: ['$replaced' => 'to be replaced', '$bar' => 'existing to be replaced'])]
8+
#[Autoconfigure(properties: ['$foo' => 'bar', '$bar' => 'baz'])]
9+
class AutoconfigureRepeatedProperties
10+
{
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
6+
7+
#[AutoconfigureTag('foo', ['priority' => 2])]
8+
#[AutoconfigureTag('bar')]
9+
class AutoconfigureRepeatedTag
10+
{
11+
}

0 commit comments

Comments
 (0)