From 5b72cf69505b08aa74ac7254109699b5a90a5604 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sun, 29 Jan 2017 12:38:33 +0100 Subject: [PATCH] [Security] Lazy load request matchers --- UPGRADE-3.3.md | 2 + UPGRADE-4.0.md | 2 + .../Bundle/SecurityBundle/CHANGELOG.md | 5 ++ .../DependencyInjection/SecurityExtension.php | 3 +- .../Resources/config/security.xml | 2 +- .../SecurityBundle/Security/FirewallMap.php | 89 ++++++++++++++++++- .../CompleteConfigurationTest.php | 4 +- 7 files changed, 99 insertions(+), 8 deletions(-) diff --git a/UPGRADE-3.3.md b/UPGRADE-3.3.md index 4e2478b71aa1..1269051076e0 100644 --- a/UPGRADE-3.3.md +++ b/UPGRADE-3.3.md @@ -58,6 +58,8 @@ SecurityBundle * The `FirewallContext::getContext()` method has been deprecated and will be removed in 4.0. Use the `getListeners()` method instead. + * The `FirewallMap::$map` and `$container` properties have been deprecated and will be removed in 4.0. + TwigBridge ---------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 8d98e342291d..1ff703c8e948 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -164,6 +164,8 @@ SecurityBundle * The `FirewallContext::getContext()` method has been removed, use the `getListeners()` method instead. + * The `FirewallMap::$map` and `$container` properties have been removed. + HttpFoundation --------------- diff --git a/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md b/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md index d83d4a62d1e5..661d2bb67732 100644 --- a/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.3.0 +----- + + * Deprecated the `FirewallMap::$map` and `$container` properties. + 3.2.0 ----- diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php index 51de15b2151b..d338a95fce57 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php @@ -15,6 +15,7 @@ use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\DependencyInjection\Alias; +use Symfony\Component\DependencyInjection\Argument\IteratorArgument; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -255,7 +256,7 @@ private function createFirewalls($config, ContainerBuilder $container) $map[$contextId] = $matcher; } - $mapDef->replaceArgument(1, $map); + $mapDef->replaceArgument(1, new IteratorArgument($map)); // add authentication providers to authentication manager $authenticationProviders = array_map(function ($id) { diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml index 54e573440299..6b8fb8df0681 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml @@ -105,7 +105,7 @@ - + diff --git a/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php b/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php index 9e166d07462f..019506d746d9 100644 --- a/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php +++ b/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php @@ -22,13 +22,94 @@ * * @author Johannes M. Schmitt */ -class FirewallMap implements FirewallMapInterface +class FirewallMap extends _FirewallMap implements FirewallMapInterface { - protected $container; - protected $map; + /** + * @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below + */ + private $container; + + /** + * @deprecated since version 3.3, to be removed in 4.0 alongside with magic methods below + */ + private $map; + + public function __construct(ContainerInterface $container, $map) + { + parent::__construct($container, $map); + $this->container = $container; + $this->map = $map; + } + + /** + * @internal + */ + public function __get($name) + { + if ('map' === $name || 'container' === $name) { + @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); + + if ('map' === $name && $this->map instanceof \Traversable) { + $this->map = iterator_to_array($this->map); + } + } + + return $this->$name; + } + + /** + * @internal + */ + public function __set($name, $value) + { + if ('map' === $name || 'container' === $name) { + @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); + + $set = \Closure::bind(function ($name, $value) { $this->$name = $value; }, $this, parent::class); + $set($name, $value); + } + + $this->$name = $value; + } + + /** + * @internal + */ + public function __isset($name) + { + if ('map' === $name || 'container' === $name) { + @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); + } + + return isset($this->$name); + } + + /** + * @internal + */ + public function __unset($name) + { + if ('map' === $name || 'container' === $name) { + @trigger_error(sprintf('Using the "%s::$%s" property is deprecated since version 3.3 as it will be removed/private in 4.0.', __CLASS__, $name), E_USER_DEPRECATED); + + $unset = \Closure::bind(function ($name) { unset($this->$name); }, $this, parent::class); + $unset($name); + } + + unset($this->$name); + } +} + +/** + * @internal to be removed in 4.0 + */ +class _FirewallMap +{ + private $container; + private $map; private $contexts; - public function __construct(ContainerInterface $container, array $map) + public function __construct(ContainerInterface $container, $map) { $this->container = $container; $this->map = $map; diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php index cbdb6e8f0f98..5e3f58ba61f7 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php @@ -68,7 +68,7 @@ public function testFirewalls() $arguments = $container->getDefinition('security.firewall.map')->getArguments(); $listeners = array(); $configs = array(); - foreach (array_keys($arguments[1]) as $contextId) { + foreach (array_keys($arguments[1]->getValues()) as $contextId) { $contextDef = $container->getDefinition($contextId); $arguments = $contextDef->getArguments(); $listeners[] = array_map(function ($ref) { return (string) $ref; }, $arguments['index_0']); @@ -180,7 +180,7 @@ public function testFirewallRequestMatchers() $arguments = $container->getDefinition('security.firewall.map')->getArguments(); $matchers = array(); - foreach ($arguments[1] as $reference) { + foreach ($arguments[1]->getValues() as $reference) { if ($reference instanceof Reference) { $definition = $container->getDefinition((string) $reference); $matchers[] = $definition->getArguments();