Skip to content

Commit

Permalink
minor #22029 [DI] Remove useless state from ServiceLocator (nicolas-g…
Browse files Browse the repository at this point in the history
…rekas)

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
  • Loading branch information
fabpot committed Mar 17, 2017
2 parents 6446639 + e0a5eec commit 4836007
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/Symfony/Component/DependencyInjection/ServiceLocator.php
Expand Up @@ -22,7 +22,6 @@
class ServiceLocator implements PsrContainerInterface
{
private $factories;
private $values = array();

/**
* @param callable[] $factories
Expand Down Expand Up @@ -53,13 +52,12 @@ public function get($id)
throw new ServiceCircularReferenceException($id, array($id, $id));
}

if (false !== $factory) {
$this->factories[$id] = true;
$this->values[$id] = $factory();
$this->factories[$id] = false;
$this->factories[$id] = true;
try {
return $factory();
} finally {
$this->factories[$id] = $factory;
}

return $this->values[$id];
}

public function __invoke($id)
Expand Down
Expand Up @@ -40,15 +40,20 @@ public function testGet()
$this->assertSame('baz', $locator->get('bar'));
}

public function testGetDoesNotExecuteTheSameCallableTwice()
public function testGetDoesNotMemoize()
{
$i = 0;
$locator = new ServiceLocator(array('foo' => function () use (&$i) { $i++; return 'bar'; }));
$locator = new ServiceLocator(array(
'foo' => function () use (&$i) {
++$i;

return 'bar';
},
));

$this->assertSame('bar', $locator->get('foo'));
$this->assertSame('bar', $locator->get('foo'));
$this->assertSame('bar', $locator->get('foo'));
$this->assertSame(1, $i);
$this->assertSame(2, $i);
}

/**
Expand Down

0 comments on commit 4836007

Please sign in to comment.