8000 bug #19848 Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)… · symfony/symfony@0f6bc0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f6bc0b

Browse files
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 c89f80a, reversing changes made to 386e5e7. 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)"
2 parents 13d0510 + 8cb28bf commit 0f6bc0b

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public function has($id)
223223
if ('service_container' === $id
224224
|| isset($this->aliases[$id])
225225
|| isset($this->services[$id])
226+
|| array_key_exists($id, $this->services)
226< 8000 /td>227
) {
227228
return true;
228229
}
@@ -265,7 +266,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
265266
$id = $this->aliases[$id];
266267
}
267268
// Re-use shared service instance if it exists.
268-
if (isset($this->services[$id])) {
269+
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
269270
return $this->services[$id];
270271
}
271272

@@ -347,7 +348,7 @@ public function initialized($id)
347348
$id = $this->aliases[$id];
348349
}
349350

350-
return isset($this->services[$id]);
351+
return isset($this->services[$id]) || array_key_exists($id, 8000 $this->services);
351352
}
352353

353354
/**

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV
436436
return $service;
437437
}
438438

439-
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
439+
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
440440
return $this->get($this->aliasDefinitions[$id]);
441441
}
442442

@@ -784,7 +784,7 @@ public function setDefinition($id, Definition $definition)
784784
*/
785785
public function hasDefinition($id)
786786
{
787-
return isset($this->definitions[strtolower($id)]);
787+
return array_key_exists(strtolower($id), $this->definitions);
788788
}
789789

790790
/**
@@ -800,7 +800,7 @@ public function getDefinition($id)
800800
{
801801
$id = strtolower($id);
802802

803-
if (!isset($this->definitions[$id])) {
803+
if (!array_key_exists($id, $this->definitions)) {
804804
throw new ServiceNotFoundException($id);
805805
}
806806

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ public function testGetReturnsNullOnInactiveScope()
256256
$this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
257257
}
258258

259+
/**
260+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
261+
* @expectedExcepionMessage You have requested a synthetic service ("request"). The DIC does not know how to construct this service.
262+
*/
263+
public function testGetSyntheticServiceAlwaysThrows()
264+
{
265+
require_once __DIR__.'/Fixtures/php/services9.php';
266+
267+
$container = new \ProjectServiceContainer();
268+
$container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
269+
}
270+
259271
public function testHas()
260272
{
261273
$sc = new ProjectServiceContainer();
@@ -287,14 +299,17 @@ public function testEnterLeaveCurrentScope()
287299
$container->addScope(new Scope('foo'));
288300

289301
$container->enterScope('foo');
302+
$container->set('foo', new \stdClass(), 'foo');
290303
$scoped1 = $container->get('scoped');
291304
$scopedFoo1 = $container->get('scoped_foo');
292305

293306
$container->enterScope('foo');
307+
$container->set('foo', new \stdClass(), 'foo');
294308
$scoped2 = $container->get('scoped');
295309
$scoped3 = $container->get('SCOPED');
296310
$scopedFoo2 = $container->get('scoped_foo');
297311

312+
$container->set('foo', null, 'foo');
298313
$container->leaveScope('foo');
299314
$scoped4 = $container->get('scoped');
300315
$scopedFoo3 = $container->get('scoped_foo');
@@ -641,6 +656,12 @@ protected function getScopedSynchronizedFooService()
641656
return $this->services['scoped_bar'] = $this->scopedServices['foo']['scoped_bar'] = new \stdClass();
642657
}
643658

659+
protected function synchronizeFooService()
660+
{
661+
// Typically get the service to pass it to a setter
662+
$this->get('foo');
663+
}
664+
644665
protected function synchronizeScopedSynchronizedFooService()
645666
{
646667
$this->synchronized = true;

0 commit comments

Comments
 (0)
0