1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiechu\SymfonyCommonsBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException; |
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
class SpiechuSymfonyCommonsExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
* |
19
|
|
|
* @throws OutOfBoundsException |
20
|
|
|
* @throws ServiceNotFoundException |
21
|
|
|
* @throws \Exception |
22
|
|
|
* @throws InvalidArgumentException |
23
|
|
|
*/ |
24
|
14 |
|
public function load(array $configs, ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
14 |
|
$processedConfig = $this->processConfiguration(new Configuration(), $configs); |
27
|
|
|
|
28
|
14 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
29
|
|
|
|
30
|
14 |
|
$this->processGetMethodOverride($loader, $container, $processedConfig['get_method_override']); |
31
|
14 |
|
$this->processResponseSchemaValidation($loader, $container, $processedConfig['response_schema_validation']); |
32
|
14 |
|
$this->processApiVersioning($loader, $container, $processedConfig['api_versioning']); |
33
|
|
|
|
34
|
14 |
|
if ($container->getParameter('kernel.debug')) { |
35
|
4 |
|
$loader->load('debug_services.xml'); |
36
|
|
|
} |
37
|
14 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param XmlFileLoader $loader |
41
|
|
|
* @param ContainerBuilder $container |
42
|
|
|
* @param array $options |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
14 |
|
protected function processGetMethodOverride(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
47
|
|
|
{ |
48
|
14 |
|
if (!$options['enabled']) { |
49
|
11 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
if ('spiechu_symfony_commons.event_listener.get_method_override_listener' === $options['listener_service_id']) { |
53
|
2 |
|
$loader->load('get_method_override_listener.xml'); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$getMethodOverrideListenerDefinition = $container->getDefinition($options['listener_service_id']); |
57
|
|
|
|
58
|
3 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 0, $options['query_param_name']); |
59
|
3 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 1, $options['allow_methods_override']); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param XmlFileLoader $loader |
64
|
|
|
* @param ContainerBuilder $container |
65
|
|
|
* @param array $options |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
14 |
|
protected function processResponseSchemaValidation(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
70
|
|
|
{ |
71
|
14 |
|
if (!$options['enabled']) { |
72
|
7 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
$loader->load('response_schema_validation_listeners.xml'); |
76
|
|
|
|
77
|
7 |
|
$this->addOrReplaceDefinitionArgument( |
78
|
7 |
|
$container->getDefinition('spiechu_symfony_commons.event_listener.response_schema_validator_listener'), |
79
|
7 |
|
1, |
80
|
7 |
|
$options['throw_exception_when_format_not_found'] |
81
|
|
|
); |
82
|
|
|
|
83
|
7 |
|
if ('spiechu_symfony_commons.event_listener.failed_schema_check_listener' !== $options['failed_schema_check_listener_service_id']) { |
84
|
1 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.failed_schema_check_listener')); |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
if ($options['disable_json_check_schema_subscriber']) { |
88
|
1 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.json_check_schema_subscriber')); |
89
|
|
|
} |
90
|
7 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param XmlFileLoader $loader |
94
|
|
|
* @param ContainerBuilder $container |
95
|
|
|
* @param array $options |
96
|
|
|
* |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
14 |
|
protected function processApiVersioning(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
100
|
|
|
{ |
101
|
14 |
|
if (!$options['enabled']) { |
102
|
9 |
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
5 |
|
$loader->load('api_versioning_listeners.xml'); |
106
|
|
|
|
107
|
5 |
|
if (!$options['versioned_view_listener']) { |
108
|
4 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.versioned_view_listener')); |
109
|
|
|
} |
110
|
5 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Definition $definition |
114
|
|
|
* @param int $index |
115
|
|
|
* @param $value |
116
|
|
|
* |
117
|
|
|
* @throws OutOfBoundsException |
118
|
|
|
*/ |
119
|
9 |
|
protected function addOrReplaceDefinitionArgument(Definition $definition, int $index, $value): void |
120
|
|
|
{ |
121
|
9 |
|
if (array_key_exists($index, $definition->getArguments())) { |
122
|
1 |
|
$definition->replaceArgument($index, $value); |
123
|
|
|
} else { |
124
|
9 |
|
$definition->setArgument($index, $value); |
125
|
|
|
} |
126
|
9 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param Definition $definition |
130
|
|
|
*/ |
131
|
6 |
|
protected function clearListenerTags(Definition $definition): void |
132
|
|
|
{ |
133
|
6 |
|
$definition->clearTag('kernel.event_subscriber'); |
134
|
6 |
|
$definition->clearTag('kernel.event_listener'); |
135
|
|
|
|
136
|
6 |
|
$definition->setPublic(false); |
137
|
6 |
|
} |
138
|
|
|
} |
139
|
|
|
|