|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nelexa\RequestDtoBundle\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Nelexa\RequestDtoBundle\ArgumentResolver\ConstraintViolationListValueResolver; |
|
8
|
|
|
use Nelexa\RequestDtoBundle\ArgumentResolver\RequestDtoValueResolver; |
|
9
|
|
|
use Nelexa\RequestDtoBundle\EventListener\RequestDtoControllerArgumentListener; |
|
10
|
|
|
use Nelexa\RequestDtoBundle\EventListener\RequestDtoExceptionListener; |
|
11
|
|
|
use Nelexa\RequestDtoBundle\Normalizer\RequestDtoExceptionNormalizer; |
|
12
|
|
|
use Nelexa\RequestDtoBundle\Transform\RequestDtoTransform; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
18
|
|
|
|
|
19
|
|
|
class RequestDtoExtension extends Extension |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Loads a specific configuration. |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \InvalidArgumentException|\Exception When provided tag is not defined in this extension |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function load(array $configs, ContainerBuilder $container): void |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->registerTransform($container); |
|
29
|
1 |
|
$this->registerArgumentResolvers($container); |
|
30
|
1 |
|
$this->registerEventListeners($container); |
|
31
|
1 |
|
$this->registerNormalizers($container); |
|
32
|
1 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
private function registerTransform(ContainerBuilder $container): void |
|
35
|
|
|
{ |
|
36
|
1 |
|
$definition = new Definition( |
|
37
|
1 |
|
RequestDtoTransform::class, |
|
38
|
|
|
[ |
|
39
|
1 |
|
new Reference('serializer'), |
|
40
|
|
|
] |
|
41
|
|
|
); |
|
42
|
1 |
|
$definition->setPublic(false); |
|
43
|
|
|
|
|
44
|
1 |
|
$container->setDefinition(RequestDtoTransform::class, $definition); |
|
45
|
1 |
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
private function registerArgumentResolvers(ContainerBuilder $container): void |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->registerArgumentResolver($container, RequestDtoValueResolver::class, 40); |
|
50
|
1 |
|
$this->registerArgumentResolver($container, ConstraintViolationListValueResolver::class, -40); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
private function registerArgumentResolver(ContainerBuilder $container, string $className, int $priority = 0): void |
|
54
|
|
|
{ |
|
55
|
1 |
|
$definition = new Definition( |
|
56
|
1 |
|
$className, |
|
57
|
|
|
[ |
|
58
|
1 |
|
new Reference(RequestDtoTransform::class), |
|
59
|
1 |
|
new Reference('validator'), |
|
60
|
|
|
] |
|
61
|
|
|
); |
|
62
|
1 |
|
$definition->setPublic(false); |
|
63
|
1 |
|
$definition->addTag( |
|
64
|
1 |
|
'controller.argument_value_resolver', |
|
65
|
|
|
[ |
|
66
|
1 |
|
'priority' => $priority, |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
$container->setDefinition($className, $definition); |
|
71
|
1 |
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
private function registerEventListeners(ContainerBuilder $container): void |
|
74
|
|
|
{ |
|
75
|
1 |
|
$this->registerEventListenerControllerArguments($container); |
|
76
|
1 |
|
$this->registerExceptionEventListener($container); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function registerEventListenerControllerArguments(ContainerBuilder $container): void |
|
80
|
|
|
{ |
|
81
|
1 |
|
$definition = new Definition(RequestDtoControllerArgumentListener::class); |
|
82
|
1 |
|
$definition->setPublic(false); |
|
83
|
1 |
|
$definition->addTag( |
|
84
|
1 |
|
'kernel.event_listener', |
|
85
|
|
|
[ |
|
86
|
1 |
|
'event' => KernelEvents::CONTROLLER_ARGUMENTS, |
|
87
|
1 |
|
'method' => 'onControllerArguments', |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
1 |
|
$container->setDefinition(RequestDtoControllerArgumentListener::class, $definition); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
private function registerExceptionEventListener(ContainerBuilder $container): void |
|
95
|
|
|
{ |
|
96
|
1 |
|
$definition = new Definition( |
|
97
|
1 |
|
RequestDtoExceptionListener::class, |
|
98
|
|
|
[ |
|
99
|
1 |
|
new Reference('serializer'), |
|
100
|
|
|
] |
|
101
|
|
|
); |
|
102
|
1 |
|
$definition->setPublic(false); |
|
103
|
1 |
|
$definition->addTag( |
|
104
|
1 |
|
'kernel.event_listener', |
|
105
|
|
|
[ |
|
106
|
1 |
|
'event' => KernelEvents::EXCEPTION, |
|
107
|
1 |
|
'method' => 'onKernelException', |
|
108
|
1 |
|
'priority' => 30, |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
1 |
|
$container->setDefinition(RequestDtoExceptionListener::class, $definition); |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
private function registerNormalizers(ContainerBuilder $container): void |
|
116
|
|
|
{ |
|
117
|
1 |
|
$definition = new Definition( |
|
118
|
1 |
|
RequestDtoExceptionNormalizer::class, |
|
119
|
|
|
[ |
|
120
|
1 |
|
new Reference('serializer.normalizer.constraint_violation_list'), |
|
121
|
1 |
|
'%kernel.debug%', |
|
122
|
|
|
] |
|
123
|
|
|
); |
|
124
|
1 |
|
$definition->setPublic(false); |
|
125
|
1 |
|
$definition->addTag( |
|
126
|
1 |
|
'serializer.normalizer', |
|
127
|
|
|
[ |
|
128
|
1 |
|
'priority' => -885, |
|
129
|
|
|
] |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
1 |
|
$container->setDefinition(RequestDtoExceptionNormalizer::class, $definition); |
|
133
|
1 |
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|