|
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
|
18 |
|
public function load(array $configs, ContainerBuilder $container): void |
|
24
|
|
|
{ |
|
25
|
18 |
|
$processedConfig = $this->processConfiguration(new Configuration(), $configs); |
|
26
|
|
|
|
|
27
|
18 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
28
|
|
|
|
|
29
|
18 |
|
$loader->load('services.xml'); |
|
30
|
|
|
|
|
31
|
18 |
|
$this->processGetMethodOverride($loader, $container, $processedConfig['get_method_override']); |
|
32
|
18 |
|
$this->processResponseSchemaValidation($loader, $container, $processedConfig['response_schema_validation']); |
|
33
|
18 |
|
$this->processApiVersioning($loader, $container, $processedConfig['api_versioning']); |
|
34
|
|
|
|
|
35
|
18 |
|
if ($container->getParameter('kernel.debug')) { |
|
36
|
9 |
|
$loader->load('debug_services.xml'); |
|
37
|
|
|
} |
|
38
|
18 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param XmlFileLoader $loader |
|
42
|
|
|
* @param ContainerBuilder $container |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
18 |
|
protected function processGetMethodOverride(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
|
48
|
|
|
{ |
|
49
|
18 |
|
if (!$options['enabled']) { |
|
50
|
11 |
|
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
|
18 |
|
protected function processResponseSchemaValidation(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
|
77
|
|
|
{ |
|
78
|
18 |
|
if (!$options['enabled']) { |
|
79
|
6 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
$loader->load('response_schema_validation_listeners.xml'); |
|
83
|
|
|
|
|
84
|
12 |
|
Utils::addOrReplaceDefinitionArgument( |
|
85
|
12 |
|
$container->getDefinition('spiechu_symfony_commons.event_listener.response_schema_validator_listener'), |
|
86
|
12 |
|
1, |
|
87
|
12 |
|
$options['throw_exception_when_format_not_found'] |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
12 |
|
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
|
12 |
|
if ($options['disable_json_check_schema_subscriber']) { |
|
95
|
1 |
|
Utils::clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.json_check_schema_subscriber')); |
|
96
|
|
|
} |
|
97
|
12 |
|
if ($options['disable_xml_check_schema_subscriber']) { |
|
98
|
|
|
Utils::clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.xml_check_schema_subscriber')); |
|
99
|
|
|
} |
|
100
|
12 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param XmlFileLoader $loader |
|
104
|
|
|
* @param ContainerBuilder $container |
|
105
|
|
|
* @param array $options |
|
106
|
|
|
* |
|
107
|
|
|
* @throws \Exception |
|
108
|
|
|
*/ |
|
109
|
18 |
|
protected function processApiVersioning(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
|
110
|
|
|
{ |
|
111
|
18 |
|
if (!$options['enabled']) { |
|
112
|
10 |
|
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
|
2 |
|
Utils::clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.versioned_view_listener')); |
|
124
|
|
|
} |
|
125
|
8 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|