8000 minor #22029 [DI] Remove useless state from ServiceLocator (nicolas-g… · symfony/symfony@4836007 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4836007

Browse files
committed
minor #22029 [DI] Remove useless state from ServiceLocator (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Remove useless state from ServiceLocator | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no (master only) | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - One less state to manage for the engine, and allows to deal with non-shared services. Commits ------- e0a5eec [DI] Remove useless state from ServiceLocator
2 parents 6446639 + e0a5eec commit 4836007

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Symfony/Component/DependencyInjection/ServiceLocator.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
class ServiceLocator implements PsrContainerInterface
2323
{
2424
private $factories;
25-
private $values = array();
2625

2726
/**
2827
* @param callable[] $factories
@@ -53,13 +52,12 @@ public function get($id)
5352
throw new ServiceCircularReferenceException($id, array($id, $id));
5453
}
5554

56-
if (false !== $factory) {
57-
$this->factories[$id] = true;
58-
$this->values[$id] = $factory();
59-
$this->factories[$id] = false;
55+
$this->factories[$id] = true;
56+
try {
57+
return $factory();
58+
} finally {
59+
$this->factories[$id] = $factory;
6060
}
61-
62-
return $this->values[$id];
6361
}
6462

6563
public function __invoke($id)

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@ public function testGet()
4040
$this->assertSame('baz', $locator->get('bar'));
4141
}
4242

43-
public function testGetDoesNotExecuteTheSameCallableTwice()
43+
public function testGetDoesNotMemoize()
4444
{
4545
$i = 0;
46-
$locator = new ServiceLocator(array('foo' => function () use (&$i) { $i++; return 'bar'; }));
46+
$locator = new ServiceLocator(array(
47+
'foo' => function () use (&$i) {
48+
++$i;
49+
50+
return 'bar';
51+
},
52+
));
4753

4854
$this->assertSame('bar', $locator->get('foo'));
4955
$this->assertSame('bar', $locator->get('foo'));
50-
$this->assertSame('bar', $locator->get('foo'));
51-
$this->assertSame(1, $i);
56+
$this->assertSame(2, $i);
5257
}
5358

5459
/**

0 commit comments

Comments
 (0)
0