Skip to content

Commit

Permalink
Merge pull request #60 from M6Web/middlewares
Browse files Browse the repository at this point in the history
Enable adding middlewares
  • Loading branch information
SofLesc authored Apr 9, 2019
2 parents 77425c9 + a44c50e commit 569f105
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ m6web_guzzlehttp:

For the `headers` options, the key in array represent the header name. The underscore will be transformed to hyphen except if it's escaped by a backslash.

## Adding a middleware

Implement `M6Web\Bundle\GuzzleHttpBundle\Middleware\MiddlewareInterface`.

Tag your service with `m6web_guzzlehttp.middleware` and specify the client as follows:

```
Acme\Infra\GraphQL\Client\MyMiddleware:
tags:
- {name: 'm6web_guzzlehttp.middleware', client: 'myclient' }
```

## Contributing

First of all, thank you for contributing !
Expand Down
52 changes: 52 additions & 0 deletions src/DependencyInjection/MiddlewarePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace M6Web\Bundle\GuzzleHttpBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class MiddlewarePass
* @package M6Web\Bundle\GuzzleHttpBundle\DependencyInjection
*/
class MiddlewarePass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$services = $container->findTaggedServiceIds('m6web_guzzlehttp.middleware');
$middlewareArguments = [];

foreach ($services as $serviceId => $tags) {
foreach ($tags as $tagAttributes) {
$this->registerMiddleware($container, $serviceId, $tagAttributes['client'], $middlewareArguments);
}
}

foreach ($middlewareArguments as $clientId => $middlewares) {
$clientDefinition = $container->getDefinition('m6web_guzzlehttp_'.$clientId);
$clientDefinition->addArgument($middlewares);
}
}

/**
* @param ContainerBuilder $container
* @param string $serviceId
* @param string $clientId
* @param array $middlewareArguments
*/
private function registerMiddleware(ContainerBuilder $container, string $serviceId, string $clientId, array &$middlewareArguments)
{
$middlewareDefinition = $container->getDefinition($serviceId);
$middlewareDefinition->addMethodCall('push', [new Reference('m6web_guzzlehttp.guzzle.handlerstack.'.$clientId)]);

if (!isset($middlewareArguments[$clientId])) {
$middlewareArguments[$clientId] = [];
}

$middlewareArguments[$clientId][] = $middlewareDefinition;
}
}
11 changes: 11 additions & 0 deletions src/M6WebGuzzleHttpBundle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace M6Web\Bundle\GuzzleHttpBundle;

use M6Web\Bundle\GuzzleHttpBundle\DependencyInjection\MiddlewarePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -15,4 +17,13 @@ public function getContainerExtension()
{
return new DependencyInjection\M6WebGuzzleHttpExtension();
}

/**
* @param ContainerBuilder $container
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new MiddlewarePass());
}
}

0 comments on commit 569f105

Please sign in to comment.