minor #20196 [SecurityBundle] Cache contexts per request in FirewallMap (chalasr)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[SecurityBundle] Cache contexts per request in FirewallMap

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/19819#r77274093
| License       | MIT
| Doc PR        | n/a

From @HeahDude in https://github.com/symfony/symfony/pull/19819#r77274093, I propose to store and retrieve `Context` objects per `Request` using `SplObjectStorage`.

At the moment, contexts are consumed by the `Symfony\Components\Security\Http\Firewall` class only, but they could be indirectly by end users if #19490 and/or #19819 come to be merged.

Commits
-------

ffacec1 [SecurityBundle] Cache contexts per request in FirewallMap
This commit is contained in:
Fabien Potencier 2016-10-11 19:18:53 -07:00
commit bf2ff76d86

View File

@ -26,18 +26,24 @@ class FirewallMap implements FirewallMapInterface
{
protected $container;
protected $map;
private $contexts;
public function __construct(ContainerInterface $container, array $map)
{
$this->container = $container;
$this->map = $map;
$this->contexts = new \SplObjectStorage();
}
public function getListeners(Request $request)
{
if ($this->contexts->contains($request)) {
return $this->contexts[$request];
}
foreach ($this->map as $contextId => $requestMatcher) {
if (null === $requestMatcher || $requestMatcher->matches($request)) {
return $this->container->get($contextId)->getContext();
return $this->contexts[$request] = $this->container->get($contextId)->getContext();
}
}