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