1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Caciobanu\Symfony\GuzzleBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
12
|
|
|
|
13
|
|
|
class CaciobanuGuzzleExtension extends Extension |
14
|
|
|
{ |
15
|
5 |
|
public function load(array $configs, ContainerBuilder $container): void |
16
|
|
|
{ |
17
|
5 |
|
$configs = $this->processConfiguration(new Configuration(), $configs); |
18
|
|
|
|
19
|
5 |
|
$loader = new XmlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config')); |
20
|
5 |
|
$loader->load('services.xml'); |
21
|
|
|
|
22
|
5 |
|
foreach ($configs['clients'] as $name => $config) { |
23
|
4 |
|
$handlerDefinition = $this->createHandler($container, $name, $config['logging']); |
24
|
|
|
|
25
|
|
|
$arguments = [ |
26
|
4 |
|
'base_uri' => $config['base_uri'], |
27
|
4 |
|
'handler' => $handlerDefinition, |
28
|
|
|
]; |
29
|
|
|
|
30
|
4 |
|
if (isset($config['options']) && \is_array($config['options'])) { |
31
|
4 |
|
foreach ($config['options'] as $key => $value) { |
32
|
1 |
|
$arguments[$key] = $value; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
$clientDefinition = new Definition($config['client_class'], [$arguments]); |
37
|
4 |
|
$container->setDefinition( |
38
|
4 |
|
sprintf('caciobanu_guzzle.client.%s', $name), |
39
|
4 |
|
$clientDefinition |
40
|
4 |
|
)->setPublic(true); |
41
|
|
|
} |
42
|
5 |
|
} |
43
|
|
|
|
44
|
4 |
|
private function createHandler(ContainerBuilder $container, string $clientName, bool $logging): Definition |
45
|
|
|
{ |
46
|
4 |
|
$handlerDefinition = new ChildDefinition('caciobanu_guzzle.handler_stack.abstract'); |
47
|
|
|
|
48
|
4 |
|
if ($logging) { |
49
|
1 |
|
$this->createLogger($handlerDefinition, $container, $clientName); |
50
|
1 |
|
$handlerDefinition->addTag('caciobanu_guzzle.loggable', ['client' => $clientName]); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
$container->setDefinition( |
54
|
4 |
|
sprintf('caciobanu_guzzle.handler.%s', $clientName), |
55
|
4 |
|
$handlerDefinition |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
|
59
|
4 |
|
return $handlerDefinition; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
private function createLogger(Definition $handler, ContainerBuilder $container, string $clientName): void |
|
|
|
|
63
|
|
|
{ |
64
|
1 |
|
$formatterDefinition = new ChildDefinition('caciobanu_guzzle.message_formatter.abstract'); |
65
|
1 |
|
$container->setDefinition( |
66
|
1 |
|
sprintf('caciobanu_guzzle.message_formatter.%s', $clientName), |
67
|
1 |
|
$formatterDefinition |
68
|
|
|
); |
69
|
|
|
|
70
|
1 |
|
$loggerDefinition = new ChildDefinition('caciobanu_guzzle.logger.abstract'); |
71
|
1 |
|
$loggerDefinition->replaceArgument( |
72
|
1 |
|
1, |
73
|
1 |
|
new Reference(sprintf('caciobanu_guzzle.message_formatter.%s', $clientName)) |
74
|
|
|
); |
75
|
|
|
|
76
|
1 |
|
$container->setDefinition( |
77
|
1 |
|
sprintf('caciobanu_guzzle.logger.%s', $clientName), |
78
|
1 |
|
$loggerDefinition |
79
|
|
|
); |
80
|
1 |
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.