Permalink
Browse files

[DependencyInjection] Added, implemented and tested IntrospectableCon…

…tainerInterface::initialized()
  • Loading branch information...
1 parent 70c447d commit 9dd2993bbbe4fc679379248e0420635ecd50987e Evan Villemez committed Apr 18, 2012
Showing with 58 additions and 1 deletion.
  1. +13 −1 Container.php
  2. +33 −0 IntrospectableContainerInterface.php
  3. +12 −0 Tests/ContainerTest.php
View
@@ -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.
@@ -0,0 +1,33 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * 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 <evillemez@gmail.com>
+ *
+ */
+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);
+
+}
View
@@ -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();

0 comments on commit 9dd2993

Please sign in to comment.