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
|
|
|
->addDefaultsIfNotSet() |
45
|
|
|
->children() |
46
|
|
|
->scalarNode('formatter') |
47
|
|
|
->defaultValue('pretty') |
48
|
|
|
->info('Not used anymore, only here for BC') |
49
|
|
|
->end() |
50
|
|
|
|
51
|
|
|
->arrayNode('headers') |
52
|
|
|
->info('Headers to print in Debug Http listener') |
53
|
|
|
->addDefaultsIfNotSet() |
54
|
|
|
->children() |
55
|
|
|
->arrayNode('request') |
56
|
|
|
->info('Request headers to print in Debug Http listener') |
57
|
|
|
->defaultValue(['Content-Type']) |
58
|
|
|
->prototype('scalar')->end() |
59
|
|
|
->end() |
60
|
|
|
|
61
|
|
|
->arrayNode('response') |
62
|
|
|
->info('Response headers to print in Debug Http listener') |
63
|
|
|
->defaultValue(['Content-Type']) |
64
|
|
|
->prototype('scalar')->end() |
65
|
|
|
->end() |
66
|
|
|
->end() |
67
|
|
|
->end() |
68
|
|
|
->end() |
69
|
|
|
->end() |
70
|
|
|
->end() |
71
|
|
|
->end(); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritDoc} */ |
76
|
|
|
public function initialize(ExtensionManager $extensionManager) |
77
|
|
|
{ |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** {@inheritDoc} */ |
81
|
|
|
public function load(ContainerBuilder $container, array $config) |
82
|
|
|
{ |
83
|
|
|
$container->register(HttpHistory\History::class, HttpHistory\History::class) |
84
|
|
|
->setPublic(false) |
85
|
|
|
; |
86
|
|
|
|
87
|
|
|
$container->register(HttpHistory\Listener::class, HttpHistory\Listener::class) |
88
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
89
|
|
|
|
90
|
|
|
->setPublic(false) |
91
|
|
|
->addTag('event_dispatcher.subscriber') |
92
|
|
|
; |
93
|
|
|
|
94
|
|
|
$this->loadDebugServices($container, $config['debug']); |
95
|
|
|
$this->loadContainer($container, $config); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** {@inheritDoc} */ |
99
|
|
|
public function process(ContainerBuilder $container) |
100
|
|
|
{ |
101
|
|
|
$dumpers = []; |
102
|
|
|
|
103
|
|
|
foreach ($container->findTaggedServiceIds(self::DEBUG_INTROSPECTION_TAG) as $id => $tags) { |
104
|
|
|
foreach ($tags as $attributes) { |
105
|
|
|
$priority = $attributes['priority'] ?? 0; |
106
|
|
|
$dumpers[$priority][] = new Reference($id); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
krsort($dumpers); |
111
|
|
|
|
112
|
|
|
$container->getDefinition(Debug\Listener::class) |
113
|
|
|
->addArgument(array_merge(...$dumpers)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function loadContainer(ContainerBuilder $container, array $config): void |
117
|
|
|
{ |
118
|
|
|
$definition = $container->register(Container::class, Container::class); |
119
|
|
|
|
120
|
|
|
$definition |
121
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
122
|
|
|
->addArgument($config['base_url']) |
123
|
|
|
; |
124
|
|
|
|
125
|
|
|
$definition->setPublic(true); |
126
|
|
|
$definition->setShared(false); |
127
|
|
|
|
128
|
|
|
$definition->addTag(HelperContainerExtension::HELPER_CONTAINER_TAG); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function loadDebugServices(ContainerBuilder $container, array $config): void |
132
|
|
|
{ |
133
|
|
|
$container->register(Debug\Configuration::class, Debug\Configuration::class) |
134
|
|
|
->addArgument($config['headers']['request']) |
135
|
|
|
->addArgument($config['headers']['response']) |
136
|
|
|
|
137
|
|
|
->setPublic(false) |
138
|
|
|
; |
139
|
|
|
|
140
|
|
|
$container->register(Debug\CliController::class, Debug\CliController::class) |
141
|
|
|
->addArgument(new Reference(Debug\Configuration::class)) |
142
|
|
|
|
143
|
|
|
->setPublic(false) |
144
|
|
|
->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 10]) |
145
|
|
|
; |
146
|
|
|
|
147
|
|
|
$container->register(Debug\Listener::class, Debug\Listener::class) |
148
|
|
|
->addArgument(new Reference(Debug\Configuration::class)) |
149
|
|
|
->addArgument(new Reference(HttpHistory\History::class)) |
150
|
|
|
|
151
|
|
|
->setPublic(false) |
152
|
|
|
->addTag('event_dispatcher.subscriber') |
153
|
|
|
; |
154
|
|
|
|
155
|
|
|
$adapters = [ |
156
|
|
|
Debug\Introspection\Request\EchoerAdapter::class => -100, |
157
|
|
|
Debug\Introspection\Response\EchoerAdapter::class => -100, |
158
|
|
|
|
159
|
|
|
Debug\Introspection\Request\VarDumperAdapter::class => -80, |
160
|
|
|
Debug\Introspection\Response\VarDumperAdapter::class => -80, |
161
|
|
|
|
162
|
|
|
Debug\Introspection\Request\VarDumper\JsonAdapter::class => -75, |
163
|
|
|
Debug\Introspection\Response\VarDumper\JsonAdapter::class => -75, |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
foreach ($adapters as $adapter => $priority) { |
167
|
|
|
$container->register($adapter, $adapter) |
168
|
|
|
->addTag(self::DEBUG_INTROSPECTION_TAG, ['priority' => $priority]) |
169
|
|
|
; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|