-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from M6Web/middlewares
Enable adding middlewares
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters