[DependencyInjection] fixed PHP dumper

In the dumped PHP class, we must use get() and not get*Service() methods to get services.
That's because all calls must be managed by get(). From the outside, you can call
get*Service() because as they are protected, they are caught by the __call() method;
which is not the case obviously when it is used internally.

If not, if you override a service with set(), this won't work when a service
depends on this one (the default one will still be used).
This commit is contained in:
Fabien Potencier 2010-11-12 09:49:59 +01:00
parent 44ccd44dd6
commit efed6005cb
7 changed files with 28 additions and 19 deletions

View File

@ -433,10 +433,6 @@ EOF;
$id = $this->container->getAlias($id);
}
if ($this->container->hasDefinition($id)) {
return sprintf('$this->get%sService()', Container::camelize($id));
}
return sprintf('$this->get(\'%s\')', $id);
}
}

View File

@ -80,4 +80,16 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($bar, $container->getBarService(), '->set() overrides an already defined service');
$this->assertEquals($bar, $container->get('bar'), '->set() overrides an already defined service');
}
public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
{
require_once self::$fixturesPath.'/php/services9.php';
require_once self::$fixturesPath.'/includes/foo.php';
require_once self::$fixturesPath.'/includes/classes.php';
$container = new \ProjectServiceContainer();
$container->set('bar', $bar = new \stdClass());
$this->assertSame($bar, $container->getFooService()->bar, '->set() overrides an already defined service');
}
}

View File

@ -14,9 +14,8 @@ $container->
addTag('foo', array('bar' => 'bar'))->
setFactoryMethod('getInstance')->
setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
setFile(realpath(__DIR__.'/../includes/foo.php'))->
setShared(false)->
addMethodCall('setBar', array('bar'))->
addMethodCall('setBar', array(new Reference('bar')))->
addMethodCall('initialize')->
setConfigurator('sc_configure')
;
@ -41,6 +40,7 @@ $container->getParameterBag()->add(array(
$container->setAlias('alias_for_foo', 'foo');
$container->
register('method_call1', 'FooClass')->
setFile(realpath(__DIR__.'/../includes/foo.php'))->
addMethodCall('setBar', array(new Reference('foo')))->
addMethodCall('setBar', array(new Reference('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->
addMethodCall('setBar', array(new Reference('foo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->

View File

@ -13,6 +13,7 @@ digraph sc {
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foo -> node_foo_baz [label="" style="filled"];
node_foo -> node_service_container [label="" style="filled"];
node_foo -> node_bar [label="setBar()" style="dashed"];
node_bar -> node_foo_baz [label="" style="filled"];
node_method_call1 -> node_foo [label="setBar()" style="dashed"];
node_method_call1 -> node_foo [label="setBar()" style="dashed"];

View File

@ -32,10 +32,8 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
*/
protected function getFooService()
{
require_once '%path%foo.php';
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->getFoo_BazService(), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
$instance->setBar('bar');
$instance = call_user_func(array('FooClass', 'getInstance'), 'foo', $this->get('foo.baz'), array($this->getParameter('foo') => 'foo is '.$this->getParameter('foo'), 'bar' => $this->getParameter('foo')), true, $this);
$instance->setBar($this->get('bar'));
$instance->initialize();
sc_configure($instance);
@ -54,9 +52,9 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
{
if (isset($this->shared['bar'])) return $this->shared['bar'];
$instance = new FooClass('foo', $this->getFoo_BazService(), $this->getParameter('foo_bar'));
$instance = new FooClass('foo', $this->get('foo.baz'), $this->getParameter('foo_bar'));
$this->shared['bar'] = $instance;
$this->getFoo_BazService()->configure($instance);
$this->get('foo.baz')->configure($instance);
return $instance;
}
@ -109,11 +107,13 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
*/
protected function getMethodCall1Service()
{
require_once '%path%foo.php';
if (isset($this->shared['method_call1'])) return $this->shared['method_call1'];
$instance = new FooClass();
$this->shared['method_call1'] = $instance;
$instance->setBar($this->getFooService());
$instance->setBar($this->get('foo'));
$instance->setBar($this->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
if ($this->has('foo')) {
$instance->setBar($this->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
@ -137,7 +137,7 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
{
if (isset($this->shared['factory_service'])) return $this->shared['factory_service'];
$instance = $this->getFoo_BazService()->getInstance();
$instance = $this->get('foo.baz')->getInstance();
$this->shared['factory_service'] = $instance;
return $instance;
@ -150,7 +150,7 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
*/
protected function getAliasForFooService()
{
return $this->getFooService();
return $this->get('foo');
}
/**

View File

@ -12,7 +12,6 @@
<service id="foo" class="FooClass" factory-method="getInstance" shared="false">
<tag name="foo" foo="foo" />
<tag name="foo" bar="bar" />
<file>%path%foo.php</file>
<argument>foo</argument>
<argument type="service" id="foo.baz" />
<argument type="collection">
@ -22,7 +21,7 @@
<argument>true</argument>
<argument type="service" id="service_container" />
<call method="setBar">
<argument>bar</argument>
<argument type="service" id="bar" />
</call>
<call method="initialize" />
<configurator function="sc_configure" />
@ -39,6 +38,7 @@
<service id="foo_bar" class="%foo_class%">
</service>
<service id="method_call1" class="FooClass">
<file>%path%foo.php</file>
<call method="setBar">
<argument type="service" id="foo" />
</call>

View File

@ -9,11 +9,10 @@ services:
tags:
- { name: foo, foo: foo }
- { name: foo, bar: bar }
file: %path%foo.php
factory_method: getInstance
arguments: [foo, '@foo.baz', { '%foo%': 'foo is %foo%', bar: '%foo%' }, true, '@service_container']
calls:
- [setBar, [bar]]
- [setBar, ['@bar']]
- [initialize, { }]
shared: false
@ -30,6 +29,7 @@ services:
class: %foo_class%
method_call1:
class: FooClass
file: %path%foo.php
calls:
- [setBar, ['@foo']]
- [setBar, ['@@foo']]