Skip to content

Commit 60fef16

Browse files
committed
[DependencyInjection] Implement lazy collection type using generators
1 parent 69dcf41 commit 60fef16

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

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

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

1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

14+
use Symfony\Component\DependencyInjection\LazyArgument;
1415
use Symfony\Component\DependencyInjection\Variable;
1516
use Symfony\Component\DependencyInjection\Definition;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -1348,6 +1349,13 @@ private function dumpValue($value, $interpolate = true)
13481349
}
13491350

13501351
return sprintf('array(%s)', implode(', ', $code));
1352+
} elseif ($value instanceof LazyArgument) {
1353+
$code = array();
1354+
foreach ($value->getValues() as $key => $reference) {
1355+
$code[] = sprintf(' yield %s => %s;', $this->dumpValue($key, $interpolate), $this->dumpValue($reference, $interpolate));
1356+
}
1357+
1358+
return sprintf("(function(){\n%s\n })()", implode("\n", $code));
13511359
} elseif ($value instanceof Definition) {
13521360
if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
13531361
return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Dumper;
1313

1414
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\LazyArgument;
1516
use Symfony\Component\DependencyInjection\Parameter;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\Definition;
@@ -283,6 +284,9 @@ private function convertParameters(array $parameters, $type, \DOMElement $parent
283284
if (is_array($value)) {
284285
$element->setAttribute('type', 'collection');
285286
$this->convertParameters($value, $type, $element, 'key');
287+
} elseif ($value instanceof LazyArgument) {
288+
$element->setAttribute('type', 'lazy');
289+
$this->convertParameters($value->getValues(), $type, $element, 'key');
286290
} elseif ($value instanceof Reference) {
287291
$element->setAttribute('type', 'service');
288292
$element->setAttribute('id', (string) $value);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection;
13+
14+
/**
15+
* Represents a collection of values to lazily iterate over.
16+
*
17+
* @author Titouan Galopin <galopintitouan@gmail.com>
18+
*/
19+
class LazyArgument
20+
{
21+
private $values;
22+
23+
/**
24+
* @param array $values The values lazily iterate over.
25+
*/
26+
public function __construct($values)
27+
{
28+
$this->values = $values;
29+
}
30+
31+
/**
32+
* @return array The services to create a lazy collection for.
33+
*/
34+
public function getValues()
35+
{
36+
return $this->values;
37+
}
38+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Alias;
1818
use Symfony\Component\DependencyInjection\Definition;
1919
use Symfony\Component\DependencyInjection\ChildDefinition;
20+
use Symfony\Component\DependencyInjection\LazyArgument;
2021
use Symfony\Component\DependencyInjection\Reference;
2122
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2223
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
@@ -401,6 +402,9 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true)
401402
case 'collection':
402403
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
403404
break;
405+
case 'lazy':
406+
$arguments[$key] = new LazyArgument($this->getArgumentsAsPhp($arg, $name, false));
407+
break;
404408
case 'string':
405409
$arguments[$key] = $arg->nodeValue;
406410
break;

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
<xsd:enumeration value="expression" />
190190
<xsd:enumeration value="string" />
191191
<xsd:enumeration value="constant" />
192+
<xsd:enumeration value="lazy" />
192193
</xsd:restriction>
193194
</xsd:simpleType>
194195

0 commit comments

Comments
 (0)