Skip to content

Commit e0af6f7

Browse files
ludekstepanfabpot
authored andcommitted
Fix 'undefined index' error, when entering scope recursively
1 parent 2bb975b commit e0af6f7

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Container.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ public function enterScope($name)
334334
unset($this->scopedServices[$name]);
335335

336336
foreach ($this->scopeChildren[$name] as $child) {
337-
$services[$child] = $this->scopedServices[$child];
338-
unset($this->scopedServices[$child]);
337+
if (isset($this->scopedServices[$child])) {
338+
$services[$child] = $this->scopedServices[$child];
339+
unset($this->scopedServices[$child]);
340+
}
339341
}
340342

341343
// update global map

Tests/ContainerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,38 @@ public function testEnterLeaveScopeWithChildScopes()
261261
$this->assertFalse($container->has('a'));
262262
}
263263

264+
public function testEnterScopeRecursivelyWithInactiveChildScopes()
265+
{
266+
$container = new Container();
267+
$container->addScope(new Scope('foo'));
268+
$container->addScope(new Scope('bar', 'foo'));
269+
270+
$this->assertFalse($container->isScopeActive('foo'));
271+
272+
$container->enterScope('foo');
273+
274+
$this->assertTrue($container->isScopeActive('foo'));
275+
$this->assertFalse($container->isScopeActive('bar'));
276+
$this->assertFalse($container->has('a'));
277+
278+
$a = new \stdClass();
279+
$container->set('a', $a, 'foo');
280+
281+
$services = $this->getField($container, 'scopedServices');
282+
$this->assertTrue(isset($services['foo']['a']));
283+
$this->assertSame($a, $services['foo']['a']);
284+
285+
$this->assertTrue($container->has('a'));
286+
$container->enterScope('foo');
287+
288+
$services = $this->getField($container, 'scopedServices');
289+
$this->assertFalse(isset($services['a']));
290+
291+
$this->assertTrue($container->isScopeActive('foo'));
292+
$this->assertFalse($container->isScopeActive('bar'));
293+
$this->assertFalse($container->has('a'));
294+
}
295+
264296
public function testLeaveScopeNotActive()
265297
{
266298
$container = new Container();

0 commit comments

Comments
 (0)