bug #19848 Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)" (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)"

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| Tests pass?   | yes
| Fixed tickets | #19689 #19840 #19825 #19857
| License       | MIT
| Doc PR        | -

This reverts commit c89f80a9cd, reversing
changes made to 386e5e78b4.

See discussion in #19847

I'll try adding test cases soon that ensure that:

- [x] *when not leaving scope* synthetic services always throw and ignore the `ContainerInterface::NULL_ON_INVALID_REFERENCE` flag (on 3.x also)
- [x] *when leaving scope* synthetic services always return null and ignore the `ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE`  (until 2.8 since scopes are gone in 3.x)

Commits
-------

8cb28bf [DI] Add anti-regression test
ac742df Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)"
This commit is contained in:
Nicolas Grekas 2016-09-06 12:51:08 +02:00
commit 0f6bc0b00a
3 changed files with 27 additions and 5 deletions

View File

@ -223,6 +223,7 @@ class Container implements IntrospectableContainerInterface
if ('service_container' === $id
|| isset($this->aliases[$id])
|| isset($this->services[$id])
|| array_key_exists($id, $this->services)
) {
return true;
}
@ -265,7 +266,7 @@ class Container implements IntrospectableContainerInterface
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id])) {
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
return $this->services[$id];
}
@ -347,7 +348,7 @@ class Container implements IntrospectableContainerInterface
$id = $this->aliases[$id];
}
return isset($this->services[$id]);
return isset($this->services[$id]) || array_key_exists($id, $this->services);
}
/**

View File

@ -436,7 +436,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
return $service;
}
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
return $this->get($this->aliasDefinitions[$id]);
}
@ -784,7 +784,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*/
public function hasDefinition($id)
{
return isset($this->definitions[strtolower($id)]);
return array_key_exists(strtolower($id), $this->definitions);
}
/**
@ -800,7 +800,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
{
$id = strtolower($id);
if (!isset($this->definitions[$id])) {
if (!array_key_exists($id, $this->definitions)) {
throw new ServiceNotFoundException($id);
}

View File

@ -256,6 +256,18 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExcepionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
*/
public function testGetSyntheticServiceAlwaysThrows()
{
require_once __DIR__.'/Fixtures/php/services9.php';
$container = new \ProjectServiceContainer();
$container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
}
public function testHas()
{
$sc = new ProjectServiceContainer();
@ -287,14 +299,17 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
$container->addScope(new Scope('foo'));
$container->enterScope('foo');
$container->set('foo', new \stdClass(), 'foo');
$scoped1 = $container->get('scoped');
$scopedFoo1 = $container->get('scoped_foo');
$container->enterScope('foo');
$container->set('foo', new \stdClass(), 'foo');
$scoped2 = $container->get('scoped');
$scoped3 = $container->get('SCOPED');
$scopedFoo2 = $container->get('scoped_foo');
$container->set('foo', null, 'foo');
$container->leaveScope('foo');
$scoped4 = $container->get('scoped');
$scopedFoo3 = $container->get('scoped_foo');
@ -641,6 +656,12 @@ class ProjectServiceContainer extends Container
return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass();
}
protected function synchronizeFooService()
{
// Typically get the service to pass it to a setter
$this->get('foo');
}
protected function synchronizeScopedSynchronizedFooService()
{
$this->synchronized = true;