1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Bundle\JsonApiBundle\DependencyInjection\Compiler; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
class ObjectMapperCompilerPass implements CompilerPassInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
1 |
|
public function process(ContainerBuilder $container) |
16
|
|
|
{ |
17
|
1 |
|
$this->processMappingHandlers($container); |
18
|
1 |
|
$this->processLinkRepositories($container); |
19
|
1 |
|
$this->processDataTypeHandlers($container); |
20
|
1 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Process mapping handlers |
24
|
|
|
* |
25
|
|
|
* @param ContainerBuilder $container |
26
|
|
|
*/ |
27
|
1 |
|
protected function processMappingHandlers(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
1 |
|
$definition = $container->findDefinition('mrtn_json_api.object_mapper'); |
30
|
1 |
|
$extensions = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.handler'); |
31
|
|
|
|
32
|
1 |
|
foreach ($extensions as $id => $tags) { |
33
|
1 |
|
$definition->addMethodCall('addHandler', [ |
34
|
1 |
|
new Reference($id) |
35
|
|
|
]); |
36
|
|
|
} |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Process links repositories |
41
|
|
|
* |
42
|
|
|
* @param ContainerBuilder $container |
43
|
|
|
*/ |
44
|
1 |
|
protected function processLinkRepositories(ContainerBuilder $container) |
45
|
|
|
{ |
46
|
1 |
|
$definition = $container->findDefinition('mrtn_json_api.object_mapper.link_repository_provider'); |
47
|
1 |
|
$repositories = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.link_repository'); |
48
|
|
|
|
49
|
1 |
|
foreach ($repositories as $id => $tags) { |
50
|
1 |
|
foreach ($tags as $tag) |
51
|
|
|
{ |
52
|
1 |
|
if (! isset($tag['alias'])) { |
53
|
|
|
throw new \LogicException('Alias must be defined for a "link-repository" tag'); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$definition->addMethodCall('registerRepository', [ |
57
|
1 |
|
trim($tag['alias']), |
58
|
1 |
|
new Reference($id) |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
protected function processDataTypeHandlers(ContainerBuilder $container) |
65
|
|
|
{ |
66
|
1 |
|
$definition = $container->findDefinition('mrtn_json_api.object_mapper.handler.attribute'); |
67
|
1 |
|
$extensions = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.datatype_handler'); |
68
|
|
|
|
69
|
1 |
|
foreach ($extensions as $id => $tags) { |
70
|
1 |
|
$definition->addMethodCall('registerDataTypeHandler', [ |
71
|
1 |
|
new Reference($id) |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |