8000 [DI] Add "=iterator" arguments to Yaml loader · symfony/symfony@7b078f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b078f4

Browse files
[DI] Add "=iterator" arguments to Yaml loader
1 parent 5313943 commit 7b078f4

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private function dumpCallable($callable)
247247
private function dumpValue($value)
248248
{
249249
if ($value instanceof IteratorArgument) {
250-
$value = $value->getValues();
250+
$value = array('=iterator' => $value->getValues());
251251
}
252252

253253
if (is_array($value)) {

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Loader;
1313

1414
use Symfony\Component\DependencyInjection\Alias;
15+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1516
use Symfony\Component\DependencyInjection\ChildDefinition;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -455,7 +456,17 @@ private function validate($content, $file)
455456
private function resolveServices($value)
456457
{
457458
if (is_array($value)) {
458-
$value = array_map(array($this, 'resolveServices'), $value);
459+
if (array_key_exists('=iterator', $value)) {
460+
if (1 !== count($value)) {
461+
throw new InvalidArgumentException('Arguments typed "=iterator" must have no sibling keys.');
462+
}
463+
if (!is_array($value['=iterator'])) {
464+
throw new InvalidArgumentException('Arguments typed "=iterator" must be arrays.');
465+
}
466+
$value = new IteratorArgument(array_map(array($this, 'resolveServices'), $value['=iterator']));
467+
} else {
468+
$value = array_map(array($this, 'resolveServices'), $value);
469+
}
459470
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
460471
return new Expression(substr($value, 2));
461472
} elseif (is_string($value) && 0 === strpos($value, '@')) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ services:
109109
factory: ['@factory_simple', getInstance]
110110
lazy_context:
111111
class: LazyContext
112-
arguments: [[foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']]
112+
arguments: [{ '=iterator': [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container'] }]
113113
lazy_context_ignore_invalid_ref:
114114
class: LazyContext
115-
arguments: [['@foo.baz', '@?invalid']]
115+
arguments: [{ '=iterator': ['@foo.baz', '@?invalid'] }]
116116
alias_for_foo: '@foo'
117117
alias_for_alias: '@foo'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public function testLoadServices()
258258
$this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
259259
}
260260

261-
public function testParsesLazyArgument()
261+
public function testParsesIteratorArgument()
262262
{
263263
$container = new ContainerBuilder();
264264
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Tests\Loader;
1313

14+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Reference;
1617
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -316,6 +317,17 @@ public function testTypes()
316317
$this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
317318
}
318319

320+
public function testParsesIteratorArgument()
321+
{
322+
$container = new ContainerBuilder();
323+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
324+
$loader->load('services9.yml');
325+
326+
$lazyDefinition = $container->getDefinition('lazy_context');
327+
328+
$this->assertEquals(array(new IteratorArgument(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
329+
}
330+
319331
public function testAutowire()
320332
{
321333
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0