Skip to content

[DI] Remove useless state from ServiceLocator #22029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Symfony/Component/DependencyInjection/ServiceLocator.php
Original file line number Diff line number Diff line change
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to cover this? The ServiceLocator is not passed to the factories so it is very unlikely to happen.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Mar 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we remove the safe guard? Symfony is used into the wild, it needs to handle properly any situations. I see no reason to trade a few lines vs a fatal error, however rare it is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, just asking, fine for me as is then.

}

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
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ public function testGet()
$this->assertSame('baz', $locator->get('bar'));
}

public function testGetDoesNotExecuteTheSameCallableTwice()
public function testGetDoesNotMemoize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memoize => Memorize

Copy link
Member Author

@nicolas-grekas nicolas-grekas Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
$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