Skip to content

Commit a0cee96

Browse files
Merge branch '5.4' into 6.3
* 5.4: Dump Valid constaints on debug command #46544 [DependencyInjection] fix dump xml with array/object/enum default value [HttpFoundation] Add a slightly more verbose comment about a warning on UploadedFile [Messenger] BatchHandlerTrait - fix phpdoc typo Remove me from CODEOWNERS Always return bool from messenger amqp conncetion nack [Console] Fix linewraps in OutputFormatter
2 parents 6dcfbb9 + addc22f commit a0cee96

12 files changed

+163
-1
lines changed

Compiler/AutowirePass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,17 @@ private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot,
233233
unset($arguments[$j]);
234234
$arguments[$namedArguments[$j]] = $value;
235235
}
236-
if ($namedArguments || !$value instanceof $this->defaultArgument) {
236+
if (!$value instanceof $this->defaultArgument) {
237237
continue;
238238
}
239239

240240
if (\is_array($value->value) ? $value->value : \is_object($value->value)) {
241241
unset($arguments[$j]);
242242
$namedArguments = $value->names;
243+
}
244+
245+
if ($namedArguments) {
246+
unset($arguments[$j]);
243247
} else {
244248
$arguments[$j] = $value->value;
245249
}

Tests/Dumper/XmlDumperTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1818
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1919
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
20+
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
2021
use Symfony\Component\DependencyInjection\ContainerBuilder;
2122
use Symfony\Component\DependencyInjection\ContainerInterface;
2223
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
2324
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2425
use Symfony\Component\DependencyInjection\Reference;
26+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute;
27+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute;
28+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute;
2529
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
2630
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2731
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
@@ -278,6 +282,32 @@ public function testDumpHandlesEnumeration()
278282
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_enumeration.xml'), $dumper->dump());
279283
}
280284

285+
/**
286+
* @dataProvider provideDefaultClasses
287+
*/
288+
public function testDumpHandlesDefaultAttribute($class, $expectedFile)
289+
{
290+
$container = new ContainerBuilder();
291+
$container
292+
->register('foo', $class)
293+
->setPublic(true)
294+
->setAutowired(true)
295+
->setArguments([2 => true]);
296+
297+
(new AutowirePass())->process($container);
298+
299+
$dumper = new XmlDumper($container);
300+
301+
$this->assertSame(file_get_contents(self::$fixturesPath.'/xml/'.$expectedFile), $dumper->dump());
302+
}
303+
304+
public static function provideDefaultClasses()
305+
{
306+
yield [FooClassWithDefaultArrayAttribute::class, 'services_with_default_array.xml'];
307+
yield [FooClassWithDefaultObjectAttribute::class, 'services_with_default_object.xml'];
308+
yield [FooClassWithDefaultEnumAttribute::class, 'services_with_default_enumeration.xml'];
309+
}
310+
281311
public function testDumpServiceWithAbstractArgument()
282312
{
283313
$container = new ContainerBuilder();

Tests/Dumper/YamlDumperTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1818
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1919
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
20+
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
2021
use Symfony\Component\DependencyInjection\ContainerBuilder;
2122
use Symfony\Component\DependencyInjection\ContainerInterface;
2223
use Symfony\Component\DependencyInjection\Definition;
2324
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
2425
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2526
use Symfony\Component\DependencyInjection\Reference;
27+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute;
28+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute;
29+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute;
2630
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute;
2731
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
2832
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
@@ -162,6 +166,34 @@ public function testDumpHandlesEnumeration()
162166
$this->assertEquals(file_get_contents(self::$fixturesPath.'/yaml/services_with_enumeration.yml'), $dumper->dump());
163167
}
164168

169+
/**
170+
* @requires PHP 8.1
171+
*
172+
* @dataProvider provideDefaultClasses
173+
*/
174+
public function testDumpHandlesDefaultAttribute($class, $expectedFile)
175+
{
176+
$container = new ContainerBuilder();
177+
$container
178+
->register('foo', $class)
179+
->setPublic(true)
180+
->setAutowired(true)
181+
->setArguments([2 => true]);
182+
183+
(new AutowirePass())->process($container);
184+
185+
$dumper = new YamlDumper($container);
186+
187+
$this->assertSame(file_get_contents(self::$fixturesPath.'/yaml/'.$expectedFile), $dumper->dump());
188+
}
189+
190+
public static function provideDefaultClasses()
191+
{
192+
yield [FooClassWithDefaultArrayAttribute::class, 'services_with_default_array.yml'];
193+
yield [FooClassWithDefaultObjectAttribute::class, 'services_with_default_object.yml'];
194+
yield [FooClassWithDefaultEnumAttribute::class, 'services_with_default_enumeration.yml'];
195+
}
196+
165197
public function testDumpServiceWithAbstractArgument()
166198
{
167199
$container = new ContainerBuilder();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class FooClassWithDefaultArrayAttribute
6+
{
7+
public function __construct(
8+
array $array = ['a', 'b', 'c'],
9+
bool $firstOptional = false,
10+
bool $secondOptional = false
11+
) {}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class FooClassWithDefaultEnumAttribute
6+
{
7+
public function __construct(
8+
FooUnitEnum $enum = FooUnitEnum::FOO,
9+
bool $firstOptional = false,
10+
bool $secondOptional = false,
11+
) {}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class FooClassWithDefaultObjectAttribute
6+
{
7+
public function __construct(
8+
object $object = new \stdClass(),
9+
bool $firstOptional = false,
10+
bool $secondOptional = false,
11+
) {}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
5+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute" public="true" autowire="true">
6+
<argument key="secondOptional">true</argument>
7+
</service>
8+
</services>
9+
</container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
5+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultEnumAttribute" public="true" autowire="true">
6+
<argument key="secondOptional">true</argument>
7+
</service>
8+
</services>
9+
</container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerInterface" public="true" synthetic="true"/>
5+
<service id="foo" class="Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultObjectAttribute" public="true" autowire="true">
6+
<argument key="secondOptional">true</argument>
7+
</service>
8+
</services>
9+
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
services:
3+
service_container:
4+
class: Symfony\Component\DependencyInjection\ContainerInterface
5+
public: true
6+
synthetic: true
7+
foo:
8+
class: Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithDefaultArrayAttribute
9+
public: true
10+
autowire: true
11+
arguments: { secondOptional: true }

0 commit comments

Comments
 (0)