1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the OpenapiBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Niels Nijens <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Nijens\OpenapiBundle\EventListener\JsonResponseExceptionSubscriber; |
17
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ProblemExceptionToJsonResponseSubscriber; |
18
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ThrowableToProblemExceptionSubscriber; |
19
|
|
|
use Nijens\OpenapiBundle\ExceptionHandling\ThrowableToProblemExceptionTransformer; |
20
|
|
|
use Nijens\OpenapiBundle\NijensOpenapiBundle; |
21
|
|
|
use Nijens\OpenapiBundle\Routing\RouteLoader; |
22
|
|
|
use Nijens\OpenapiBundle\Validation\EventSubscriber\RequestValidationSubscriber; |
23
|
|
|
use Nijens\OpenapiBundle\Validation\RequestValidator\RequestParameterValidator; |
24
|
|
|
use Symfony\Component\Config\FileLocator; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
26
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
27
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Loads and manages the bundle configuration and services. |
31
|
|
|
* |
32
|
|
|
* @author Niels Nijens <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class NijensOpenapiExtension extends Extension |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
40
|
|
|
{ |
41
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
42
|
|
|
$loader->load('services.xml'); |
43
|
|
|
|
44
|
|
|
$this->loadDeprecatedServices($loader); |
45
|
|
|
|
46
|
|
|
$configuration = new Configuration(); |
47
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
48
|
|
|
|
49
|
|
|
$this->registerRoutingConfiguration($config['routing'], $container); |
50
|
|
|
$this->registerValidationConfiguration($config['validation'], $container); |
51
|
|
|
$this->registerExceptionHandlingConfiguration($config['exception_handling'], $container); |
52
|
|
|
|
53
|
|
|
if ($config['validation']['enabled'] === true && $config['exception_handling']['enabled'] !== true) { |
54
|
|
|
trigger_error( |
55
|
|
|
'Enabling the validation component without the exception handling component might cause unexpected results.', |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Loads the deprecated services file with backwards compatibility for XML Schema. |
62
|
|
|
*/ |
63
|
|
|
private function loadDeprecatedServices(XmlFileLoader $loader): void |
64
|
|
|
{ |
65
|
|
|
$deprecatedServicesFileSuffix = ''; |
66
|
|
|
if (NijensOpenapiBundle::getSymfonyVersion() >= 50100) { |
67
|
|
|
$deprecatedServicesFileSuffix = '_5.1'; |
68
|
|
|
} |
69
|
|
|
$loader->load(sprintf('services_deprecated%s.xml', $deprecatedServicesFileSuffix)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function registerRoutingConfiguration(array $config, ContainerBuilder $container): void |
73
|
|
|
{ |
74
|
|
|
$definition = $container->getDefinition(RouteLoader::class); |
75
|
|
|
$definition->replaceArgument(2, $config['operation_id_as_route_name']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function registerValidationConfiguration(array $config, ContainerBuilder $container): void |
79
|
|
|
{ |
80
|
|
|
if ($config['enabled'] !== true) { |
81
|
|
|
$container->removeDefinition(RequestValidationSubscriber::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($config['enabled'] !== null) { |
85
|
|
|
$container->removeDefinition('nijens_openapi.event_subscriber.json_request_body_validation'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($config['parameter_validation'] === false) { |
89
|
|
|
$container->removeDefinition(RequestParameterValidator::class); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function registerExceptionHandlingConfiguration(array $config, ContainerBuilder $container): void |
94
|
|
|
{ |
95
|
|
|
$definition = $container->getDefinition(ThrowableToProblemExceptionTransformer::class); |
96
|
|
|
$definition->replaceArgument( |
97
|
|
|
0, |
98
|
|
|
array_replace_recursive(Configuration::DEFAULT_EXCEPTION_HANDLING_EXCEPTIONS, $config['exceptions']) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if ($config['enabled'] !== true) { |
102
|
|
|
$container->removeDefinition(ThrowableToProblemExceptionSubscriber::class); |
103
|
|
|
$container->removeDefinition(ProblemExceptionToJsonResponseSubscriber::class); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($config['enabled'] !== null) { |
107
|
|
|
$container->removeDefinition(JsonResponseExceptionSubscriber::class); |
108
|
|
|
$container->removeDefinition('nijens_openapi.service.exception_json_response_builder'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|