8000 [DependencyInjection] Implement lazy collection type using generators by tgalopin · Pull Request #20907 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Implement lazy collection type using generators #20907

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

Merged
merged 2 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[DI] Add "=iterator" arguments to Yaml loader
  • Loading branch information
nicolas-grekas committed Jan 5, 2017
commit 1dbf52ad0340fde324d82eb1279ffbdd96b5f4ef
2 changes: 2 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
3.3.0
-----

* Add "iterator" argument type for lazy iteration over a set of values and services

* Using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and
will not be supported anymore in 4.0.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 + 8000 247,7 @@ private function dumpCallable($callable)
private function dumpValue($value)
{
if ($value instanceof IteratorArgument) {
$value = $value->getValues();
$value = array('=iterator' => $value->getValues());
}

if (is_array($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Loader;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -455,7 +456,17 @@ private function validate($content, $file)
private function resolveServices($value)
{
if (is_array($value)) {
$value = array_map(array($this, 'resolveServices'), $value);
if (array_key_exists('=iterator', $value)) {
if (1 !== count($value)) {
throw new InvalidArgumentException('Arguments typed "=iterator" must have no sibling keys.');
}
if (!is_array($value['=iterator'])) {
throw new InvalidArgumentException('Arguments typed "=iterator" must be arrays.');
}
$value = new IteratorArgument(array_map(array($this, 'resolveServices'), $value['=iterator']));
} else {
$value = array_map(array($this, 'resolveServices'), $value);
}
} elseif (is_string($value) && 0 === strpos($value, '@=')) {
return new Expression(substr($value, 2));
} elseif (is_string($value) && 0 === strpos($value, '@')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ services:
factory: ['@factory_simple', getInstance]
lazy_context:
class: LazyContext
arguments: [[foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container']]
arguments: [{ '=iterator': [foo, '@foo.baz', { '%foo%': 'foo is %foo%', foobar: '%foo%' }, true, '@service_container'] }]
lazy_context_ignore_invalid_ref:
class: LazyContext
arguments: [['@foo.baz', '@?invalid']]
arguments: [{ '=iterator': ['@foo.baz', '@?invalid'] }]
alias_for_foo: '@foo'
alias_for_alias: '@foo'
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function testLoadServices()
$this->assertEquals(array('decorated', 'decorated.pif-pouf', 5), $services['decorator_service_with_name_and_priority']->getDecoratedService());
}

public function testParsesLazyArgument()
public function testParsesIteratorArgument()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Tests\Loader;

use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -316,6 +317,17 @@ public function testTypes()
$this->assertEquals(array('Foo'), $container->getDefinition('baz_service')->getAutowiringTypes());
}

public function testParsesIteratorArgument()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services9.yml');

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

$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');
}

public function testAutowire()
{
$container = new ContainerBuilder();
Expand Down
< 2907 /div>
0