1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Behapi; |
3
|
|
|
|
4
|
|
|
use Behat\Testwork\ServiceContainer\Extension; |
5
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
6
|
|
|
use Behat\Testwork\Cli\ServiceContainer\CliExtension; |
7
|
|
|
|
8
|
|
|
use Behat\Behat\HelperContainer\ServiceContainer\HelperContainerExtension; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
|
15
|
|
|
use Behapi\Debug; |
16
|
|
|
use Behapi\HttpHistory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Extension which feeds the dependencies of behapi's features |
20
|
|
|
* |
21
|
|
|
* @author Baptiste Clavié <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class Behapi implements Extension |
24
|
|
|
{ |
25
|
|
|
const DEBUG_INTROSPECTION_TAG = 'behapi.debug.introspection'; |
26
|
|
|
|
27
|
|
|
/** {@inheritDoc} */ |
28
|
|
|
public function getConfigKey() |
29
|
|
|
{ |
30
|
|
|
return 'behapi'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** {@inheritDoc} */ |
34
|
|
|
public function configure(ArrayNodeDefinition $builder) |
35
|
|
|
{ |
36
|
|
|
$builder |
37
|
|
|
->children() |
38
|
|
|
->scalarNode('base_url') |
39
|
|
|
->isRequired() |
40
|
|
|
->cannotBeEmpty() |
41
|
|
|
->end() |
42
|
|
|
|
43
|
|
|
->arrayNode('debug') |
44
|
|
|
->canBeDisabled() |
45
|
|
|
->children() |
46
|
|
|
->arrayNode('introspection') |
47
|
|
|
->info('Debug Introspection configuration') |
48
|
|
|
->addDefaultsIfNotSet() |
49
|
|
|
->children() |
50
|
|
|
->arrayNode('var_dumper') |
51
|
|
|
->addDefaultsIfNotSet() |
52
|
|
|
->children() |
53
|
|
|
->arrayNode('json') |
54
|
|
|
->addDefaultsIfNotSet() |
55
|
|
|
->children() |
56
|
|
|
->arrayNode('types') |
57
|
|
|
->info('Types to be used in the json var-dumper adapter') |
58
|
|
|
->defaultValue(['application/json']) |
59
|
|
|
->prototype('scalar')->end() |
60
|
|
|
->end() |
61
|
|
|
->end() |
62
|
|
|
->end() |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
->end() |
66
|
|
|
->end() |
67
|
|
|
|
68
|
|
|
->arrayNode('headers') |
69
|
|
|
->info('Headers to print in Debug Http introspection adapters') |
70
|
|
|
->addDefaultsIfNotSet() |
71
|
|
|
->children() |
72
|
|
|
->arrayNode('request') |
73
|
|
|
->info('Request headers to print in Debug Http introspection adapters') |
74
|
|
|
->defaultValue(['Content-Type']) |
75
|
|
|
->prototype('scalar')->end() |
76
|
|
|
->end() |
77
|
|
|
|
78
|
|
|
->arrayNode('response') |
79
|
|
|
->info('Response headers to print in Debug Http introspection adapters') |
80
|
|
|
->defaultValue(['Content-Type']) |
81
|
|
|
->prototype('scalar')->end() |
82
|
|
|
->end() |
83
|
|
|
->end() |
84
|
|
|
->end() |
85
|
|
|
->end() |
86
|
|
|
->end() |
87
|
|
|
->end() |
88
|
|
|
->end(); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** {@inheritDoc} */ |
93
|
|
|
public function initialize(ExtensionManager $extensionManager) |
94
|
|
|
{ |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** {@inheritDoc} */ |
98
|
|
|
public function load(ContainerBuilder $container, array $config) |
99
|
|
|
{ |
100
|
|
|
$container->register(HttpHistory\History::class, HttpHistory\History::class) |
101
|
|
|
->setPublic(false) |
102
|
|
|
; |
103
|
|
|
|
104
|
|
|
$container->register(HttpHistory\Listener::class, HttpHistory\Listener::class) |
105
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
106
|
|
|
|
107
|
|
|
->setPublic(false) |
108
|
|
|
->addTag('event_dispatcher.subscriber') |
109
|
|
|
; |
110
|
|
|
|
111
|
|
|
$this->loadDebugServices($container, $config['debug']); |
112
|
|
|
$this->loadContainer($container, $config); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** {@inheritDoc} */ |
116
|
|
|
public function process(ContainerBuilder $container) |
117
|
|
|
{ |
118
|
|
|
$dumpers = []; |
119
|
|
|
|
120
|
|
|
foreach ($container->findTaggedServiceIds(self::DEBUG_INTROSPECTION_TAG) as $id => $tags) { |
121
|
|
|
foreach ($tags as $attributes) { |
122
|
|
|
$priority = $attributes['priority'] ?? 0; |
123
|
|
|
$dumpers[$priority][] = new Reference($id); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
krsort($dumpers); |
128
|
|
|
|
129
|
|
|
$container->getDefinition(Debug\Listener::class) |
130
|
|
|
->addArgument(array_merge(...$dumpers)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function loadContainer(ContainerBuilder $container, array $config): void |
134
|
|
|
{ |
135
|
|
|
$definition = $container->register(Container::class, Container::class); |
136
|
|
|
|
137
|
|
|
$definition |
138
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
139
|
|
|
->addArgument($config['base_url']) |
140
|
|
|
; |
141
|
|
|
|
142
|
|
|
$definition->setPublic(true); |
143
|
|
|
$definition->setShared(false); |
144
|
|
|
|
145
|
|
|
$definition->addTag(HelperContainerExtension::HELPER_CONTAINER_TAG); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function loadDebugServices(ContainerBuilder $container, array $config): void |
149
|
|
|
{ |
150
|
|
|
if (!$config['enabled']) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$container->register(Debug\Status::class, Debug\Status::class) |
155
|
|
|
->setPublic(false) |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
$container->register(Debug\CliController::class, Debug\CliController::class) |
159
|
|
|
->addArgument(new Reference(Debug\Status::class)) |
160
|
|
|
|
161
|
|
|
->setPublic(false) |
162
|
|
|
->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 10]) |
163
|
|
|
; |
164
|
|
|
|
165
|
|
|
$container->register(Debug\Listener::class, Debug\Listener::class) |
166
|
|
|
->addArgument(new Reference(Debug\Status::class)) |
167
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
168
|
|
|
|
169
|
|
|
->setPublic(false) |
170
|
|
|
->addTag('event_dispatcher.subscriber') |
171
|
|
|
; |
172
|
|
|
|
173
|
|
|
$adapters = [ |
174
|
|
|
Debug\Introspection\Request\EchoerAdapter::class => [-100, [$config['headers']['request']]], |
175
|
|
|
Debug\Introspection\Response\EchoerAdapter::class => [-100, [$config['headers']['response']]], |
176
|
|
|
|
177
|
|
|
Debug\Introspection\Request\VarDumperAdapter::class => [-80, [$config['headers']['request']]], |
178
|
|
|
Debug\Introspection\Response\VarDumperAdapter::class => [-80, [$config['headers']['response']]], |
179
|
|
|
|
180
|
|
|
Debug\Introspection\Request\VarDumper\JsonAdapter::class => [-75, [$config['headers']['request'], $config['introspection']['var_dumper']['json']['types']]], |
181
|
|
|
Debug\Introspection\Response\VarDumper\JsonAdapter::class => [-75, [$config['headers']['response'], $config['introspection']['var_dumper']['json']['types']]], |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
foreach ($adapters as $adapter => [$priority, $args]) { |
185
|
|
|
$def = $container->register($adapter, $adapter) |
186
|
|
|
->addTag(self::DEBUG_INTROSPECTION_TAG, ['priority' => $priority]) |
187
|
|
|
; |
188
|
|
|
|
189
|
|
|
foreach ($args as $arg) { |
190
|
|
|
$def->addArgument($arg); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|