|
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
|
8 |
|
public function load(array $configs, ContainerBuilder $container): void |
|
25
|
|
|
{ |
|
26
|
8 |
|
$processedConfig = $this->processConfiguration(new Configuration(), $configs); |
|
27
|
|
|
|
|
28
|
8 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
29
|
|
|
|
|
30
|
8 |
|
$this->processGetMethodOverride($loader, $container, $processedConfig['get_method_override']); |
|
31
|
8 |
|
$this->processResponseSchemaValidation($loader, $container, $processedConfig['response_schema_validation']); |
|
32
|
|
|
|
|
33
|
8 |
|
if ($container->getParameter('kernel.debug')) { |
|
34
|
1 |
|
$loader->load('debug_services.xml'); |
|
35
|
|
|
} |
|
36
|
8 |
|
} |
|
37
|
|
|
|
|
38
|
8 |
|
protected function processGetMethodOverride(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
|
39
|
|
|
{ |
|
40
|
8 |
|
if (!$options['enabled']) { |
|
41
|
5 |
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
if ('spiechu_symfony_commons.event_listener.get_method_override_listener' === $options['listener_service_id']) { |
|
45
|
2 |
|
$loader->load('get_method_override_listener.xml'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
$getMethodOverrideListenerDefinition = $container->getDefinition($options['listener_service_id']); |
|
49
|
|
|
|
|
50
|
3 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 0, $options['query_param_name']); |
|
51
|
3 |
|
$this->addOrReplaceDefinitionArgument($getMethodOverrideListenerDefinition, 1, $options['allow_methods_override']); |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
8 |
|
protected function processResponseSchemaValidation(XmlFileLoader $loader, ContainerBuilder $container, array $options): void |
|
55
|
|
|
{ |
|
56
|
8 |
|
if (!$options['enabled']) { |
|
57
|
4 |
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
$loader->load('response_schema_validation_listeners.xml'); |
|
61
|
|
|
|
|
62
|
4 |
|
$this->addOrReplaceDefinitionArgument( |
|
63
|
4 |
|
$container->getDefinition('spiechu_symfony_commons.event_listener.response_schema_validator_listener'), |
|
64
|
4 |
|
1, |
|
65
|
4 |
|
$options['throw_exception_when_format_not_found'] |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
4 |
|
if ('spiechu_symfony_commons.event_listener.failed_schema_check_listener' !== $options['failed_schema_check_listener_service_id']) { |
|
69
|
1 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.failed_schema_check_listener')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
if ($options['disable_json_check_schema_subscriber']) { |
|
73
|
1 |
|
$this->clearListenerTags($container->getDefinition('spiechu_symfony_commons.event_listener.json_check_schema_subscriber')); |
|
74
|
|
|
} |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
6 |
|
protected function addOrReplaceDefinitionArgument(Definition $definition, int $index, $value): void |
|
78
|
|
|
{ |
|
79
|
6 |
|
if (array_key_exists($index, $definition->getArguments())) { |
|
80
|
1 |
|
$definition->replaceArgument($index, $value); |
|
81
|
|
|
} else { |
|
82
|
6 |
|
$definition->setArgument($index, $value); |
|
83
|
|
|
} |
|
84
|
6 |
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
protected function clearListenerTags(Definition $definition): void |
|
87
|
|
|
{ |
|
88
|
2 |
|
$definition->clearTag('kernel.event_subscriber'); |
|
89
|
2 |
|
$definition->clearTag('kernel.event_listener'); |
|
90
|
|
|
|
|
91
|
2 |
|
$definition->setPublic(false); |
|
92
|
2 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|