Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e4e1ba

Browse files
committed
[Security][Guard] Lazy load authenticators
1 parent bcf8b68 commit 6e4e1ba

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/GuardAuthenticationFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1819

1920
/**
2021
* Configures the "guard" authentication provider key under a firewall.
@@ -62,11 +63,13 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
6263
$authenticatorReferences[] = new Reference($authenticatorId);
6364
}
6465

66+
$authenticators = new IteratorArgument($authenticatorReferences);
67+
6568
// configure the GuardAuthenticationFactory to have the dynamic constructor arguments
6669
$providerId = 'security.authentication.provider.guard.'.$id;
6770
$container
6871
->setDefinition($providerId, new ChildDefinition('security.authentication.provider.guard'))
69-
->replaceArgument(0, $authenticatorReferences)
72+
->replaceArgument(0, $authenticators)
7073
->replaceArgument(1, new Reference($userProvider))
7174
->replaceArgument(2, $id)
7275
->replaceArgument(3, new Reference('security.user_checker.'.$id))
@@ -76,7 +79,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
7679
$listenerId = 'security.authentication.listener.guard.'.$id;
7780
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.guard'));
7881
$listener->replaceArgument(2, $id);
79-
$listener->replaceArgument(3, $authenticatorReferences);
82+
$listener->replaceArgument(3, $authenticators);
8083

8184
// determine the entryPointId to use
8285
$entryPointId = $this->determineEntryPoint($defaultEntryPoint, $config);

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Security/Factory/GuardAuthenticationFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1819

1920
class GuardAuthenticationFactoryTest extends \PHPUnit_Framework_TestCase
2021
{
@@ -106,15 +107,15 @@ public function testBasicCreate()
106107

107108
$providerDefinition = $container->getDefinition('security.authentication.provider.guard.my_firewall');
108109
$this->assertEquals(array(
109-
'index_0' => array(new Reference('authenticator123')),
110+
'index_0' => new IteratorArgument(array(new Reference('authenticator123'))),
110111
'index_1' => new Reference('my_user_provider'),
111112
'index_2' => 'my_firewall',
112113
'index_3' => new Reference('security.user_checker.my_firewall'),
113114
), $providerDefinition->getArguments());
114115

115116
$listenerDefinition = $container->getDefinition('security.authentication.listener.guard.my_firewall');
116117
$this->assertEquals('my_firewall', $listenerDefinition->getArgument(2));
117-
$this->assertEquals(array(new Reference('authenticator123')), $listenerDefinition->getArgument(3));
118+
$this->assertEquals(array(new Reference('authenticator123')), $listenerDefinition->getArgument(3)->getValues());
118119
}
119120

120121
public function testExistingDefaultEntryPointUsed()

src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class GuardAuthenticationListener implements ListenerInterface
4545
* @param GuardAuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationProvider
4646
* @param LoggerInterface $logger A LoggerInterface instance
4747
*/
48-
public function __construct(GuardAuthenticatorHandler $guardHandler, AuthenticationManagerInterface $authenticationManager, $providerKey, array $guardAuthenticators, LoggerInterface $logger = null)
48+
public function __construct(GuardAuthenticatorHandler $guardHandler, AuthenticationManagerInterface $authenticationManager, $providerKey, $guardAuthenticators, LoggerInterface $logger = null)
4949
{
5050
if (empty($providerKey)) {
5151
throw new \InvalidArgumentException('$providerKey must not be empty.');
@@ -66,7 +66,12 @@ public function __construct(GuardAuthenticatorHandler $guardHandler, Authenticat
6666
public function handle(GetResponseEvent $event)
6767
{
6868
if (null !== $this->logger) {
69-
$this->logger->debug('Checking for guard authentication credentials.', array('firewall_key' => $this->providerKey, 'authenticators' => count($this->guardAuthenticators)));
69+
$context = array('firewall_key' => $this->providerKey);
70+
if (is_array($this->guardAuthenticators) || $this->guardAuthenticators instanceof \Countable) {
71+
$context['authenticators'] = count($this->guardAuthenticators);
72+
}
73+
74+
$this->logger->debug('Checking for guard authentication credentials.', $context);
7075
}
7176

7277
foreach ($this->guardAuthenticators as $key => $guardAuthenticator) {

src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class GuardAuthenticationProvider implements AuthenticationProviderInterface
4545
* @param string $providerKey The provider (i.e. firewall) key
4646
* @param UserCheckerInterface $userChecker
4747
*/
48-
public function __construct(array $guardAuthenticators, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker)
48+
public function __construct($guardAuthenticators, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker)
4949
{
5050
$this->guardAuthenticators = $guardAuthenticators;
5151
$this->userProvider = $userProvider;

0 commit comments

Comments
 (0)