Skip to content

Commit

Permalink
Merge pull request csarrazi#92 from csarrazi/feature/middleware-priority
Browse files Browse the repository at this point in the history
Add support for setting a middleware's priority
  • Loading branch information
csarrazi committed Nov 24, 2015
2 parents 7849b63 + 19b0e00 commit 45af1d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/DependencyInjection/CompilerPass/MiddlewarePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/middleware.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<service id="csa_guzzle.middleware.history" class="Closure">
<factory class="Csa\Bundle\GuzzleBundle\GuzzleHttp\Middleware" method="history" />
<argument type="expression">service('csa_guzzle.data_collector.guzzle').getHistory()</argument>
<tag name="csa_guzzle.middleware" alias="history" />
<tag name="csa_guzzle.middleware" alias="history" priority="-1000" />
</service>

<service id="csa_guzzle.middleware.logger" class="Closure">
Expand Down
26 changes: 24 additions & 2 deletions src/Tests/DependencyInjection/CompilerPass/MiddlewarePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 45af1d0

Please sign in to comment.