Skip to content

[DependencyInjection] make dumping inlined configurators to XML possible #13557

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
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ public function isAbstract()
/**
* Sets a configurator to call after the service is fully initialized.
*
* @param callable $callable A PHP callable
* @param callable|Definition $callable A PHP callable or a service definition
Copy link
Member

Choose a reason for hiding this comment

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

technically, it is not a callable. It is the definition of a callable (for object callables, it would be array(new Reference(...), 'configureThings')

and it cannot be a Definition object itself. it can be array(new Definition(...), 'configureThings'). the configurator will be a method of a service, not the service itself

*
* @return Definition The current instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ private function addService($definition, $id, \DOMElement $parent)
$this->addMethodCalls($definition->getMethodCalls(), $service);

if ($callable = $definition->getConfigurator()) {
$configurator = $this->document->createElement('configurator');
if (is_array($callable)) {
$configurator = $this->document->createElement('configurator');
$configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
$configurator->setAttribute('method', $callable[1]);
} elseif ($callable instanceof Definition) {
Copy link
Member

Choose a reason for hiding this comment

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

this is not where the Definition will be. It will be in $callable[0]

$configurator = $this->document->createElement('configurator-service');
$this->addService($callable, null, $configurator);
} else {
$configurator = $this->document->createElement('configurator');
$configurator->setAttribute('function', $callable);
}
$service->appendChild($configurator);
Expand Down
24 changes: 18 additions & 6 deletions src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,28 @@ private function parseDefinitions(\DOMDocument $xml, $file)
}

foreach ($services as $service) {
$this->parseDefinition((string) $service->getAttribute('id'), $service, $file);
if (null !== $definition = $this->parseDefinition($service, $file)) {
$this->container->setDefinition((string) $service->getAttribute('id'), $definition);
}
}
}

/**
* Parses an individual Definition.
*
* @param string $id
* @param \DOMElement $service
* @param string $file
*
* @return Definition
*/
private function parseDefinition($id, \DOMElement $service, $file)
private function parseDefinition(\DOMElement $service, $file)
{
if ($alias = $service->getAttribute('alias')) {
$public = true;
if ($publicAttr = $service->getAttribute('public')) {
$public = XmlUtils::phpize($publicAttr);
}
$this->container->setAlias($id, new Alias($alias, $public));
$this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));

return;
}
Expand Down Expand Up @@ -174,6 +177,13 @@ private function parseDefinition($id, \DOMElement $service, $file)
}
}

$configuratorService = $this->getChildren($service, 'configurator-service');

if (count($configuratorService) === 1) {
$serviceNode = $this->getChildren($configuratorService[0], 'service');
$definition->setConfigurator($this->parseDefinition($serviceNode[0], $file));
}

foreach ($this->getChildren($service, 'call') as $call) {
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument'));
}
Expand All @@ -200,7 +210,7 @@ private function parseDefinition($id, \DOMElement $service, $file)
$definition->setDecoratedService($value, $renameId);
}

$this->container->setDefinition($id, $definition);
return $definition;
}

/**
Expand Down Expand Up @@ -276,7 +286,9 @@ private function processAnonymousServices(\DOMDocument $xml, $file)
// we could not use the constant false here, because of XML parsing
$domElement->setAttribute('public', 'false');

$this->parseDefinition($id, $domElement, $file);
if (null !== $definition = $this->parseDefinition($domElement, $file)) {
$this->container->setDefinition($id, $definition);
}

if (true === $wild) {
$tmpDomElement = new \DOMElement('_services', null, self::NS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@
<xsd:attribute name="function" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="configurator-service" mixed="true">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="service" type="service" />
</xsd:choice>
</xsd:complexType>

<xsd:complexType name="service">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="file" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="argument" type="argument" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="configurator" type="configurator" minOccurs="0" maxOccurs="1" />
<xsd:element name="configurator-service" type="configurator-service" minOccurs="0" maxOccurs="1" />
<xsd:element name="call" type="call" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="tag" type="tag" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,25 @@ public function provideDecoratedServicesData()
", include $fixturesPath.'/containers/container16.php'),
);
}

public function testDumpAnonymousConfiguratorServices()
{
include self::$fixturesPath.'/containers/container19.php';
$dumper = new XmlDumper($container);
$this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>
<container xmlns=\"http://symfony.com/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd\">
<services>
<service id=\"foo\" class=\"FooClass\">
<configurator-service>
<service class=\"BarClass\">
<configurator-service>
<service class=\"BazClass\"/>
</configurator-service>
</service>
</configurator-service>
</service>
</services>
</container>
", $dumper->dump());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

$container = new ContainerBuilder();

$bar = new Definition('BarClass');
$bar->setConfigurator(new Definition('BazClass'));

$container
->register('foo', 'FooClass')
->setConfigurator($bar)
;

return $container;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="foo" class="Foo">
<configurator-service>
Copy link
Member

Choose a reason for hiding this comment

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

couldn't it reuse the <configurator> tag instead of using a different one ?

<service class="Bar">
<configurator-service>
<service class="Baz"/>
</configurator-service>
</service>
</configurator-service>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,21 @@ public function testLoadIndexedArguments()

$this->assertEquals(array('index_0' => 'app'), $container->findDefinition('logger')->getArguments());
}

public function testLoadAnonymousConfiguratorServices()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services19.xml');

$bar = $container->getDefinition('foo')->getConfigurator();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $bar);
$this->assertSame('Bar', $bar->getClass());

$baz = $bar->getConfigurator();
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $baz);
$this->assertSame('Baz', $baz->getClass());

$this->assertNull($baz->getConfigurator());
}
}