diff --git a/core/core.services.yml b/core/core.services.yml index 777396f..1f6880b 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -7,7 +7,6 @@ parameters: default: keyvalue.database factory.keyvalue.expirable: default: keyvalue.expirable.database - authentication.global_providers: {} services: # Simple cache contexts, directly derived from the request context. cache_context.ip: @@ -1201,7 +1200,6 @@ services: - [setThemeManager, ['@theme.manager']] authentication: class: Drupal\Core\Authentication\AuthenticationManager - arguments: ['%authentication.global_providers%'] tags: - { name: service_collector, tag: authentication_provider, call: addProvider } authentication_subscriber: diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index ac00937..bb80c44 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -20,7 +20,7 @@ * * If no provider set an active user then the user is set to anonymous. */ -class AuthenticationManager implements AuthenticationProviderInterface, AuthenticationProviderFilterInterface, AuthenticationProviderChallengeInterface { +class AuthenticationManager implements AuthenticationProviderInterface, AuthenticationProviderFilterInterface, AuthenticationProviderChallengeInterface, AuthenticationManagerInterface { /** * Array of all registered authentication providers, keyed by ID. @@ -65,31 +65,7 @@ class AuthenticationManager implements AuthenticationProviderInterface, Authenti protected $globalProviders; /** - * Constructs an authentication manager. - * - * @todo Revisit service construction. Especially write a custom compiler pass - * which is capable of collecting, sorting and injecting all providers - * (including global/vs non global), filters and challengers on compile - * time in https://www.drupal.org/node/2432585. - * - * @param array $global_providers - * List of global providers, keyed by the provier ID. - */ - public function __construct($global_providers = []) { - $this->globalProviders = $global_providers; - } - - /** - * Adds a provider to the array of registered providers. - * - * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider - * The provider object. - * @param string $id - * Identifier of the provider. - * @param int $priority - * The providers priority. - * @param bool $global - * TRUE if the provider is to be applied globally on all routes. + * {@inheritdoc} */ public function addProvider(AuthenticationProviderInterface $provider, $id, $priority = 0, $global = FALSE) { // Remove the 'authentication.' prefix from the provider ID. diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php new file mode 100644 index 0000000..62785ac --- /dev/null +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php @@ -0,0 +1,29 @@ + $param) { if ($param->getClass()) { $interface = $param->getClass(); } - else if ($param->getName() == 'id') { + if ($param->getName() === 'id') { $id_pos = $pos; } - else if ($param->getName() == 'priority') { + else if ($param->getName() === 'priority') { $priority_pos = $pos; } else { - $other_params[$param->getName()] = $pos; + $extra_params[$param->getName()] = $pos; } } // Determine the ID. @@ -122,7 +122,7 @@ public function process(ContainerBuilder $container) { // Find all tagged handlers. $handlers = array(); - $other_arguments = array(); + $extra_arguments = array(); foreach ($container->findTaggedServiceIds($tag) as $id => $attributes) { // Validate the interface. $handler = $container->getDefinition($id); @@ -131,8 +131,8 @@ public function process(ContainerBuilder $container) { } $handlers[$id] = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; // Keep track of other tagged handlers arguments. - foreach ($other_params as $name => $pos) { - $other_arguments[$id][$pos] = isset($attributes[0][$name]) ? $attributes[0][$name] : NULL; + foreach ($extra_params as $name => $pos) { + $extra_arguments[$id][$pos] = isset($attributes[0][$name]) ? $attributes[0][$name] : NULL; } } if (empty($handlers)) { @@ -157,8 +157,8 @@ public function process(ContainerBuilder $container) { } // Sort the arguments by position. ksort($arguments); - if (isset($other_arguments[$id])) { - $arguments += $other_arguments[$id]; + if (isset($extra_arguments[$id])) { + $arguments += $extra_arguments[$id]; } $consumer->addMethodCall($method_name, $arguments); } diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/TaggedHandlersPassTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/TaggedHandlersPassTest.php index db6f39f..7d3d6a5 100644 --- a/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/TaggedHandlersPassTest.php +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/TaggedHandlersPassTest.php @@ -226,6 +226,97 @@ public function testProcessInterfaceMismatch() { $handler_pass->process($container); } + /** + * Tests consumer method with extra parameters. + * + * @covers ::process + */ + public function testProcessWithExtraArguments() { + $container = $this->buildContainer(); + + $container + ->register('consumer_id', __NAMESPACE__ . '\ValidConsumerWithExtraArguments') + ->addTag('service_collector'); + + $container + ->register('handler1', __NAMESPACE__ . '\ValidHandler') + ->addTag('consumer_id', array( + 'extra1' => 'extra1', + 'extra2' => 'extra2', + )); + + $handler_pass = new TaggedHandlersPass(); + $handler_pass->process($container); + + $method_calls = $container->getDefinition('consumer_id')->getMethodCalls(); + $this->assertCount(4, $method_calls[0][1]); + $this->assertEquals(new Reference('handler1'), $method_calls[0][1][0]); + $this->assertEquals(0, $method_calls[0][1][1]); + $this->assertEquals('extra1', $method_calls[0][1][2]); + $this->assertEquals('extra2', $method_calls[0][1][3]); + } + + /** + * Tests consumer method with extra parameters and no priority. + * + * @covers ::process + */ + public function testProcessNoPriorityAndExtraArguments() { + $container = $this->buildContainer(); + + $container + ->register('consumer_id', __NAMESPACE__ . '\ValidConsumerWithExtraArguments') + ->addTag('service_collector', array( + 'call' => 'addNoPriority' + )); + + $container + ->register('handler1', __NAMESPACE__ . '\ValidHandler') + ->addTag('consumer_id', array( + 'extra' => 'extra', + )); + + $handler_pass = new TaggedHandlersPass(); + $handler_pass->process($container); + + $method_calls = $container->getDefinition('consumer_id')->getMethodCalls(); + $this->assertCount(2, $method_calls[0][1]); + $this->assertEquals(new Reference('handler1'), $method_calls[0][1][0]); + $this->assertEquals('extra', $method_calls[0][1][1]); + } + + /** + * Tests consumer method with extra parameters and no priority. + * + * @covers ::process + */ + public function testProcessWithIdAndExtraArguments() { + $container = $this->buildContainer(); + + $container + ->register('consumer_id', __NAMESPACE__ . '\ValidConsumerWithExtraArguments') + ->addTag('service_collector', array( + 'call' => 'addWithId' + )); + + $container + ->register('handler1', __NAMESPACE__ . '\ValidHandler') + ->addTag('consumer_id', array( + 'extra1' => 'extra1', + )); + + $handler_pass = new TaggedHandlersPass(); + $handler_pass->process($container); + + $method_calls = $container->getDefinition('consumer_id')->getMethodCalls(); + $this->assertCount(5, $method_calls[0][1]); + $this->assertEquals(new Reference('handler1'), $method_calls[0][1][0]); + $this->assertEquals('handler1', $method_calls[0][1][1]); + $this->assertEquals(0, $method_calls[0][1][2]); + $this->assertEquals('extra1', $method_calls[0][1][3]); + $this->assertNull($method_calls[0][1][4]); + } + } interface HandlerInterface { @@ -242,6 +333,14 @@ class InvalidConsumer { public function addHandler($instance, $priority = 0) { } } +class ValidConsumerWithExtraArguments { + public function addHandler(HandlerInterface $instance, $priority = 0, $extra1, $extra2) { + } + public function addNoPriority(HandlerInterface $instance, $extra) { + } + public function addWithId(HandlerInterface $instance, $id, $priority = 0, $extra1, $extra2) { + } +} class ValidHandler implements HandlerInterface { } class InvalidHandler {