diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 759bd45029835..cf29685c605f5 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -59,7 +59,7 @@ * * @api */ -class Container implements ContainerInterface +class Container implements IntrospectableContainerInterface { protected $parameterBag; protected $services; @@ -265,6 +265,18 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE throw new ServiceNotFoundException($id); } } + + /** + * Returns true if the given service has actually been initialized + * + * @param string $id The service identifier + * + * @return Boolean true if service has already been initialized, false otherwise + */ + public function initialized($id) + { + return isset($this->services[strtolower($id)]); + } /** * Gets all service ids. diff --git a/src/Symfony/Component/DependencyInjection/IntrospectableContainerInterface.php b/src/Symfony/Component/DependencyInjection/IntrospectableContainerInterface.php new file mode 100644 index 0000000000000..726652c05e503 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/IntrospectableContainerInterface.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection; + +/** + * IntrospectableContainerInterface defines additional introspection functionality + * for containers, allowing logic to be implemented based on a Container's state. + * + * @author Evan Villemez + * + */ +interface IntrospectableContainerInterface extends ContainerInterface +{ + /** + * Check for whether or not a service has been initialized. + * + * @param string $id + * + * @return Boolean true if the service has been initialized, false otherwise + * + */ + function initialized($id); + +} \ No newline at end of file diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 9d38be2450419..1be51061446bd 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -193,6 +193,18 @@ public function testHas() $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined'); } + /** + * @covers Symfony\Component\DependencyInjection\Container::initialized + */ + public function testInitialized() + { + $sc = new ProjectServiceContainer(); + $sc->set('foo', new \stdClass()); + $this->assertTrue($sc->initialized('foo'), '->initialized() returns true if service is loaded'); + $this->assertFalse($sc->initialized('foo1'), '->initialized() returns false if service is not loaded'); + $this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded'); + } + public function testEnterLeaveCurrentScope() { $container = new ProjectServiceContainer();