Skip to content

[DependencyInjection] allows references to be declared as "lazy" #6140

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Martin Hasoň <martin.hason@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*
* @api
*/
Expand Down Expand Up @@ -239,6 +240,14 @@ private function convertParameters($parameters, $type, \DOMElement $parent, $key
} elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
$element->setAttribute('on-invalid', 'ignore');
}

if ( ! $value->isStrict()) {
$element->setAttribute('strict', 'false');
}

if ($value->isLazy()) {
$element->setAttribute('lazy', 'true');
}
} elseif ($value instanceof Definition) {
$element->setAttribute('type', 'service');
$this->addService($value, null, $element);
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* YamlDumper dumps a service container as a YAML string.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*
* @api
*/
Expand Down Expand Up @@ -212,7 +213,15 @@ private function dumpValue($value)

return $code;
} elseif ($value instanceof Reference) {
return $this->getServiceCall((string) $value, $value);
$call = $this->getServiceCall((string) $value, $value);
if ($value->isLazy()) {
$call .= '~';
}
if ( ! $value->isStrict()) {
$call .= '=';
}

return $call;
} elseif ($value instanceof Parameter) {
return $this->getParameterCall((string) $value);
} elseif (is_object($value) || is_resource($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* XmlFileLoader loads XML files service definitions.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I was going to change the file, and saw that I had not yet added my name :) Anyway, I have made many small feature additions in this file that should warrant the tag.

If possible, I'd like to first talk about the general idea of this patch though. If we agree on that, we can also talk about when @author tags are justified, and whether this one (or others) need to be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was just curious, sorry for that

*/
class XmlFileLoader extends FileLoader
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* The YAML format does not support anonymous services (cf. the XML loader).
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class YamlFileLoader extends FileLoader
{
Expand Down Expand Up @@ -298,14 +299,20 @@ private function resolveServices($value)
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
}

$strict = true;
if ('=' === substr($value, -1)) {
$value = substr($value, 0, -1);
$strict = false;
} else {
$strict = true;
}

$lazy = false;
if ('~' === substr($value, -1)) {
$value = substr($value, 0, -1);
$lazy = true;
}

$value = new Reference($value, $invalidBehavior, $strict);
$value->setLazy($lazy);
}

return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="on-invalid" type="xsd:string" />
<xsd:attribute name="strict" type="boolean" />
<xsd:attribute name="lazy" type="boolean" />
</xsd:complexType>

<xsd:complexType name="argument" mixed="true">
Expand All @@ -139,6 +140,7 @@
<xsd:attribute name="index" type="xsd:integer" />
<xsd:attribute name="on-invalid" type="xsd:string" />
<xsd:attribute name="strict" type="boolean" />
<xsd:attribute name="lazy" type="boolean" />
</xsd:complexType>

<xsd:complexType name="call" mixed="true">
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/DependencyInjection/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Reference represents a service reference.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*
* @api
*/
Expand All @@ -23,6 +24,7 @@ class Reference
private $id;
private $invalidBehavior;
private $strict;
private $lazy = false;

/**
* Constructor.
Expand Down Expand Up @@ -69,4 +71,28 @@ public function isStrict()
{
return $this->strict;
}

/**
* Whether this reference should be lazily initialized if available.
*
* @return Boolean
*/
public function isLazy()
{
return $this->lazy;
}

/**
* Sets the lazily initialization status for this reference.
*
* @param Boolean $bool
*
* @return Reference
*/
public function setLazy($bool)
{
$this->lazy = $bool;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function getArgumentsAsPhp($name, $lowercase = true)
}

$arguments[$key] = new Reference((string) $arg['id'], $invalidBehavior, $strict);

if (isset($arg['lazy'])) {
$arguments[$key]->setLazy(self::phpize($arg['lazy']));
}

break;
case 'collection':
$arguments[$key] = $arg->getArgumentsAsPhp($name, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
<service id="alias_for_foo" alias="foo" />
<service id="another_alias_for_foo" alias="foo" public="false" />
<service id="factory_service" factory-method="getInstance" factory-service="baz_factory" />
<service id="lazy_deps_service">
<argument type="service" id="foo" lazy="true" />
<argument type="service" id="bar" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ services:
alias: foo
public: false
factory_service: { class: BazClass, factory_method: getInstance, factory_service: baz_factory }
lazy_deps_service:
arguments: ["@foo~", "@bar"]
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public function testLoadServices()
$this->assertEquals('getInstance', $services['factory_service']->getFactoryMethod());
$this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());

$args = $services['lazy_deps_service']->getArguments();
$this->assertTrue($args[0]->isLazy());
$this->assertFalse($args[1]->isLazy());

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses <service> elements');
$this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public function testLoadServices()
$this->assertEquals(array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), $services['method_call2']->getMethodCalls(), '->load() parses the method_call tag');
$this->assertEquals('baz_factory', $services['factory_service']->getFactoryService());

$args = $services['lazy_deps_service']->getArguments();
$this->assertTrue($args[0]->isLazy());
$this->assertFalse($args[1]->isLazy());

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
$this->assertEquals('foo', (string) $aliases['alias_for_foo'], '->load() parses aliases');
Expand Down