1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Cruds\Adaptors\DoctrineOrm\CircularReferenceHandler; |
6
|
|
|
use ScayTrase\Api\Cruds\Adaptors\DoctrineOrm\DoctrineObjectNormalizer; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
|
15
|
|
|
class DoctrineOrmCompilerPass implements CompilerPassInterface |
16
|
|
|
{ |
17
|
|
|
/** {@inheritdoc} */ |
18
|
|
|
public function process(ContainerBuilder $container) |
19
|
|
|
{ |
20
|
|
|
if (!$container->has('doctrine')) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../Resources/config')); |
25
|
|
|
$loader->load('doctrine.yml'); |
26
|
|
|
|
27
|
|
|
$factory = $container->getDefinition('cruds.factory.reflection'); |
28
|
|
|
$factory->setFactory([new Reference('cruds.factory.doctrine_reflection_factory'), 'create']); |
29
|
|
|
|
30
|
|
|
if ($container->has('serializer.normalizer.object')) { |
31
|
|
|
|
32
|
|
|
$handler = new Definition(CircularReferenceHandler::class); |
33
|
|
|
$handler->setArguments([new Reference('doctrine')]); |
34
|
|
|
|
35
|
|
|
$container->getDefinition('serializer.normalizer.object') |
36
|
|
|
->addMethodCall('setCircularReferenceHandler', [[$handler, 'handle']]); |
37
|
|
|
|
38
|
|
|
$normalizer = new DefinitionDecorator('serializer.normalizer.object'); |
39
|
|
|
$normalizer->setClass(DoctrineObjectNormalizer::class); |
40
|
|
|
$normalizer->addMethodCall('setRegistry', [new Reference('doctrine')]); |
41
|
|
|
$normalizer->addTag('serializer.normalizer', ['priority' => -800]); |
42
|
|
|
|
43
|
|
|
$container->setDefinition('cruds.serializer.doctrine_object_normalizer', $normalizer); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|