1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Http Adapter bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapterBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Ivory\HttpAdapterBundle\DependencyInjection\Compiler\RegisterListenerCompilerPass; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
23
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author GeLo <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class IvoryHttpAdapterExtension extends ConfigurableExtension |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param string|null $suffix |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public static function createServiceName($suffix = null) |
36
|
|
|
{ |
37
|
|
|
return 'ivory.http_adapter'.($suffix !== null ? '.' : null).$suffix; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function loadInternal(array $config, ContainerBuilder $container) |
44
|
|
|
{ |
45
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
46
|
|
|
$loader->load('adapters.xml'); |
47
|
|
|
|
48
|
|
|
$definition = $container->getDefinition('ivory.http_adapter.abstract'); |
49
|
|
|
|
50
|
|
|
if (Kernel::VERSION_ID < 20600) { |
51
|
|
|
$definition |
|
|
|
|
52
|
|
|
->setFactoryClass('Ivory\HttpAdapter\HttpAdapterFactory') |
53
|
|
|
->setFactoryMethod('create'); |
54
|
|
|
} else { |
55
|
|
|
$definition->setFactory(['Ivory\HttpAdapter\HttpAdapterFactory', 'create']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($container->getParameter('kernel.debug')) { |
59
|
|
|
$loader->load('data_collector.xml'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->loadAdapters($config, $container, $loader); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $config |
67
|
|
|
* @param ContainerBuilder $container |
68
|
|
|
* @param LoaderInterface $loader |
69
|
|
|
*/ |
70
|
|
|
private function loadAdapters(array $config, ContainerBuilder $container, LoaderInterface $loader) |
71
|
|
|
{ |
72
|
|
|
foreach ($config['adapters'] as $name => $adapter) { |
73
|
|
|
$this->loadAdapter($name, $adapter, $config['configs'], $config['subscribers'], $container); |
74
|
|
|
$this->loadSubscribers($name, $adapter, $config['subscribers'], $container, $loader); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$container->setParameter(RegisterListenerCompilerPass::PARAMETER, array_keys($config['adapters'])); |
78
|
|
|
$container->setAlias(self::createServiceName(), self::createServiceName($config['default'])); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* @param array $adapter |
84
|
|
|
* @param array $configs |
85
|
|
|
* @param array $subscribers |
86
|
|
|
* @param ContainerBuilder $container |
87
|
|
|
*/ |
88
|
|
|
private function loadAdapter($name, array $adapter, array $configs, array $subscribers, ContainerBuilder $container) |
89
|
|
|
{ |
90
|
|
|
$httpAdapterName = self::createServiceName($name); |
91
|
|
|
$httpAdapter = $httpAdapterName.'.adapter'; |
92
|
|
|
$configuration = $httpAdapterName.'.configuration'; |
93
|
|
|
|
94
|
|
|
$container->setDefinition($configuration, $this->createConfigurationDefinition($adapter, $configs)); |
95
|
|
|
$container->setDefinition($httpAdapter, $this->createAdapterDefinition($adapter, $configuration)); |
96
|
|
|
|
97
|
|
|
if ($adapter['subscribers']['enabled'] || $subscribers['enabled'] || $container->getParameter('kernel.debug')) { |
98
|
|
|
$eventDispatcherHttpAdapter = $httpAdapterName.'.wrapper.event_dispatcher'; |
99
|
|
|
$eventDispatcher = $httpAdapterName.'.event_dispatcher'; |
100
|
|
|
|
101
|
|
|
$container->setDefinition( |
102
|
|
|
$eventDispatcher, |
103
|
|
|
new DefinitionDecorator(self::createServiceName('event_dispatcher')) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$container->setDefinition( |
107
|
|
|
$eventDispatcherHttpAdapter, |
108
|
|
|
new Definition( |
109
|
|
|
'Ivory\HttpAdapter\EventDispatcherHttpAdapter', |
110
|
|
|
[new Reference($httpAdapter), new Reference($eventDispatcher)] |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$httpAdapter = $eventDispatcherHttpAdapter; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($container->getParameter('kernel.debug')) { |
118
|
|
|
$stopwatchHttpAdapter = $httpAdapterName.'.wrapper.stopwatch'; |
119
|
|
|
|
120
|
|
|
$container->setDefinition( |
121
|
|
|
$stopwatchHttpAdapter, |
122
|
|
|
new Definition( |
123
|
|
|
'Ivory\HttpAdapter\StopwatchHttpAdapter', |
124
|
|
|
[new Reference($httpAdapter), new Reference('debug.stopwatch')] |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$httpAdapter = $stopwatchHttpAdapter; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$container->setAlias($httpAdapterName, $httpAdapter); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $name |
136
|
|
|
* @param array $adapter |
137
|
|
|
* @param array $subscribers |
138
|
|
|
* @param ContainerBuilder $container |
139
|
|
|
* @param LoaderInterface $loader |
140
|
|
|
*/ |
141
|
|
|
private function loadSubscribers( |
142
|
|
|
$name, |
143
|
|
|
array $adapter, |
144
|
|
|
array $subscribers, |
145
|
|
|
ContainerBuilder $container, |
146
|
|
|
LoaderInterface $loader |
147
|
|
|
) { |
148
|
|
|
if ($container->getParameter('kernel.debug')) { |
149
|
|
|
$subscribers['debug'] = null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
unset($adapter['subscribers']['enabled']); |
153
|
|
|
unset($subscribers['enabled']); |
154
|
|
|
|
155
|
|
|
foreach (array_merge($subscribers, $adapter['subscribers']) as $subscriberName => $subscriber) { |
156
|
|
|
$loader->load('subscribers/'.$subscriberName.'.xml'); |
157
|
|
|
|
158
|
|
|
$container->setDefinition( |
159
|
|
|
self::createServiceName($name.'.subscriber.'.$subscriberName), |
160
|
|
|
$this->createSubscriberDefinition($name, $subscriberName, $subscriber, $container) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param array $adapter |
167
|
|
|
* @param string $configuration |
168
|
|
|
* |
169
|
|
|
* @return DefinitionDecorator |
170
|
|
|
*/ |
171
|
|
|
private function createAdapterDefinition(array $adapter, $configuration) |
172
|
|
|
{ |
173
|
|
|
$definition = new DefinitionDecorator(self::createServiceName('abstract')); |
174
|
|
|
$definition->setArguments([$adapter['type']]); |
175
|
|
|
$definition->addMethodCall('setConfiguration', [new Reference($configuration)]); |
176
|
|
|
|
177
|
|
|
return $definition; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array $adapter |
182
|
|
|
* @param array $configs |
183
|
|
|
* |
184
|
|
|
* @return DefinitionDecorator |
185
|
|
|
*/ |
186
|
|
|
private function createConfigurationDefinition(array $adapter, array $configs) |
187
|
|
|
{ |
188
|
|
|
$definition = new DefinitionDecorator(self::createServiceName('configuration')); |
189
|
|
|
|
190
|
|
|
foreach (array_merge($configs, $adapter['configs']) as $property => $value) { |
191
|
|
|
$definition->addMethodCall($this->getMethod($property), [$value]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $definition; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $adapterName |
199
|
|
|
* @param string $subscriberName |
200
|
|
|
* @param array|string $configuration |
201
|
|
|
* @param ContainerBuilder $container |
202
|
|
|
* |
203
|
|
|
* @return DefinitionDecorator |
204
|
|
|
*/ |
205
|
|
|
private function createSubscriberDefinition( |
206
|
|
|
$adapterName, |
207
|
|
|
$subscriberName, |
208
|
|
|
$configuration, |
209
|
|
|
ContainerBuilder $container |
210
|
|
|
) { |
211
|
|
|
$parent = self::createServiceName('subscriber.'.$subscriberName); |
212
|
|
|
|
213
|
|
|
$subscriber = new DefinitionDecorator($parent); |
214
|
|
|
$subscriber->setClass($container->getDefinition($parent)->getClass()); |
215
|
|
|
$subscriber->addTag(RegisterListenerCompilerPass::SUBSCRIBER_TAG, ['adapter' => $adapterName]); |
216
|
|
|
|
217
|
|
|
switch ($subscriberName) { |
218
|
|
|
case 'basic_auth': |
219
|
|
|
$this->configureBasicAuthSubscriberDefinition($subscriber, $configuration, $adapterName, $container); |
|
|
|
|
220
|
|
|
break; |
221
|
|
|
|
222
|
|
|
case 'cache': |
223
|
|
|
$this->configureCacheSubscriberDefinition($subscriber, $configuration, $adapterName, $container); |
|
|
|
|
224
|
|
|
break; |
225
|
|
|
|
226
|
|
|
case 'cookie': |
227
|
|
|
$this->configureCookieSubscriberDefinition($subscriber, $configuration); |
|
|
|
|
228
|
|
|
break; |
229
|
|
|
|
230
|
|
|
case 'history': |
231
|
|
|
$this->configureHistorySubscriberDefinition($subscriber, $configuration); |
|
|
|
|
232
|
|
|
break; |
233
|
|
|
|
234
|
|
|
case 'logger': |
235
|
|
|
$this->configureLoggerSubscriberDefinition($subscriber, $configuration); |
|
|
|
|
236
|
|
|
break; |
237
|
|
|
|
238
|
|
|
case 'redirect': |
239
|
|
|
$this->configureRedirectSubscriberDefinition($subscriber, $configuration, $adapterName, $container); |
|
|
|
|
240
|
|
|
break; |
241
|
|
|
|
242
|
|
|
case 'retry': |
243
|
|
|
$this->configureRetrySubscriberDefinition($subscriber, $adapterName, $container); |
244
|
|
|
break; |
245
|
|
|
|
246
|
|
|
case 'stopwatch': |
247
|
|
|
$this->configureStopwatchSubscriberDefinition($subscriber, $configuration); |
|
|
|
|
248
|
|
|
break; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $subscriber; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param Definition $subscriber |
256
|
|
|
* @param array $basicAuth |
257
|
|
|
* @param string $adapterName |
258
|
|
|
* @param ContainerBuilder $container |
259
|
|
|
*/ |
260
|
|
|
private function configureBasicAuthSubscriberDefinition( |
261
|
|
|
Definition $subscriber, |
262
|
|
|
array $basicAuth, |
263
|
|
|
$adapterName, |
264
|
|
|
ContainerBuilder $container |
265
|
|
|
) { |
266
|
|
|
$model = new DefinitionDecorator(self::createServiceName('subscriber.basic_auth.model')); |
267
|
|
|
$model->setArguments([$basicAuth['username'], $basicAuth['password']]); |
268
|
|
|
|
269
|
|
|
if (isset($basicAuth['matcher'])) { |
270
|
|
|
$model->addArgument($basicAuth['matcher']); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$container->setDefinition($service = self::createServiceName($adapterName.'.basic_auth.model'), $model); |
274
|
|
|
$subscriber->setArguments([new Reference($service)]); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param Definition $subscriber |
279
|
|
|
* @param array $cache |
280
|
|
|
* @param string $adapterName |
281
|
|
|
* @param ContainerBuilder $container |
282
|
|
|
*/ |
283
|
|
|
private function configureCacheSubscriberDefinition( |
284
|
|
|
Definition $subscriber, |
285
|
|
|
array $cache, |
286
|
|
|
$adapterName, |
287
|
|
|
ContainerBuilder $container |
288
|
|
|
) { |
289
|
|
|
$model = new DefinitionDecorator(self::createServiceName('subscriber.cache.model')); |
290
|
|
|
$model->setArguments([new Reference($cache['adapter']), null, $cache['lifetime'], $cache['exception']]); |
291
|
|
|
|
292
|
|
|
$container->setDefinition($service = self::createServiceName($adapterName.'.cache.model'), $model); |
293
|
|
|
$subscriber->setArguments([new Reference($service)]); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param Definition $subscriber |
298
|
|
|
* @param string|null $cookieJar |
299
|
|
|
*/ |
300
|
|
|
private function configureCookieSubscriberDefinition(Definition $subscriber, $cookieJar = null) |
301
|
|
|
{ |
302
|
|
|
if ($cookieJar === null) { |
303
|
|
|
$cookieJar = 'default'; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
if (in_array($cookieJar, ['default', 'file', 'session'], true)) { |
307
|
|
|
$cookieJar = 'ivory.http_adapter.subscriber.cookie.jar.'.$cookieJar; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$subscriber->setArguments([new Reference($cookieJar)]); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param Definition $definition |
315
|
|
|
* @param string|null $journal |
316
|
|
|
*/ |
317
|
|
|
private function configureHistorySubscriberDefinition(Definition $definition, $journal = null) |
318
|
|
|
{ |
319
|
|
|
$definition->setArguments([new Reference($journal ?: 'ivory.http_adapter.subscriber.history.journal')]); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param Definition $subscriber |
324
|
|
|
* @param string|null $logger |
325
|
|
|
*/ |
326
|
|
|
private function configureLoggerSubscriberDefinition(Definition $subscriber, $logger = null) |
327
|
|
|
{ |
328
|
|
|
$subscriber->setArguments([new Reference($logger ?: 'logger')]); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param Definition $subscriber |
333
|
|
|
* @param array $redirect |
334
|
|
|
* @param string $adapterName |
335
|
|
|
* @param ContainerBuilder $container |
336
|
|
|
*/ |
337
|
|
|
private function configureRedirectSubscriberDefinition( |
338
|
|
|
Definition $subscriber, |
339
|
|
|
array $redirect, |
340
|
|
|
$adapterName, |
341
|
|
|
ContainerBuilder $container |
342
|
|
|
) { |
343
|
|
|
$model = new DefinitionDecorator(self::createServiceName('subscriber.redirect.model')); |
344
|
|
|
|
345
|
|
|
if (isset($redirect['max'])) { |
346
|
|
|
$model->addMethodCall('setMax', [$redirect['max']]); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if (isset($redirect['strict'])) { |
350
|
|
|
$model->addMethodCall('setStrict', [$redirect['strict']]); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
if (isset($redirect['throw_exception'])) { |
354
|
|
|
$model->addMethodCall('setThrowException', [$redirect['throw_exception']]); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$container->setDefinition($service = self::createServiceName($adapterName.'.redirect.model'), $model); |
358
|
|
|
$subscriber->setArguments([new Reference($service)]); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param Definition $subscriber |
363
|
|
|
* @param string $adapterName |
364
|
|
|
* @param ContainerBuilder $container |
365
|
|
|
*/ |
366
|
|
|
private function configureRetrySubscriberDefinition( |
367
|
|
|
Definition $subscriber, |
368
|
|
|
$adapterName, |
369
|
|
|
ContainerBuilder $container |
370
|
|
|
) { |
371
|
|
|
$container->setDefinition( |
372
|
|
|
$service = self::createServiceName($adapterName.'.retry.model'), |
373
|
|
|
new DefinitionDecorator(self::createServiceName('subscriber.retry.model')) |
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
$subscriber->setArguments([new Reference($service)]); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param Definition $subscriber |
381
|
|
|
* @param string|null $stopwatch |
382
|
|
|
*/ |
383
|
|
|
private function configureStopwatchSubscriberDefinition(Definition $subscriber, $stopwatch = null) |
384
|
|
|
{ |
385
|
|
|
$subscriber->setArguments([new Reference($stopwatch ?: 'debug.stopwatch')]); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param string $property |
390
|
|
|
* |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
|
|
private function getMethod($property) |
394
|
|
|
{ |
395
|
|
|
return 'set'.str_replace('_', '', $property); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.