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
|
16 |
|
public function load(array $configs, ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
16 |
|
$processedConfig = $this->processConfiguration(new Configuration(), $configs); |
27
|
|
|
|
28
|
16 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
29
|
|
|
|
30
|
16 |
|
$this->processGetMethodOverride($loader, $container, $processedConfig['get_method_override']); |
31
|
16 |
|
$this->processResponseSchemaValidation($loader, $container, $processedConfig['response_schema_validation']); |
32
|
16 |
|
$this->processApiVersioning($loader, $container, $processedConfig['api_versioning']); |
33
|
|
|
|
34
|
16 |
|
if ($container->getParameter('kernel.debug')) { |
35
|
6 |
|
$loader->load('debug_services.xml'); |
36
|
|
|
} |
37
|
16 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param XmlFileLoader $loader |
41
|
|
|
* @param ContainerBuilder $container |
42
|
|
|
* @param array $options |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
16 |
|
protected function processGetMethodOverride(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
47
|
|
|
{ |
48
|
16 |
|
if (!$options['enabled']) { |
49
|
9 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
if ('spiechu_symfony_commons.event_listener.get_method_override_listener' === $options['listener_service_id']) { |
53
|
6 |
|
$loader->load('get_method_override_listener.xml'); |
54
|
|
|
} |
55
|
|
|
|
56
|
7 |
|
$getMethodOverrideListenerDefinition = $container->getDefinition($options['listener_service_id']); |
57
|
|
|
|
58
|
7 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 0, $options['query_param_name']); |
59
|
7 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 1, $options['allow_methods_override']); |
60
|
7 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param XmlFileLoader $loader |
64
|
|
|
* @param ContainerBuilder $container |
65
|
|
|
* @param array $options |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
16 |
|
protected function processResponseSchemaValidation(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
70
|
|
|
{ |
71
|
16 |
|
if (!$options['enabled']) { |
72
|
7 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
9 |
|
$loader->load('response_schema_validation_listeners.xml'); |
76
|
|
|
|
77
|
9 |
|
$this->addOrReplaceDefinitionArgument( |
78
|
9 |
|
$container->getDefinition('spiechu_symfony_commons.event_listener.response_schema_validator_listener'), |
79
|
9 |
|
1, |
80
|
9 |
|
$options['throw_exception_when_format_not_found'] |
81
|
|
|
); |
82
|
|
|
|
83
|
9 |
|
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
|
9 |
|
if ($options['disable_json_check_schema_subscriber']) { |
88
|
1 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.json_check_schema_subscriber')); |
89
|
|
|
} |
90
|
9 |
|
if ($options['disable_xml_check_schema_subscriber']) { |
91
|
|
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.xml_check_schema_subscriber')); |
92
|
|
|
} |
93
|
9 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param XmlFileLoader $loader |
97
|
|
|
* @param ContainerBuilder $container |
98
|
|
|
* @param array $options |
99
|
|
|
* |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
16 |
|
protected function processApiVersioning(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
103
|
|
|
{ |
104
|
16 |
|
if (!$options['enabled']) { |
105
|
9 |
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
7 |
|
$loader->load('api_versioning_listeners.xml'); |
109
|
|
|
|
110
|
7 |
|
if (!empty($options['features'])) { |
111
|
4 |
|
$featuresProviderDefinition = $container->getDefinition('spiechu_symfony_commons.service.api_version_features_provider'); |
112
|
4 |
|
$featuresProviderDefinition->addMethodCall('addFeatures', [$options['features']]); |
113
|
|
|
} |
114
|
|
|
|
115
|
7 |
|
if (!$options['versioned_view_listener']) { |
116
|
6 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.versioned_view_listener')); |
117
|
|
|
} |
118
|
7 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Definition $definition |
122
|
|
|
* @param int $index |
123
|
|
|
* @param mixed $value |
124
|
|
|
* |
125
|
|
|
* @throws OutOfBoundsException |
126
|
|
|
*/ |
127
|
11 |
|
protected function addOrReplaceDefinitionArgument(Definition $definition, int $index, $value): void |
128
|
|
|
{ |
129
|
11 |
|
if (array_key_exists($index, $definition->getArguments())) { |
130
|
1 |
|
$definition->replaceArgument($index, $value); |
131
|
|
|
} else { |
132
|
11 |
|
$definition->setArgument($index, $value); |
133
|
|
|
} |
134
|
11 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Definition $definition |
138
|
|
|
*/ |
139
|
8 |
|
protected function clearListenerTags(Definition $definition): void |
140
|
|
|
{ |
141
|
8 |
|
$definition->clearTag('kernel.event_subscriber'); |
142
|
8 |
|
$definition->clearTag('kernel.event_listener'); |
143
|
|
|
|
144
|
8 |
|
$definition->setPublic(false); |
145
|
8 |
|
} |
146
|
|
|
} |
147
|
|
|
|