Skip to content

Commit c0a7e1b

Browse files
committed
feature #9791 [DependencyInjection] added support for inlining Configurators (realityking)
This PR was merged into the 2.5-dev branch. Discussion ---------- [DependencyInjection] added support for inlining Configurators | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This is one commit from #9432. As mentioned in #3758 configurators can not be private (it's just ignored). This pull changes that and allows them to be inlined. It it also creates better code if a configurator is used multiple times for one service (i.e. to both inject it and configure the same service, or to configure multiple inlined services) but this should be very rare. Commits ------- 4e9aa07 [DependencyInjection] added support for inlining Configurators
2 parents 8e1f854 + 4e9aa07 commit c0a7e1b

File tree

8 files changed

+109
-7
lines changed

8 files changed

+109
-7
lines changed

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function process(ContainerBuilder $container)
6262
$definition->setProperties(
6363
$this->inlineArguments($container, $definition->getProperties())
6464
);
65+
66+
$configurator = $this->inlineArguments($container, array($definition->getConfigurator()));
67+
$definition->setConfigurator(
68+
$configurator[0]
69+
);
6570
}
6671
}
6772

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ private function addServiceLocalTempVariables($cId, $definition)
159159
$this->getServiceCallsFromArguments($iDefinition->getArguments(), $calls, $behavior);
160160
$this->getServiceCallsFromArguments($iDefinition->getMethodCalls(), $calls, $behavior);
161161
$this->getServiceCallsFromArguments($iDefinition->getProperties(), $calls, $behavior);
162+
$this->getServiceCallsFromArguments(array($iDefinition->getConfigurator()), $calls, $behavior);
162163
}
163164

164165
$code = '';
@@ -481,8 +482,9 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
481482
}
482483

483484
if (is_array($callable)) {
484-
if ($callable[0] instanceof Reference) {
485-
return sprintf(" %s->%s(\$%s);\n", $this->getServiceCall((string) $callable[0]), $callable[1], $variableName);
485+
if ($callable[0] instanceof Reference
486+
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
487+
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
486488
}
487489

488490
return sprintf(" call_user_func(array(%s, '%s'), \$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
@@ -1070,7 +1072,8 @@ private function getInlinedDefinitions(Definition $definition)
10701072
$definitions = array_merge(
10711073
$this->getDefinitionsFromArguments($definition->getArguments()),
10721074
$this->getDefinitionsFromArguments($definition->getMethodCalls()),
1073-
$this->getDefinitionsFromArguments($definition->getProperties())
1075+
$this->getDefinitionsFromArguments($definition->getProperties()),
1076+
$this->getDefinitionsFromArguments(array($definition->getConfigurator()))
10741077
);
10751078

10761079
$this->inlinedDefinitions->offsetSet($definition, $definitions);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,14 @@
8383
->register('depends_on_request', 'stdClass')
8484
->addMethodCall('setRequest', array(new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)))
8585
;
86+
$container
87+
->register('configurator_service', 'ConfClass')
88+
->setPublic(false)
89+
->addMethodCall('setFoo', array(new Reference('baz')))
90+
;
91+
$container
92+
->register('configured_service', 'stdClass')
93+
->setConfigurator(array(new Reference('configurator_service'), 'configureStdClass'))
94+
;
8695

8796
return $container;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/graphviz/services9.dot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ digraph sc {
1414
node_baz [label="baz\nBaz\n", shape=record, fillcolor="#eeeeee", style="filled"];
1515
node_request [label="request\nRequest\n", shape=record, fillcolor="#eeeeee", style="filled"];
1616
node_depends_on_request [label="depends_on_request\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
17+
node_configurator_service [label="configurator_service\nConfClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
18+
node_configured_service [label="configured_service\nstdClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
1719
node_service_container [label="service_container\nSymfony\\Component\\DependencyInjection\\ContainerBuilder\n", shape=record, fillcolor="#9999ff", style="filled"];
1820
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
1921
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
@@ -31,4 +33,5 @@ digraph sc {
3133
node_inlined -> node_baz [label="setBaz()" style="dashed"];
3234
node_baz -> node_foo_with_inline [label="setFoo()" style="dashed"];
3335
node_depends_on_request -> node_request [label="setRequest()" style="dashed"];
36+
node_configurator_service -> node_baz [label="setFoo()" style="dashed"];
3437
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public function __construct()
2525
$this->methodMap = array(
2626
'bar' => 'getBarService',
2727
'baz' => 'getBazService',
28+
'configurator_service' => 'getConfiguratorServiceService',
29+
'configured_service' => 'getConfiguredServiceService',
2830
'depends_on_request' => 'getDependsOnRequestService',
2931
'factory_service' => 'getFactoryServiceService',
3032
'foo' => 'getFooService',
@@ -51,9 +53,11 @@ public function __construct()
5153
*/
5254
protected function getBarService()
5355
{
54-
$this->services['bar'] = $instance = new \FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar'));
56+
$a = $this->get('foo.baz');
57+
58+
$this->services['bar'] = $instance = new \FooClass('foo', $a, $this->getParameter('foo_bar'));
5559

56-
$this->get('foo.baz')->configure($instance);
60+
$a->configure($instance);
5761

5862
return $instance;
5963
}
@@ -75,6 +79,23 @@ protected function getBazService()
7579
return $instance;
7680
}
7781

82+
/**
83+
* Gets the 'configured_service' service.
84+
*
85+
* This service is shared.
86+
* This method always returns the same instance of the service.
87+
*
88+
* @return stdClass A stdClass instance.
89+
*/
90+
protected function getConfiguredServiceService()
91+
{
92+
$this->services['configured_service'] = $instance = new \stdClass();
93+
94+
$this->get('configurator_service')->configureStdClass($instance);
95+
96+
return $instance;
97+
}
98+
7899
/**
79100
* Gets the 'depends_on_request' service.
80101
*
@@ -224,6 +245,27 @@ protected function synchronizeRequestService()
224245
}
225246
}
226247

248+
/**
249+
* Gets the 'configurator_service' service.
250+
*
251+
* This service is shared.
252+
* This method always returns the same instance of the service.
253+
*
254+
* This service is private.
255+
* If you want to be able to request this service from the container directly,
256+
* make it public, otherwise you might end up with broken code.
257+
*
258+
* @return ConfClass A ConfClass instance.
259+
*/
260+
protected function getConfiguratorServiceService()
261+
{
262+
$this->services['configurator_service'] = $instance = new \ConfClass();
263+
264+
$instance->setFoo($this->get('baz'));
265+
266+
return $instance;
267+
}
268+
227269
/**
228270
* Gets the 'inlined' service.
229271
*

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function __construct()
3434
$this->methodMap = array(
3535
'bar' => 'getBarService',
3636
'baz' => 'getBazService',
37+
'configured_service' => 'getConfiguredServiceService',
3738
'depends_on_request' => 'getDependsOnRequestService',
3839
'factory_service' => 'getFactoryServiceService',
3940
'foo' => 'getFooService',
@@ -59,9 +60,11 @@ public function __construct()
5960
*/
6061
protected function getBarService()
6162
{
62-
$this->services['bar'] = $instance = new \FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar'));
63+
$a = $this->get('foo.baz');
64+
65+
$this->services['bar'] = $instance = new \FooClass('foo', $a, $this->getParameter('foo_bar'));
6366

64-
$this->get('foo.baz')->configure($instance);
67+
$a->configure($instance);
6568

6669
return $instance;
6770
}
@@ -83,6 +86,26 @@ protected function getBazService()
8386
return $instance;
8487
}
8588

89+
/**
90+
* Gets the 'configured_service' service.
91+
*
92+
* This service is shared.
93+
* This method always returns the same instance of the service.
94+
*
95+
* @return stdClass A stdClass instance.
96+
*/
97+
protected function getConfiguredServiceService()
98+
{
99+
$a = new \ConfClass();
100+
$a->setFoo($this->get('baz'));
101+
102+
$this->services['configured_service'] = $instance = new \stdClass();
103+
104+
$a->configureStdClass($instance);
105+
106+
return $instance;
107+
}
108+
86109
/**
87110
* Gets the 'depends_on_request' service.
88111
*

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
<argument type="service" id="request" on-invalid="null" strict="false"/>
7777
</call>
7878
</service>
79+
<service id="configurator_service" class="ConfClass" public="false">
80+
<call method="setFoo">
81+
<argument type="service" id="baz"/>
82+
</call>
83+
</service>
84+
<service id="configured_service" class="stdClass">
85+
<configurator service="configurator_service" method="configureStdClass"/>
86+
</service>
7987
<service id="alias_for_foo" alias="foo"/>
8088
<service id="alias_for_alias" alias="foo"/>
8189
</services>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,14 @@ services:
7070
calls:
7171
- [setRequest, ['@?request']]
7272

73+
configurator_service:
74+
class: ConfClass
75+
public: false
76+
calls:
77+
- [setFoo, ['@baz']]
78+
79+
configured_service:
80+
class: stdClass
81+
configurator: ['@configurator_service', configureStdClass]
7382
alias_for_foo: @foo
7483
alias_for_alias: @foo

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy