[DI] Remove useless state from ServiceLocator
This commit is contained in:
parent
18bbf3741a
commit
e0a5eecf2a
@ -22,7 +22,6 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
|||||||
class ServiceLocator implements PsrContainerInterface
|
class ServiceLocator implements PsrContainerInterface
|
||||||
{
|
{
|
||||||
private $factories;
|
private $factories;
|
||||||
private $values = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable[] $factories
|
* @param callable[] $factories
|
||||||
@ -53,13 +52,12 @@ class ServiceLocator implements PsrContainerInterface
|
|||||||
throw new ServiceCircularReferenceException($id, array($id, $id));
|
throw new ServiceCircularReferenceException($id, array($id, $id));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false !== $factory) {
|
$this->factories[$id] = true;
|
||||||
$this->factories[$id] = true;
|
try {
|
||||||
$this->values[$id] = $factory();
|
return $factory();
|
||||||
$this->factories[$id] = false;
|
} finally {
|
||||||
|
$this->factories[$id] = $factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->values[$id];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __invoke($id)
|
public function __invoke($id)
|
||||||
|
@ -40,15 +40,20 @@ class ServiceLocatorTest extends TestCase
|
|||||||
$this->assertSame('baz', $locator->get('bar'));
|
$this->assertSame('baz', $locator->get('bar'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetDoesNotExecuteTheSameCallableTwice()
|
public function testGetDoesNotMemoize()
|
||||||
{
|
{
|
||||||
$i = 0;
|
$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('bar', $locator->get('foo'));
|
||||||
$this->assertSame('bar', $locator->get('foo'));
|
$this->assertSame(2, $i);
|
||||||
$this->assertSame(1, $i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user