1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\DependencyInjection\Component; |
4
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\DependencyInjection\Compiler\MiddlewarePass; |
6
|
|
|
use Sludio\HelperBundle\Script\Utils\Helper; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
class Guzzle implements ConfigureInterface |
13
|
|
|
{ |
14
|
|
|
protected $alias; |
15
|
|
|
|
16
|
|
|
public function configure(ContainerBuilder $container, $alias) |
17
|
|
|
{ |
18
|
|
|
$this->alias = $alias.'.guzzle'; |
19
|
|
|
$dataCollector = $container->getDefinition($this->alias.'.data_collector.guzzle'); |
20
|
|
|
$dataCollector->replaceArgument(0, $container->getParameter($this->alias.'.profiler')['max_body_size']); |
21
|
|
|
|
22
|
|
|
if (!$container->getParameter($this->alias.'.profiler')['enabled']) { |
23
|
|
|
$container->removeDefinition($this->alias.'.middleware.history'); |
24
|
|
|
$container->removeDefinition($this->alias.'.middleware.stopwatch'); |
25
|
|
|
$container->removeDefinition($this->alias.'.data_collector.guzzle'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->processLoggerConfiguration($container->getParameter($this->alias.'.logger'), $container); |
29
|
|
|
$this->processMockConfiguration($container->getParameter($this->alias.'.mock'), $container, $container->getParameter($this->alias.'.profiler')['enabled']); |
30
|
|
|
$this->processCacheConfiguration($container->getParameter($this->alias.'.cache'), $container, $container->getParameter($this->alias.'.profiler')['enabled']); |
31
|
|
|
$this->processClientsConfiguration($container->getParameter($this->alias.'.clients'), $container, $container->getParameter($this->alias.'.profiler')['enabled']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function processLoggerConfiguration(array $config, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
|
|
if (!$config['enabled']) { |
37
|
|
|
$container->removeDefinition($this->alias.'.middleware.logger'); |
38
|
|
|
$container->removeDefinition($this->alias.'.logger.message_formatter'); |
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$loggerDefinition = $container->getDefinition($this->alias.'.middleware.logger'); |
44
|
|
|
|
45
|
|
|
if ($config['service']) { |
46
|
|
|
$loggerDefinition->replaceArgument(0, new Reference($config['service'])); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($config['format']) { |
50
|
|
|
$formatterDefinition = $container->getDefinition($this->alias.'.logger.message_formatter'); |
51
|
|
|
$formatterDefinition->replaceArgument(0, $config['format']); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($config['level']) { |
55
|
|
|
$loggerDefinition->replaceArgument(2, $config['level']); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function processMockConfiguration(array $config, ContainerBuilder $container, $debug) |
60
|
|
|
{ |
61
|
|
|
if (!$config['enabled']) { |
62
|
|
|
$container->removeDefinition($this->alias.'.middleware.mock'); |
63
|
|
|
$container->removeDefinition($this->alias.'.mock.storage'); |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$storage = $container->getDefinition($this->alias.'.mock.storage'); |
69
|
|
|
$storage->setArguments([ |
70
|
|
|
$config['storage_path'], |
71
|
|
|
$config['request_headers_blacklist'], |
72
|
|
|
$config['response_headers_blacklist'], |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$middleware = $container->getDefinition($this->alias.'.middleware.mock'); |
76
|
|
|
$middleware->replaceArgument(1, $config['mode']); |
77
|
|
|
$middleware->replaceArgument(2, $debug); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function processCacheConfiguration(array $config, ContainerBuilder $container, $debug) |
81
|
|
|
{ |
82
|
|
|
if (!$config['enabled'] || $config['disabled'] === true) { |
83
|
|
|
$container->removeDefinition($this->alias.'.middleware.cache'); |
84
|
|
|
$container->removeDefinition($this->alias.'.cache_adapter.redis'); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$container->getDefinition($this->alias.'.middleware.cache')->addArgument($debug); |
90
|
|
|
$container->getDefinition($this->alias.'.redis_cache')->replaceArgument(0, new Reference('snc_redis.'.$container->getParameter('sludio_helper.redis.guzzle'))); |
91
|
|
|
|
92
|
|
|
$container->setAlias($this->alias.'.cache_adapter', $config['adapter']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function processClientsConfiguration(array $config, ContainerBuilder $container, $debug) |
96
|
|
|
{ |
97
|
|
|
foreach ($config as $name => $options) { |
98
|
|
|
$client = new Definition($options['class']); |
99
|
|
|
$client->setLazy($options['lazy']); |
100
|
|
|
$this->parseAuthentication($options); |
101
|
|
|
|
102
|
|
|
if (isset($options['config'])) { |
103
|
|
|
if (!\is_array($options['config'])) { |
104
|
|
|
throw new InvalidArgumentException(sprintf('Config for "'.$this->alias.'.client.%s" should be an array, but got %s', $name, \gettype($options['config']))); |
105
|
|
|
} |
106
|
|
|
$client->addArgument($this->buildGuzzleConfig($options['config'], $debug)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$attributes = []; |
110
|
|
|
$this->parseMiddleware($attributes, $options, $debug); |
111
|
|
|
|
112
|
|
|
$client->addTag(MiddlewarePass::CLIENT_TAG, $attributes); |
113
|
|
|
|
114
|
|
|
$clientServiceId = sprintf($this->alias.'.client.%s', $name); |
115
|
|
|
$container->setDefinition($clientServiceId, $client); |
116
|
|
|
|
117
|
|
|
/** @var $options array */ |
118
|
|
|
if (isset($options['alias'])) { |
119
|
|
|
$container->setAlias($options['alias'], $clientServiceId); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function parseAuthentication(&$options) |
125
|
|
|
{ |
126
|
|
|
if ($options['credentials']['enabled'] === true) { |
127
|
|
|
if (!Helper::multiset(array_values($options['credentials']))) { |
128
|
|
|
throw new InvalidArgumentException(sprintf('If authentication parameter is set, htpasswd user and password can not be null')); |
129
|
|
|
} |
130
|
|
|
$credentials = [ |
131
|
|
|
'auth' => [ |
132
|
|
|
$options['credentials']['user'], |
133
|
|
|
$options['credentials']['pass'], |
134
|
|
|
$options['authentication_type'], |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
if (!isset($options['config'])) { |
139
|
|
|
$options['config'] = $credentials; |
140
|
|
|
} else { |
141
|
|
|
$options['config'] = array_merge($options['config'], $credentials); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function parseMiddleware(&$attributes, &$options, $debug) |
147
|
|
|
{ |
148
|
|
|
if (!empty($options['middleware'])) { |
149
|
|
|
if ($debug) { |
150
|
|
|
$addDebugMiddleware = true; |
151
|
|
|
|
152
|
|
|
/** @var $options array[] */ |
153
|
|
|
foreach ($options['middleware'] as $middleware) { |
154
|
|
|
if ('!' === $middleware[0]) { |
155
|
|
|
$addDebugMiddleware = false; |
156
|
|
|
break; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($addDebugMiddleware) { |
161
|
|
|
$middleware = [ |
162
|
|
|
'stopwatch', |
163
|
|
|
'history', |
164
|
|
|
'logger', |
165
|
|
|
]; |
166
|
|
|
$options['middleware'] = array_merge($options['middleware'], $middleware); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$attributes['middleware'] = implode(' ', array_unique($options['middleware'])); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function buildGuzzleConfig(array $config, $debug) |
175
|
|
|
{ |
176
|
|
|
if (isset($config['handler'])) { |
177
|
|
|
$config['handler'] = new Reference($config['handler']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($debug && \function_exists('curl_init')) { |
181
|
|
|
$config['on_stats'] = [ |
182
|
|
|
new Reference($this->alias.'.data_collector.history_bag'), |
183
|
|
|
'addStats', |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $config; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|