|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\Twig\Extension\DebugExtension; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
11
|
|
|
use Symfony\Component\Config\FileLocator; |
|
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
13
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
14
|
|
|
use GuzzleHttp\HandlerStack; |
|
15
|
|
|
|
|
16
|
|
|
class EightPointsGuzzleExtension extends Extension |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundlePlugin[] */ |
|
19
|
|
|
protected $plugins; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundlePlugin[] $plugins |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(array $plugins = []) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->plugins = $plugins; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) : Configuration |
|
33
|
|
|
{ |
|
34
|
|
|
return new Configuration($this->getAlias(), $container->getParameter('kernel.debug'), $this->plugins); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Loads the Guzzle configuration. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $configs an array of configuration settings |
|
41
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container a ContainerBuilder instance |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \InvalidArgumentException |
|
44
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
|
45
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
51
|
|
|
{ |
|
52
|
|
|
$configPath = implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Resources', 'config']); |
|
53
|
|
|
$loader = new XmlFileLoader($container, new FileLocator($configPath)); |
|
54
|
|
|
|
|
55
|
|
|
$loader->load('services.xml'); |
|
56
|
|
|
|
|
57
|
|
|
$configuration = new Configuration($this->getAlias(), $container->getParameter('kernel.debug'), $this->plugins); |
|
58
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
59
|
|
|
$logging = $config['logging'] === true; |
|
60
|
|
|
$profiling = $config['profiling'] === true; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->plugins as $plugin) { |
|
63
|
|
|
$container->addObjectResource(new \ReflectionClass(get_class($plugin))); |
|
64
|
|
|
$plugin->load($config, $container); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($logging) { |
|
68
|
|
|
$this->defineTwigDebugExtension($container); |
|
69
|
|
|
$this->defineLogger($container); |
|
70
|
|
|
$this->defineDataCollector($container); |
|
71
|
|
|
$this->defineFormatter($container); |
|
72
|
|
|
$this->defineSymfonyLogFormatter($container); |
|
73
|
|
|
$this->defineSymfonyLogMiddleware($container); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
foreach ($config['clients'] as $name => $options) { |
|
77
|
|
|
$argument = [ |
|
78
|
|
|
'base_uri' => $options['base_url'], |
|
79
|
|
|
'handler' => $this->createHandler($container, $name, $options, $logging, $profiling) |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
// if present, add default options to the constructor argument for the Guzzle client |
|
83
|
|
|
if (isset($options['options']) && is_array($options['options'])) { |
|
84
|
|
|
foreach ($options['options'] as $key => $value) { |
|
85
|
|
|
if ($value === null || (is_array($value) && count($value) === 0)) { |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$argument[$key] = $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$client = new Definition($options['class']); |
|
94
|
|
|
$client->addArgument($argument); |
|
95
|
|
|
$client->setPublic(true); |
|
96
|
|
|
$client->setLazy($options['lazy']); |
|
97
|
|
|
|
|
98
|
|
|
// set service name based on client name |
|
99
|
|
|
$serviceName = sprintf('%s.client.%s', $this->getAlias(), $name); |
|
100
|
|
|
$container->setDefinition($serviceName, $client); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
106
|
|
|
* @param string $clientName |
|
107
|
|
|
* @param array $options |
|
108
|
|
|
* @param bool $logging |
|
109
|
|
|
* @param bool $profiling |
|
110
|
|
|
* |
|
111
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
|
112
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
113
|
|
|
* |
|
114
|
|
|
* @return \Symfony\Component\DependencyInjection\Definition |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function createHandler(ContainerBuilder $container, string $clientName, array $options, bool $logging, bool $profiling) : Definition |
|
117
|
|
|
{ |
|
118
|
|
|
// Event Dispatching service |
|
119
|
|
|
$eventServiceName = sprintf('eight_points_guzzle.middleware.event_dispatch.%s', $clientName); |
|
120
|
|
|
$eventService = $this->createEventMiddleware($clientName); |
|
121
|
|
|
$container->setDefinition($eventServiceName, $eventService); |
|
122
|
|
|
|
|
123
|
|
|
// Create the event Dispatch Middleware |
|
124
|
|
|
$eventExpression = new Expression(sprintf("service('%s').dispatchEvent()", $eventServiceName)); |
|
125
|
|
|
|
|
126
|
|
|
$handler = new Definition(HandlerStack::class); |
|
127
|
|
|
$handler->setFactory([HandlerStack::class, 'create']); |
|
128
|
|
|
$handler->setPublic(true); |
|
129
|
|
|
$handler->setLazy($options['lazy']); |
|
130
|
|
|
|
|
131
|
|
|
if ($logging) { |
|
132
|
|
|
$this->defineLogMiddleware($container, $handler, $clientName); |
|
133
|
|
|
$this->defineRequestTimeMiddleware($container, $handler, $clientName); |
|
134
|
|
|
$this->attachSymfonyLogMiddlewareToHandler($handler); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($profiling) { |
|
138
|
|
|
$this->defineProfileMiddleware($container, $handler, $clientName); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
foreach ($this->plugins as $plugin) { |
|
142
|
|
|
$plugin->loadForClient($options['plugin'][$plugin->getPluginName()], $container, $clientName, $handler); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// goes on the end of the stack. |
|
146
|
|
|
$handler->addMethodCall('unshift', [$eventExpression, 'events']); |
|
147
|
|
|
|
|
148
|
|
|
return $handler; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param ContainerBuilder $container |
|
153
|
|
|
* |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function defineTwigDebugExtension(ContainerBuilder $container) |
|
157
|
|
|
{ |
|
158
|
|
|
$twigDebugExtensionDefinition = new Definition(DebugExtension::class); |
|
159
|
|
|
$twigDebugExtensionDefinition->addTag('twig.extension'); |
|
160
|
|
|
$twigDebugExtensionDefinition->setPublic(false); |
|
161
|
|
|
$container->setDefinition('eight_points_guzzle.twig_extension.debug', $twigDebugExtensionDefinition); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Define Logger |
|
166
|
|
|
* |
|
167
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
168
|
|
|
* |
|
169
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
|
170
|
|
|
* |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function defineLogger(ContainerBuilder $container) |
|
174
|
|
|
{ |
|
175
|
|
|
$loggerDefinition = new Definition('%eight_points_guzzle.logger.class%'); |
|
176
|
|
|
$loggerDefinition->setPublic(true); |
|
177
|
|
|
$container->setDefinition('eight_points_guzzle.logger', $loggerDefinition); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Define Data Collector |
|
182
|
|
|
* |
|
183
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
184
|
|
|
* |
|
185
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
|
186
|
|
|
* |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function defineDataCollector(ContainerBuilder $container) |
|
190
|
|
|
{ |
|
191
|
|
|
$dataCollectorDefinition = new Definition('%eight_points_guzzle.data_collector.class%'); |
|
192
|
|
|
$dataCollectorDefinition->addArgument(new Reference('eight_points_guzzle.logger')); |
|
193
|
|
|
$dataCollectorDefinition->setPublic(false); |
|
194
|
|
|
$dataCollectorDefinition->addTag('data_collector', [ |
|
195
|
|
|
'id' => 'eight_points_guzzle', |
|
196
|
|
|
'template' => '@EightPointsGuzzle/debug.html.twig', |
|
197
|
|
|
]); |
|
198
|
|
|
$container->setDefinition('eight_points_guzzle.data_collector', $dataCollectorDefinition); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Define Formatter |
|
203
|
|
|
* |
|
204
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
205
|
|
|
* |
|
206
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
|
207
|
|
|
* |
|
208
|
|
|
* @return void |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function defineFormatter(ContainerBuilder $container) |
|
211
|
|
|
{ |
|
212
|
|
|
$formatterDefinition = new Definition('%eight_points_guzzle.formatter.class%'); |
|
213
|
|
|
$formatterDefinition->setPublic(true); |
|
214
|
|
|
$container->setDefinition('eight_points_guzzle.formatter', $formatterDefinition); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Define Request Time Middleware |
|
219
|
|
|
* |
|
220
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
221
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
|
222
|
|
|
* @param string $clientName |
|
223
|
|
|
* |
|
224
|
|
|
* @return void |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function defineRequestTimeMiddleware(ContainerBuilder $container, Definition $handler, string $clientName) |
|
227
|
|
|
{ |
|
228
|
|
|
$requestTimeMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.request_time.%s', $clientName); |
|
229
|
|
|
$requestTimeMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.request_time.class%'); |
|
230
|
|
|
$requestTimeMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.logger')); |
|
231
|
|
|
$requestTimeMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.data_collector')); |
|
232
|
|
|
$requestTimeMiddlewareDefinition->setPublic(true); |
|
233
|
|
|
$container->setDefinition($requestTimeMiddlewareDefinitionName, $requestTimeMiddlewareDefinition); |
|
234
|
|
|
|
|
235
|
|
|
$requestTimeExpression = new Expression(sprintf("service('%s')", $requestTimeMiddlewareDefinitionName)); |
|
236
|
|
|
$handler->addMethodCall('after', ['log', $requestTimeExpression, 'request_time']); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Define Log Middleware for client |
|
241
|
|
|
* |
|
242
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
243
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
|
244
|
|
|
* @param string $clientName |
|
245
|
|
|
* |
|
246
|
|
|
* @return void |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function defineLogMiddleware(ContainerBuilder $container, Definition $handler, string $clientName) |
|
249
|
|
|
{ |
|
250
|
|
|
$logMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.log.%s', $clientName); |
|
251
|
|
|
$logMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.log.class%'); |
|
252
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.logger')); |
|
253
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.formatter')); |
|
254
|
|
|
$logMiddlewareDefinition->setPublic(true); |
|
255
|
|
|
$container->setDefinition($logMiddlewareDefinitionName, $logMiddlewareDefinition); |
|
256
|
|
|
|
|
257
|
|
|
$logExpression = new Expression(sprintf("service('%s').log()", $logMiddlewareDefinitionName)); |
|
258
|
|
|
$handler->addMethodCall('push', [$logExpression, 'log']); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Define Profile Middleware for client |
|
263
|
|
|
* |
|
264
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
265
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
|
266
|
|
|
* @param string $clientName |
|
267
|
|
|
* |
|
268
|
|
|
* @return void |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function defineProfileMiddleware(ContainerBuilder $container, Definition $handler, string $clientName) |
|
271
|
|
|
{ |
|
272
|
|
|
if (!class_exists('Symfony\Component\Stopwatch\Stopwatch')) { |
|
273
|
|
|
throw new RuntimeException('Eightpoint GuzzleBundle profiling requires "symfony/stopwatch" package'); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$profileMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.profile.%s', $clientName); |
|
277
|
|
|
$profileMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.profile.class%'); |
|
278
|
|
|
$profileMiddlewareDefinition->addArgument(new Reference('debug.stopwatch')); |
|
279
|
|
|
$profileMiddlewareDefinition->setPublic(false); |
|
280
|
|
|
$container->setDefinition($profileMiddlewareDefinitionName, $profileMiddlewareDefinition); |
|
281
|
|
|
|
|
282
|
|
|
$profileExpression = new Expression(sprintf("service('%s').profile()", $profileMiddlewareDefinitionName)); |
|
283
|
|
|
$handler->addMethodCall('push', [$profileExpression, 'profile']); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
|
288
|
|
|
* |
|
289
|
|
|
* @return void |
|
290
|
|
|
*/ |
|
291
|
|
|
protected function attachSymfonyLogMiddlewareToHandler(Definition $handler) |
|
292
|
|
|
{ |
|
293
|
|
|
$logExpression = new Expression(sprintf("service('%s')", 'eight_points_guzzle.middleware.symfony_log')); |
|
294
|
|
|
$handler->addMethodCall('push', [$logExpression, 'symfony_log']); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Create Middleware For dispatching events |
|
299
|
|
|
* |
|
300
|
|
|
* @param string $name |
|
301
|
|
|
* |
|
302
|
|
|
* @return \Symfony\Component\DependencyInjection\Definition |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function createEventMiddleware(string $name) : Definition |
|
305
|
|
|
{ |
|
306
|
|
|
$eventMiddleWare = new Definition('%eight_points_guzzle.middleware.event_dispatcher.class%'); |
|
307
|
|
|
$eventMiddleWare->addArgument(new Reference('event_dispatcher')); |
|
308
|
|
|
$eventMiddleWare->addArgument($name); |
|
309
|
|
|
$eventMiddleWare->setPublic(true); |
|
310
|
|
|
|
|
311
|
|
|
return $eventMiddleWare; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
316
|
|
|
* |
|
317
|
|
|
* @return void |
|
318
|
|
|
*/ |
|
319
|
|
|
protected function defineSymfonyLogFormatter(ContainerBuilder $container) |
|
320
|
|
|
{ |
|
321
|
|
|
$formatterDefinition = new Definition('%eight_points_guzzle.symfony_log_formatter.class%'); |
|
322
|
|
|
$formatterDefinition->setArguments(['%eight_points_guzzle.symfony_log_formatter.pattern%']); |
|
323
|
|
|
$formatterDefinition->setPublic(true); |
|
324
|
|
|
$container->setDefinition('eight_points_guzzle.symfony_log_formatter', $formatterDefinition); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
|
329
|
|
|
* |
|
330
|
|
|
* @return void |
|
331
|
|
|
*/ |
|
332
|
|
|
protected function defineSymfonyLogMiddleware(ContainerBuilder $container) |
|
333
|
|
|
{ |
|
334
|
|
|
$logMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.symfony_log.class%'); |
|
335
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('logger')); |
|
336
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.symfony_log_formatter')); |
|
337
|
|
|
$logMiddlewareDefinition->setPublic(true); |
|
338
|
|
|
$logMiddlewareDefinition->addTag('monolog.logger', ['channel' => 'eight_points_guzzle']); |
|
339
|
|
|
$container->setDefinition('eight_points_guzzle.middleware.symfony_log', $logMiddlewareDefinition); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Returns alias of extension |
|
344
|
|
|
* |
|
345
|
|
|
* @return string |
|
346
|
|
|
*/ |
|
347
|
|
|
public function getAlias() : string |
|
348
|
|
|
{ |
|
349
|
|
|
return 'eight_points_guzzle'; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|