1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EightPoints\Bundle\GuzzleBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use EightPoints\Bundle\GuzzleBundle\Log\Logger; |
6
|
|
|
use EightPoints\Bundle\GuzzleBundle\Twig\Extension\DebugExtension; |
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\PluginInterface[] */ |
19
|
|
|
protected $plugins; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \EightPoints\Bundle\GuzzleBundle\PluginInterface[] $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
|
|
|
foreach ($config['clients'] as $name => $options) { |
68
|
|
|
$options['logging'] = $logging ? ($options['logging'] ?? true) : false; |
69
|
|
|
|
70
|
|
|
$argument = [ |
71
|
|
|
'base_uri' => $options['base_url'], |
72
|
|
|
'handler' => $this->createHandler($container, $name, $options, $profiling) |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
// if present, add default options to the constructor argument for the Guzzle client |
76
|
|
|
if (isset($options['options']) && is_array($options['options'])) { |
77
|
|
|
foreach ($options['options'] as $key => $value) { |
78
|
|
|
if ($value === null || (is_array($value) && count($value) === 0)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$argument[$key] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$client = new Definition($options['class']); |
87
|
|
|
$client->addArgument($argument); |
88
|
|
|
$client->setPublic(true); |
89
|
|
|
$client->setLazy($options['lazy']); |
90
|
|
|
|
91
|
|
|
// set service name based on client name |
92
|
|
|
$serviceName = sprintf('%s.client.%s', $this->getAlias(), $name); |
93
|
|
|
$container->setDefinition($serviceName, $client); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$clientsWithLogging = array_filter($config['clients'], function ($options) use ($logging) { |
97
|
|
|
return $options['logging'] !== false && $logging !== false; |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
if (count($clientsWithLogging)>0) { |
101
|
|
|
$this->defineTwigDebugExtension($container); |
102
|
|
|
$this->defineDataCollector($container, $config['slow_response_time'] / 1000); |
103
|
|
|
$this->defineFormatter($container); |
104
|
|
|
$this->defineSymfonyLogFormatter($container); |
105
|
|
|
$this->defineSymfonyLogMiddleware($container); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
111
|
|
|
* @param string $clientName |
112
|
|
|
* @param array $options |
113
|
|
|
* @param bool $profiling |
114
|
|
|
* |
115
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
116
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
117
|
|
|
* |
118
|
|
|
* @return \Symfony\Component\DependencyInjection\Definition |
119
|
|
|
*/ |
120
|
|
|
protected function createHandler(ContainerBuilder $container, string $clientName, array $options, bool $profiling) : Definition |
121
|
|
|
{ |
122
|
|
|
// Event Dispatching service |
123
|
|
|
$eventServiceName = sprintf('eight_points_guzzle.middleware.event_dispatch.%s', $clientName); |
124
|
|
|
$eventService = $this->createEventMiddleware($clientName); |
125
|
|
|
$container->setDefinition($eventServiceName, $eventService); |
126
|
|
|
|
127
|
|
|
// Create the event Dispatch Middleware |
128
|
|
|
$eventExpression = new Expression(sprintf("service('%s').dispatchEvent()", $eventServiceName)); |
129
|
|
|
|
130
|
|
|
$handler = new Definition(HandlerStack::class); |
131
|
|
|
$handler->setFactory([HandlerStack::class, 'create']); |
132
|
|
|
if (isset($options['handler'])) { |
133
|
|
|
|
134
|
|
|
$handlerServiceName = sprintf('eight_points_guzzle.handler.%s', $clientName); |
135
|
|
|
$handlerService = new Definition($options['handler']); |
136
|
|
|
$container->setDefinition($handlerServiceName, $handlerService); |
137
|
|
|
|
138
|
|
|
$handler->addArgument(new Reference($handlerServiceName)); |
139
|
|
|
} |
140
|
|
|
$handler->setPublic(true); |
141
|
|
|
$handler->setLazy($options['lazy']); |
142
|
|
|
|
143
|
|
|
$handlerStackServiceName = sprintf('eight_points_guzzle.handler_stack.%s', $clientName); |
144
|
|
|
$container->setDefinition($handlerStackServiceName, $handler); |
145
|
|
|
|
146
|
|
|
if ($profiling) { |
147
|
|
|
$this->defineProfileMiddleware($container, $handler, $clientName); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ($this->plugins as $plugin) { |
151
|
|
|
if (isset($options['plugin'][$plugin->getPluginName()])) { |
152
|
|
|
$plugin->loadForClient($options['plugin'][$plugin->getPluginName()], $container, $clientName, $handler); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$logMode = $this->convertLogMode($options['logging']); |
157
|
|
|
if ($logMode > Logger::LOG_MODE_NONE) { |
158
|
|
|
$loggerName = $this->defineLogger($container, $logMode, $clientName); |
159
|
|
|
$this->defineLogMiddleware($container, $handler, $clientName, $loggerName); |
160
|
|
|
$this->defineRequestTimeMiddleware($container, $handler, $clientName, $loggerName); |
161
|
|
|
$this->attachSymfonyLogMiddlewareToHandler($handler); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// goes on the end of the stack. |
165
|
|
|
$handler->addMethodCall('unshift', [$eventExpression, 'events']); |
166
|
|
|
|
167
|
|
|
return $handler; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int|bool $logMode |
172
|
|
|
* @return int |
173
|
|
|
*/ |
174
|
|
|
private function convertLogMode($logMode) : int |
175
|
|
|
{ |
176
|
|
|
if ($logMode === true){ |
177
|
|
|
return Logger::LOG_MODE_REQUEST_AND_RESPONSE; |
178
|
|
|
} elseif ($logMode === false) { |
179
|
|
|
return Logger::LOG_MODE_NONE; |
180
|
|
|
} else { |
181
|
|
|
return $logMode; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param ContainerBuilder $container |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
protected function defineTwigDebugExtension(ContainerBuilder $container) : void |
191
|
|
|
{ |
192
|
|
|
$twigDebugExtensionDefinition = new Definition(DebugExtension::class); |
193
|
|
|
$twigDebugExtensionDefinition->addTag('twig.extension'); |
194
|
|
|
$twigDebugExtensionDefinition->setPublic(false); |
195
|
|
|
$container->setDefinition('eight_points_guzzle.twig_extension.debug', $twigDebugExtensionDefinition); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Define Logger |
200
|
|
|
* |
201
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
202
|
|
|
* |
203
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
protected function defineLogger(ContainerBuilder $container, int $logMode, string $clientName) : string |
208
|
|
|
{ |
209
|
|
|
$loggerDefinition = new Definition('%eight_points_guzzle.logger.class%'); |
210
|
|
|
$loggerDefinition->setPublic(false); |
211
|
|
|
$loggerDefinition->setArgument(0, $logMode); |
212
|
|
|
$loggerDefinition->addTag('eight_points_guzzle.logger'); |
213
|
|
|
|
214
|
|
|
$loggerName = sprintf('eight_points_guzzle.%s_logger', $clientName); |
215
|
|
|
$container->setDefinition($loggerName, $loggerDefinition); |
216
|
|
|
|
217
|
|
|
return $loggerName; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Define Data Collector |
222
|
|
|
* |
223
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
224
|
|
|
* @param float $slowResponseTime |
225
|
|
|
* |
226
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
protected function defineDataCollector(ContainerBuilder $container, float $slowResponseTime) : void |
231
|
|
|
{ |
232
|
|
|
$dataCollectorDefinition = new Definition('%eight_points_guzzle.data_collector.class%'); |
233
|
|
|
$dataCollectorDefinition->addArgument(array_map(function ($loggerId) : Reference { |
234
|
|
|
return new Reference($loggerId); |
235
|
|
|
}, array_keys($container->findTaggedServiceIds('eight_points_guzzle.logger')))); |
236
|
|
|
|
237
|
|
|
$dataCollectorDefinition->addArgument($slowResponseTime); |
238
|
|
|
$dataCollectorDefinition->setPublic(false); |
239
|
|
|
$dataCollectorDefinition->addTag('data_collector', [ |
240
|
|
|
'id' => 'eight_points_guzzle', |
241
|
|
|
'template' => '@EightPointsGuzzle/debug.html.twig', |
242
|
|
|
]); |
243
|
|
|
$container->setDefinition('eight_points_guzzle.data_collector', $dataCollectorDefinition); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Define Formatter |
248
|
|
|
* |
249
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
250
|
|
|
* |
251
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
252
|
|
|
* |
253
|
|
|
* @return void |
254
|
|
|
*/ |
255
|
|
|
protected function defineFormatter(ContainerBuilder $container) : void |
256
|
|
|
{ |
257
|
|
|
$formatterDefinition = new Definition('%eight_points_guzzle.formatter.class%'); |
258
|
|
|
$formatterDefinition->setPublic(true); |
259
|
|
|
$container->setDefinition('eight_points_guzzle.formatter', $formatterDefinition); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Define Request Time Middleware |
264
|
|
|
* |
265
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
266
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
267
|
|
|
* @param string $clientName |
268
|
|
|
* |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
|
protected function defineRequestTimeMiddleware(ContainerBuilder $container, Definition $handler, string $clientName, string $loggerName) : void |
272
|
|
|
{ |
273
|
|
|
$requestTimeMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.request_time.%s', $clientName); |
274
|
|
|
$requestTimeMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.request_time.class%'); |
275
|
|
|
$requestTimeMiddlewareDefinition->addArgument(new Reference($loggerName)); |
276
|
|
|
$requestTimeMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.data_collector')); |
277
|
|
|
$requestTimeMiddlewareDefinition->setPublic(true); |
278
|
|
|
$container->setDefinition($requestTimeMiddlewareDefinitionName, $requestTimeMiddlewareDefinition); |
279
|
|
|
|
280
|
|
|
$requestTimeExpression = new Expression(sprintf("service('%s')", $requestTimeMiddlewareDefinitionName)); |
281
|
|
|
$handler->addMethodCall('after', ['log', $requestTimeExpression, 'request_time']); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Define Log Middleware for client |
286
|
|
|
* |
287
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
288
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
289
|
|
|
* @param string $clientName |
290
|
|
|
* |
291
|
|
|
* @return void |
292
|
|
|
*/ |
293
|
|
|
protected function defineLogMiddleware(ContainerBuilder $container, Definition $handler, string $clientName, string $loggerName) : void |
294
|
|
|
{ |
295
|
|
|
$logMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.log.%s', $clientName); |
296
|
|
|
$logMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.log.class%'); |
297
|
|
|
$logMiddlewareDefinition->addArgument(new Reference($loggerName)); |
298
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.formatter')); |
299
|
|
|
$logMiddlewareDefinition->setPublic(true); |
300
|
|
|
$container->setDefinition($logMiddlewareDefinitionName, $logMiddlewareDefinition); |
301
|
|
|
|
302
|
|
|
$logExpression = new Expression(sprintf("service('%s').log()", $logMiddlewareDefinitionName)); |
303
|
|
|
$handler->addMethodCall('push', [$logExpression, 'log']); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Define Profile Middleware for client |
308
|
|
|
* |
309
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
310
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
311
|
|
|
* @param string $clientName |
312
|
|
|
* |
313
|
|
|
* @return void |
314
|
|
|
*/ |
315
|
|
|
protected function defineProfileMiddleware(ContainerBuilder $container, Definition $handler, string $clientName) : void |
316
|
|
|
{ |
317
|
|
|
$profileMiddlewareDefinitionName = sprintf('eight_points_guzzle.middleware.profile.%s', $clientName); |
318
|
|
|
$profileMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.profile.class%'); |
319
|
|
|
$profileMiddlewareDefinition->addArgument(new Reference('debug.stopwatch')); |
320
|
|
|
$profileMiddlewareDefinition->setPublic(true); |
321
|
|
|
$container->setDefinition($profileMiddlewareDefinitionName, $profileMiddlewareDefinition); |
322
|
|
|
|
323
|
|
|
$profileExpression = new Expression(sprintf("service('%s').profile()", $profileMiddlewareDefinitionName)); |
324
|
|
|
$handler->addMethodCall('push', [$profileExpression, 'profile']); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @param \Symfony\Component\DependencyInjection\Definition $handler |
329
|
|
|
* |
330
|
|
|
* @return void |
331
|
|
|
*/ |
332
|
|
|
protected function attachSymfonyLogMiddlewareToHandler(Definition $handler) : void |
333
|
|
|
{ |
334
|
|
|
$logExpression = new Expression(sprintf("service('%s')", 'eight_points_guzzle.middleware.symfony_log')); |
335
|
|
|
$handler->addMethodCall('push', [$logExpression, 'symfony_log']); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Create Middleware For dispatching events |
340
|
|
|
* |
341
|
|
|
* @param string $name |
342
|
|
|
* |
343
|
|
|
* @return \Symfony\Component\DependencyInjection\Definition |
344
|
|
|
*/ |
345
|
|
|
protected function createEventMiddleware(string $name) : Definition |
346
|
|
|
{ |
347
|
|
|
$eventMiddleWare = new Definition('%eight_points_guzzle.middleware.event_dispatcher.class%'); |
348
|
|
|
$eventMiddleWare->addArgument(new Reference('event_dispatcher')); |
349
|
|
|
$eventMiddleWare->addArgument($name); |
350
|
|
|
$eventMiddleWare->setPublic(true); |
351
|
|
|
|
352
|
|
|
return $eventMiddleWare; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
357
|
|
|
* |
358
|
|
|
* @return void |
359
|
|
|
*/ |
360
|
|
|
protected function defineSymfonyLogFormatter(ContainerBuilder $container) : void |
361
|
|
|
{ |
362
|
|
|
$formatterDefinition = new Definition('%eight_points_guzzle.symfony_log_formatter.class%'); |
363
|
|
|
$formatterDefinition->setArguments(['%eight_points_guzzle.symfony_log_formatter.pattern%']); |
364
|
|
|
$formatterDefinition->setPublic(true); |
365
|
|
|
$container->setDefinition('eight_points_guzzle.symfony_log_formatter', $formatterDefinition); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
370
|
|
|
* |
371
|
|
|
* @return void |
372
|
|
|
*/ |
373
|
|
|
protected function defineSymfonyLogMiddleware(ContainerBuilder $container) : void |
374
|
|
|
{ |
375
|
|
|
$logMiddlewareDefinition = new Definition('%eight_points_guzzle.middleware.symfony_log.class%'); |
376
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('logger')); |
377
|
|
|
$logMiddlewareDefinition->addArgument(new Reference('eight_points_guzzle.symfony_log_formatter')); |
378
|
|
|
$logMiddlewareDefinition->setPublic(true); |
379
|
|
|
$logMiddlewareDefinition->addTag('monolog.logger', ['channel' => 'eight_points_guzzle']); |
380
|
|
|
$container->setDefinition('eight_points_guzzle.middleware.symfony_log', $logMiddlewareDefinition); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Returns alias of extension |
385
|
|
|
* |
386
|
|
|
* @return string |
387
|
|
|
*/ |
388
|
|
|
public function getAlias() : string |
389
|
|
|
{ |
390
|
|
|
return 'eight_points_guzzle'; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|