Selaa lähdekoodia

[DependencyInjection] reverted 2ac6faaa0b723e84e29cf940610806bad047bd15

Fabien Potencier 15 vuotta sitten
vanhempi
commit
1683f46279

+ 4 - 4
src/Symfony/Components/DependencyInjection/Container.php

@@ -193,14 +193,14 @@ class Container implements ContainerInterface, \ArrayAccess
       throw new \InvalidArgumentException(sprintf('A service id should be a string (%s given).', str_replace("\n", '', var_export($id, true))));
     }
 
-    if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') && 'getService' !== $method)
+    if (isset($this->services[$id]))
     {
-      return $this->$method();
+      return $this->services[$id];
     }
 
-    if (isset($this->services[$id]))
+    if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') && 'getService' !== $method)
     {
-      return $this->services[$id];
+      return $this->$method();
     }
 
     if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior)

+ 1 - 1
tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php

@@ -126,7 +126,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method');
 
     $sc->setService('bar', $bar = new \stdClass());
-    $this->assertNotEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()');
+    $this->assertEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()');
 
     try
     {

+ 13 - 0
tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php

@@ -57,4 +57,17 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase
     {
     }
   }
+
+  public function testOverrideServiceWhenUsingADumpedContainer()
+  {
+    require_once self::$fixturesPath.'/php/services9.php';
+    require_once self::$fixturesPath.'/includes/foo.php';
+
+    $container = new \ProjectServiceContainer();
+    $container->setService('bar', $bar = new \stdClass());
+    $container->setParameter('foo_bar', 'foo_bar');
+
+    $this->assertEquals($bar, $container->getBarService(), '->setService() overrides an already defined service');
+    $this->assertEquals($bar, $container->getService('bar'), '->setService() overrides an already defined service');
+  }
 }