Skip to content

Commit b51d617

Browse files
committed
[Security] Lazy load request matchers
1 parent 2183f98 commit b51d617

File tree

7 files changed

+79
-13
lines changed

7 files changed

+79
-13
lines changed

UPGRADE-3.3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ SecurityBundle
5858
* The `FirewallContext::getContext()` method has been deprecated and will be removed in 4.0.
5959
Use the `getListeners()` method instead.
6060

61+
* The `FirewallContext::$map` property has been deprecated and will be removed in 4.0.
62+
6163
TwigBridge
6264
----------
6365

UPGRADE-4.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ SecurityBundle
163163
--------------
164164

165165
* The `FirewallContext::getContext()` method has been removed, use the `getListeners()` method instead.
166+
167+
* The `FirewallContext::$map` property has been removed.
166168

167169
HttpFoundation
168170
---------------

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* Deprecated the `FirewallContext::$map` property in favor of `FirewallMap::$contextMap`.
8+
49
3.2.0
510
-----
611

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
2222
use Symfony\Component\DependencyInjection\Reference;
23+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
2324
use Symfony\Component\Config\FileLocator;
2425
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2526

@@ -255,7 +256,7 @@ private function createFirewalls($config, ContainerBuilder $container)
255256

256257
$map[$contextId] = $matcher;
257258
}
258-
$mapDef->replaceArgument(1, $map);
259+
$mapDef->replaceArgument(1, new IteratorArgument($map));
259260

260261
// add authentication providers to authentication manager
261262
$authenticationProviders = array_map(function ($id) {

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
<service id="security.firewall.map" class="Symfony\Bundle\SecurityBundle\Security\FirewallMap" public="false">
107107
<argument type="service" id="service_container" />
108-
<argument type="collection" />
108+
<argument />
109109
</service>
110110

111111
<service id="security.firewall.context" class="Symfony\Bundle\SecurityBundle\Security\FirewallContext" abstract="true">

src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424
*/
2525
class FirewallMap implements FirewallMapInterface
2626
{
27+
/**
28+
* @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below
29+
*/
30+
private $map;
2731
protected $container;
28-
protected $map;
29-
private $contexts;
32+
private $contextCache;
33+
private $contextMap;
3034

31-
public function __construct(ContainerInterface $container, array $map)
35+
public function __construct(ContainerInterface $container, $contextMap)
3236
{
3337
$this->container = $container;
34-
$this->map = $map;
35-
$this->contexts = new \SplObjectStorage();
38+
$this->contextCache = new \SplObjectStorage();
39+
$this->contextMap = $contextMap;
3640
}
3741

3842
/**
@@ -65,14 +69,66 @@ public function getFirewallConfig(Request $request)
6569

6670
private function getFirewallContext(Request $request)
6771
{
68-
if ($this->contexts->contains($request)) {
69-
return $this->contexts[$request];
72+
if ($this->contextCache->contains($request)) {
73+
return $this->contextCache[$request];
7074
}
7175

72-
foreach ($this->map as $contextId => $requestMatcher) {
76+
foreach ($this->contextMap as $contextId => $requestMatcher) {
7377
if (null === $requestMatcher || $requestMatcher->matches($request)) {
74-
return $this->contexts[$request] = $this->container->get($contextId);
78+
return $this->contextCache[$request] = $this->container->get($contextId);
79+
}
80+
}
81+
}
82+
83+
/**
84+
* @internal
85+
*/
86+
public function __get($name)
87+
{
88+
if ('map' === $name) {
89+
@trigger_error(sprintf('Using the "%s::$map" property is deprecated since version 3.3 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
90+
91+
if ($this->contextMap instanceof \Iterator) {
92+
$this->map = iterator_to_array($this->contextMap);
7593
}
7694
}
95+
96+
return $this->$name;
97+
}
98+
99+
/**
100+
* @internal
101+
*/
102+
public function __set($name, $value)
103+
{
104+
if ('map' === $name) {
105+
@trigger_error(sprintf('Using the "%s::$map" property is deprecated since version 3.3 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
106+
}
107+
108+
$this->$name = $value;
109+
}
110+
111+
/**
112+
* @internal
113+
*/
114+
public function __isset($name)
115+
{
116+
if ('map' === $name) {
117+
@trigger_error(sprintf('Using the "%s::$map" property is deprecated since version 3.3 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
118+
}
119+
120+
return isset($this->$name);
121+
}
122+
123+
/**
124+
* @internal
125+
*/
126+
public function __unset($name)
127+
{
128+
if ('map' === $name) {
129+
@trigger_error(sprintf('Using the "%s::$map" property is deprecated since version 3.3 as it will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
130+
}
131+
132+
unset($this->$name);
77133
}
78134
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testFirewalls()
6868
$arguments = $container->getDefinition('security.firewall.map')->getArguments();
6969
$listeners = array();
7070
$configs = array();
71-
foreach (array_keys($arguments[1]) as $contextId) {
71+
foreach (array_keys($arguments[1]->getValues()) as $contextId) {
7272
$contextDef = $container->getDefinition($contextId);
7373
$arguments = $contextDef->getArguments();
7474
$listeners[] = array_map(function ($ref) { return (string) $ref; }, $arguments['index_0']);
@@ -180,7 +180,7 @@ public function testFirewallRequestMatchers()
180180
$arguments = $container->getDefinition('security.firewall.map')->getArguments();
181181
$matchers = array();
182182

183-
foreach ($arguments[1] as $reference) {
183+
foreach ($arguments[1]->getValues() as $reference) {
184184
if ($reference instanceof Reference) {
185185
$definition = $container->getDefinition((string) $reference);
186186
$matchers[] = $definition->getArguments();

0 commit comments

Comments
 (0)