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