8000 bug #22790 [DependencyInjection] Fix dumping of RewindableGenerator w… · symfony/symfony@5c40e12 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c40e12

Browse files
bug #22790 [DependencyInjection] Fix dumping of RewindableGenerator with empty IteratorArgument (meyerbaptiste)
This PR was merged into the 3.3 branch. Discussion ---------- [DependencyInjection] Fix dumping of RewindableGenerator with empty IteratorArgument | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22780 | License | MIT | Doc PR | N/A According with #22780 (comment), when an `IteratorArgument` is empty, the closure generated by the `PhpDumper` should be `function () { return new EmptyIterator();` instead of `function () {}`, which is an invalid traversable for the `RewindableGenerator`. Commits ------- c2db0c1 [DependencyInjection] Fix dumping of RewindableGenerator with empty IteratorArgument
2 parents 297faba + c2db0c1 commit 5c40e12

File tree

11 files changed

+62
-26
lines changed

11 files changed

+62
-26
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,24 +1429,29 @@ private function dumpValue($value, $interpolate = true)
14291429
}
14301430

14311431
if ($value instanceof IteratorArgument) {
1432-
$countCode = array();
1433-
$countCode[] = 'function () {';
14341432
$operands = array(0);
1435-
14361433
$code = array();
14371434
$code[] = 'new RewindableGenerator(function () {';
1438-
foreach ($value->getValues() as $k => $v) {
1439-
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
1440-
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
1441-
foreach (explode("\n", $v) as $v) {
1442-
if ($v) {
1443-
$code[] = ' '.$v;
1435+
1436+
if (!$values = $value->getValues()) {
1437+
$code[] = ' return new \EmptyIterator();';
1438+
} else {
1439+
$countCode = array();
1440+
$countCode[] = 'function () {';
1441+
1442+
foreach ($values as $k => $v) {
1443+
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
1444+
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
1445+
foreach (explode("\n", $v) as $v) {
1446+
if ($v) {
1447+
$code[] = ' '.$v;
1448+
}
14441449
}
14451450
}
1446-
}
14471451

1448-
$countCode[] = sprintf(' return %s;', implode(' + ', $operands));
1449-
$countCode[] = ' }';
1452+
$countCode[] = sprintf(' return %s;', implode(' + ', $operands));
1453+
$countCode[] = ' }';
1454+
}
14501455

14511456
$code[] = sprintf(' }, %s)', count($operands) > 1 ? implode("\n", $countCode) : $operands[0]);
14521457

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,17 @@ public function testCreateServiceWithIteratorArgument()
449449
$builder->register('bar', 'stdClass');
450450
$builder
451451
->register('lazy_context', 'LazyContext')
452-
->setArguments(array(new IteratorArgument(array('k1' => new Reference('bar'), new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))))
452+
->setArguments(array(
453+
new IteratorArgument(array('k1' => new Reference('bar'), new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))),
454+
new IteratorArgument(array()),
455+
))
453456
;
454457

455458
$lazyContext = $builder->get('lazy_context');
456459
$this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyValues);
460+
$this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyEmptyValues);
457461
$this->assertCount(1, $lazyContext->lazyValues);
462+
$this->assertCount(0, $lazyContext->lazyEmptyValues);
458463

459464
$i = 0;
460465
foreach ($lazyContext->lazyValues as $k => $v) {
@@ -465,6 +470,13 @@ public function testCreateServiceWithIteratorArgument()
465470

466471
// The second argument should have been ignored.
467472
$this->assertEquals(1, $i);
473+
474+
$i = 0;
475+
foreach ($lazyContext->lazyEmptyValues as $k => $v) {
476+
++$i;
477+
}
478+
479+
$this->assertEquals(0, $i);
468480
}
469481

470482
/**

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ public function testLazyArgumentProvideGenerator()
436436
$container->register('lazy_referenced', 'stdClass');
437437
$container
438438
->register('lazy_context', 'LazyContext')
439-
->setArguments(array(new IteratorArgument(array('k1' => new Reference('lazy_referenced'), 'k2' => new Reference('service_container')))))
439+
->setArguments(array(
440+
new IteratorArgument(array('k1' => new Reference('lazy_referenced'), 'k2' => new Reference('service_container'))),
441+
new IteratorArgument(array()),
442+
))
440443
;
441444
$container->compile();
442445

@@ -447,6 +450,9 @@ public function testLazyArgumentProvideGenerator()
447450
$lazyContext = $container->get('lazy_context');
448451

449452
$this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyValues);
453+
$this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyEmptyValues);
454+
$this->assertCount(2, $lazyContext->lazyValues);
455+
$this->assertCount(0, $lazyContext->lazyEmptyValues);
450456

451457
$i = -1;
452458
foreach ($lazyContext->lazyValues as $k => $v) {
@@ -461,6 +467,8 @@ public function testLazyArgumentProvideGenerator()
461467
break;
462468
}
463469
}
470+
471+
$this->assertEmpty(iterator_to_array($lazyContext->lazyEmptyValues));
464472
}
465473

466474
public function testClosureProxy()

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@
134134
;
135135
$container
136136
->register('lazy_context', 'LazyContext')
137-
->setArguments(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')))))
137+
->setArguments(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container'))), new IteratorArgument(array())))
138138
;
139139
$container
140140
->register('lazy_context_ignore_invalid_ref', 'LazyContext')
141-
->setArguments(array(new IteratorArgument(array(new Reference('foo.baz'), new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))))
< 10000 /td>141+
->setArguments(array(new IteratorArgument(array(new Reference('foo.baz'), new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))), new IteratorArgument(array())))
142142
;
143143
$container
144144
->register('closure_proxy', 'BarClass')

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ public function getProxyCode(Definition $definition)
102102
class LazyContext
103103
{
104104
public $lazyValues;
105+
public $lazyEmptyValues;
105106

106-
public function __construct($lazyValues)
107+
public function __construct($lazyValues, $lazyEmptyValues)
107108
{
108109
$this->lazyValues = $lazyValues;
110+
$this->lazyEmptyValues = $lazyEmptyValues;
109111
}
110112
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ protected function getLazyContextService()
323323
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
324324
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
325325
yield 'k2' => $this;
326-
}, 2));
326+
}, 2), new RewindableGenerator(function () {
327+
return new \EmptyIterator();
328+
}, 0));
327329
}
328330

329331
/**
@@ -343,7 +345,9 @@ protected function getLazyContextIgnoreInvalidRefService()
343345
}
344346
}, function () {
345347
return 1 + (int) ($this->has('invalid'));
346-
}));
348+
}), new RewindableGenerator(function () {
349+
return new \EmptyIterator();
350+
}, 0));
347351
}
348352

349353
/**

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ protected function getLazyContextService()
326326
return $this->services['lazy_context'] = new \LazyContext(new RewindableGenerator(function () {
327327
yield 'k1' => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
328328
yield 'k2' => $this;
329-
}, 2));
329+
}, 2), new RewindableGenerator(function () {
330+
return new \EmptyIterator();
331+
}, 0));
330332
}
331333

332334
/**
@@ -341,7 +343,9 @@ protected function getLazyContextIgnoreInvalidRefService()
341343
{
342344
return $this->services['lazy_context_ignore_invalid_ref'] = new \LazyContext(new RewindableGenerator(function () {
343345
yield 0 => ${($_ = isset($this->services['foo.baz']) ? $this->services['foo.baz'] : $this->get('foo.baz')) && false ?: '_'};
344-
}, 1));
346+
}, 1), new RewindableGenerator(function () {
347+
return new \EmptyIterator();
348+
}, 0));
345349
}
346350

347351
/**

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@
121121
<argument key="k1" type="service" id="foo.baz"/>
122122
<argument key="k2" type="service" id="service_container"/>
123123
</argument>
124+
<argument type="iterator"/>
124125
</service>
125126
<service id="lazy_context_ignore_invalid_ref" class="LazyContext">
126127
<argument type="iterator">
127128
<argument type="service" id="foo.baz"/>
128129
<argument type="service" id="invalid" on-invalid="ignore"/>
129130
</argument>
131+
<argument type="iterator"/>
130132
</service>
131133
<service id="closure_proxy" class="BarClass">
132134
<argument type="closure-proxy" id="closure_proxy" method="getBaz"/>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ services:
112112
factory: ['@factory_simple', getInstance]
113113
lazy_context:
114114
class: LazyContext
115-
arguments: [!iterator {'k1': '@foo.baz', 'k2': '@service_container'}]
115+
arguments: [!iterator {'k1': '@foo.baz', 'k2': '@service_container'}, !iterator []]
116116
lazy_context_ignore_invalid_ref:
117117
class: LazyContext
118-
arguments: [!iterator ['@foo.baz', '@?invalid']]
118+
arguments: [!iterator ['@foo.baz', '@?invalid'], !iterator []]
119119
closure_proxy:
120120
class: BarClass
121121
arguments: [!closure_proxy ['@closure_proxy', getBaz]]

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testParsesIteratorArgument()
273273

274274
$lazyDefinition = $container->getDefinition('lazy_context');
275275

276-
$this->assertEquals(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')))), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
276+
$this->assertEquals(array(new IteratorArgument(array('k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container'))), new IteratorArgument(array())), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
277277
}
278278

279279
public function testParsesTags()
@@ -659,7 +659,6 @@ public function testDefaults()
659659

660660
$this->assertFalse($container->getDefinition('no_defaults')->isAutowired());
661661

662-
663662
$this->assertTrue($container->getDefinition('child_def')->isPublic());
664663
$this->assertSame(array('foo' => array(array())), $container->getDefinition('child_def')->getTags());
665664
$this->assertFalse($container->getDefinition('child_def')->isAutowired());

0 commit comments

Comments
 (0)
0