From 19b0e002709a53daa4182c9b119c504448a8a8c1 Mon Sep 17 00:00:00 2001 From: Charles Sarrazin Date: Tue, 24 Nov 2015 12:55:42 +0100 Subject: [PATCH] Added support for middleware priority --- .../CompilerPass/MiddlewarePass.php | 23 ++++++++++++---- src/Resources/config/middleware.xml | 2 +- .../CompilerPass/MiddlewarePassTest.php | 26 +++++++++++++++++-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/DependencyInjection/CompilerPass/MiddlewarePass.php b/src/DependencyInjection/CompilerPass/MiddlewarePass.php index 89cb3cda..ae6fa25c 100644 --- a/src/DependencyInjection/CompilerPass/MiddlewarePass.php +++ b/src/DependencyInjection/CompilerPass/MiddlewarePass.php @@ -54,10 +54,21 @@ private function findAvailableMiddleware(ContainerBuilder $container) throw new \LogicException('The \'alias\' attribute is mandatory for the \'csa_guzzle.middleware\' tag'); } - $middleware[$tags[0]['alias']] = new Reference($id); + if (!isset($tags[0]['priority'])) { + $services[$id][0]['priority'] = 0; + } + + $priority = (string) $services[$id][0]['priority']; + + $middleware[$priority][] = [ + 'alias' => $tags[0]['alias'], + 'id' => $id, + ]; } - return $middleware; + krsort($middleware); + + return call_user_func_array('array_merge', $middleware); } /** @@ -79,14 +90,16 @@ private function registerMiddleware(ContainerBuilder $container, array $middlewa if (isset($tags[0]['middleware'])) { $whitelist = explode(' ', $tags[0]['middleware']); - $clientMiddleware = array_intersect_key($clientMiddleware, array_flip($whitelist)); + $clientMiddleware = array_filter($clientMiddleware, function ($value) use ($whitelist) { + return in_array($value['alias'], $whitelist, true); + }); } $handlerStack = new DefinitionDecorator('csa_guzzle.handler_stack'); $handlerStack->setPublic(false); - foreach ($clientMiddleware as $alias => $middleware) { - $handlerStack->addMethodCall('push', [$middleware, $alias]); + foreach ($clientMiddleware as $middleware) { + $handlerStack->addMethodCall('push', [new Reference($middleware['id']), $middleware['alias']]); } $clientHandlerStackId = sprintf('csa_guzzle.handler_stack.%s', $clientId); diff --git a/src/Resources/config/middleware.xml b/src/Resources/config/middleware.xml index 7a1c671b..8519651d 100644 --- a/src/Resources/config/middleware.xml +++ b/src/Resources/config/middleware.xml @@ -19,7 +19,7 @@ service('csa_guzzle.data_collector.guzzle').getHistory() - + diff --git a/src/Tests/DependencyInjection/CompilerPass/MiddlewarePassTest.php b/src/Tests/DependencyInjection/CompilerPass/MiddlewarePassTest.php index 85ff802f..5309079f 100644 --- a/src/Tests/DependencyInjection/CompilerPass/MiddlewarePassTest.php +++ b/src/Tests/DependencyInjection/CompilerPass/MiddlewarePassTest.php @@ -56,10 +56,32 @@ public function testSpecificMiddlewareAddedToClient() $this->assertEquals(['push', [new Reference('bar'), 'bar']], $calls[1]); } - private function createMiddleware(ContainerBuilder $container, $alias) + public function testMiddlewareWithPriority() + { + $client = $this->createClient(); + + $container = $this->createContainer(); + $container->setDefinition('client', $client); + + foreach (['foo' => 0, 'bar' => 10, 'qux' => -1000] as $alias => $priority) { + $this->createMiddleware($container, $alias, $priority); + } + + $pass = new MiddlewarePass(); + $pass->process($container); + + $handler = $client->getArgument(0)['handler']; + $handlerDefinition = $container->getDefinition((string) $handler); + $this->assertCount(3, $calls = $handlerDefinition->getMethodCalls()); + $this->assertEquals(['push', [new Reference('bar'), 'bar']], $calls[0]); + $this->assertEquals(['push', [new Reference('foo'), 'foo']], $calls[1]); + $this->assertEquals(['push', [new Reference('qux'), 'qux']], $calls[2]); + } + + private function createMiddleware(ContainerBuilder $container, $alias, $priority = null) { $middleware = new Definition(); - $middleware->addTag(MiddlewarePass::MIDDLEWARE_TAG, ['alias' => $alias]); + $middleware->addTag(MiddlewarePass::MIDDLEWARE_TAG, ['alias' => $alias, 'priority' => $priority]); $container->setDefinition($alias, $middleware); }