Quellcode durchsuchen

[DependencyInjection] fixed Container::getService() when the service is empty (closes #8456)

Fabien Potencier vor 15 Jahren
Ursprung
Commit
c69410ccde

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

@@ -193,7 +193,7 @@ 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'))
+    if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service') && 'getService' !== $method)
     {
       return $this->$method();
     }

+ 14 - 0
tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php

@@ -176,6 +176,20 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     {
     }
   }
+
+  public function testGetService()
+  {
+    $sc = new Container();
+
+    try
+    {
+      $sc->getService('');
+      $this->fail('->getService() throws a \InvalidArgumentException exception if the service is empty');
+    }
+    catch (\InvalidArgumentException $e)
+    {
+    }
+  }
 }
 
 class ProjectServiceContainer extends Container