8000 [DependencyInjection] Added, implemented and tested IntrospectableCon… · symfony/symfony@b7b26af · GitHub
[go: up one dir, main page]

Skip to content

Commit b7b26af

Browse files
author
Evan Villemez
committed
[DependencyInjection] Added, implemented and tested IntrospectableContainerInterface::initialized()
1 parent 83a0edd commit b7b26af

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
*
6060
* @api
6161
*/
62-
class Container implements ContainerInterface
62+
class Container implements IntrospectableContainerInterface
6363
{
6464
protected $parameterBag;
6565
protected $services;
@@ -265,6 +265,18 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
265265
throw new ServiceNotFoundException($id);
266266
}
267267
}
268+
269+
/**
270+
* Returns true if the given service has actually been initialized
271+
*
272+
* @param string $id The service identifier
273+
*
274+
* @return Boolean true if service has already been initialized, false otherwise
275+
*/
276+
public function initialized($id)
277+
{
278+
return isset($this->services[strtolower($id)]);
279+
}
268280

269281
/**
270282
* Gets all service ids.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection;
13+
14+
/**
15+
* IntrospectableContainerInterface defines additional introspection functionality
16+
* for containers, allowing logic to be implemented based on a Container's state.
17+
*
18+
* @author Evan Villemez <evillemez@gmail.com>
19+
*
20+
*/
21+
interface IntrospectableContainerInterface extends ContainerInterface
22+
{
23+
/**
24+
* Check for whether or not a service has been initialized.
25+
*
26+
* @param string $id
27+
*
28+
* @return Boolean true if the service has been initialized, false otherwise
29+
*
30+
*/
31+
function initialized($id);
32+
33+
}

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ public function testHas()
193193
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
194194
}
195195

196+
/**
197+
* @covers Symfony\Component\DependencyInjection\Container::initialized
198+
*/
199+
public function testInitialized()
200+
{
201+
$sc = new ProjectServiceContainer();
202+
$sc->set('foo', new \stdClass());
203+
$this->assertTrue($sc->initialized('foo'), '->initialized() returns true if service is loaded');
204+
$this->assertFalse($sc->initialized('foo1'), '->initialized() returns false if service is not loaded');
205+
$this->assertFalse($sc->initialized('bar'), '->initialized() returns false if a service is defined, but not currently loaded');
206+
}
207+
196208
public function testEnterLeaveCurrentScope()
197209
{
198210
$container = new ProjectServiceContainer();

0 commit comments

Comments
 (0)
0