[DependencyInjection] Added, implemented and tested IntrospectableContainerInterface::initialized()

This commit is contained in:
Evan Villemez 2012-04-18 13:26:58 -04:00
parent 83a0edd24b
commit b7b26af9c5
3 changed files with 58 additions and 1 deletions

View File

@ -59,7 +59,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
*
* @api
*/
class Container implements ContainerInterface
class Container implements IntrospectableContainerInterface
{
protected $parameterBag;
protected $services;
@ -265,6 +265,18 @@ class Container implements ContainerInterface
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.

View File

@ -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 File

@ -193,6 +193,18 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$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();