10000 [2.3] Return BC compatibility for DI Container by stloyd · Pull Request #8392 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.3] Return BC compatibility for DI Container #8392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Symfony/Component/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ public function has($id)
{
$id = strtolower($id);

return array_key_exists($id, $this->services)
|| array_key_exists($id, $this->aliases)
return isset($this->services[$id])
|| isset($this->aliases[$id])
|| method_exists($this, 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')
;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
} catch (\Exception $e) {
unset($this->loading[$id]);

if (array_key_exists($id, $this->services)) {
if (isset($this->services[$id])) {
unset($this->services[$id]);
}

Expand All @@ -328,7 +328,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
*/
public function initialized($id)
{
return array_key_exists(strtolower($id), $this->services);
return isset($this->services[strtolower($id)]);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public function testGetThrowServiceNotFoundException()

public function testGetCircularReference()
{

$sc = new ProjectServiceContainer();
try {
$sc->get('circular');
Expand All @@ -210,7 +209,13 @@ public function testGetCircularReference()
public function testGetReturnsNullOnInactiveScope()
{
$sc = new ProjectServiceContainer();
$this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
$this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if service not exists and ContainerInterface::NULL_ON_INVALID_REFERENCE is used');

$sc->set('foo', $foo = new \stdClass());
$this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id if it exists');

$sc->set('foo', null);
$this->assertNull($sc->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if service is null and ContainerInterface::NULL_ON_INVALID_REFERENCE is used');
}

/**
Expand All @@ -220,11 +225,16 @@ public function testHas()
{
$sc = new ProjectServiceContainer();
$sc->set('foo', new \stdClass());
$sc->set('foo2', new \stdClass());
$this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
$this->assertTrue($sc->has('foo2'), '->has() returns true if the service exists');
$this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');

$sc->set('foo', null);
$this->assertFalse($sc->has('foo'), '->has() returns false if the service was set to null');
}

/**
Expand All @@ -237,6 +247,9 @@ public function testInitialized()
$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');

$sc->set('foo', null);
$this->assertFalse($sc->initialized('foo'), '->initialized() returns false if the service was set to null');
}

public function testEnterLeaveCurrentScope()
Expand Down
0