|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
11
|
|
|
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
|
12
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class SymfonySerializerCompilerPass implements CompilerPassInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** {@inheritdoc} */ |
|
17
|
2 |
|
public function process(ContainerBuilder $container) |
|
18
|
|
|
{ |
|
19
|
2 |
|
if (!$container->has('serializer') || $container->hasAlias('serializer')) { |
|
20
|
|
|
|
|
21
|
1 |
|
return; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
$class = $container->getDefinition('serializer')->getClass(); |
|
25
|
|
|
|
|
26
|
1 |
|
if ('%serializer.class%' === $class) { |
|
27
|
|
|
// 2.x Definition |
|
28
|
|
|
$class = $container->getParameter('serializer.class'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
if (!in_array(SerializerInterface::class, class_implements($class), true)) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config')); |
|
36
|
1 |
|
$loader->load('symfony_serializer.yml'); |
|
37
|
|
|
|
|
38
|
|
|
/** @var Reference $converter */ |
|
39
|
1 |
|
$objectNormalizer = $container->getDefinition('serializer.normalizer.object'); |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
1 |
|
if ($this->shouldHaveNormalizerConfigured($container)) { |
|
43
|
|
|
$converter = $objectNormalizer->getArgument(1); |
|
44
|
1 |
|
} elseif ($container->has('serializer.name_converter.camel_case_to_snake_case')) { |
|
45
|
1 |
|
$converter = new Reference('serializer.name_converter.camel_case_to_snake_case'); |
|
46
|
1 |
|
} else { |
|
47
|
|
|
$container->register( |
|
48
|
|
|
'serializer.name_converter.camel_case_to_snake_case', |
|
49
|
|
|
CamelCaseToSnakeCaseNameConverter::class |
|
50
|
|
|
); |
|
51
|
|
|
$converter = new Reference('serializer.name_converter.camel_case_to_snake_case'); |
|
52
|
|
|
$objectNormalizer->setArguments( |
|
53
|
|
|
array_replace( |
|
54
|
|
|
$objectNormalizer->getArguments(), |
|
55
|
|
|
[ |
|
56
|
|
|
1 => $converter, |
|
57
|
|
|
] |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
$container->setAlias('serializer.normalizer.object.name_converter', (string)$converter); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ContainerBuilder $container |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
1 |
|
private function shouldHaveNormalizerConfigured(ContainerBuilder $container) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$kernel = $container->getDefinition('kernel')->getClass(); |
|
73
|
|
|
|
|
74
|
1 |
|
return $kernel instanceof Kernel && |
|
75
|
|
|
( |
|
76
|
|
|
$kernel::MAJOR_VERSION === '3' || |
|
77
|
|
|
( |
|
78
|
|
|
$kernel::MAJOR_VERSION === '2' |
|
79
|
|
|
&& $kernel::MINOR_VERSION === '8' |
|
80
|
|
|
) |
|
81
|
1 |
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|