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 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Process mapping handlers |
23
|
|
|
* |
24
|
|
|
* @param ContainerBuilder $container |
25
|
|
|
*/ |
26
|
1 |
|
protected function processMappingHandlers(ContainerBuilder $container) |
27
|
|
|
{ |
28
|
1 |
|
$definition = $container->findDefinition('mrtn_json_api.object_mapper'); |
29
|
1 |
|
$extensions = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.handler'); |
30
|
|
|
|
31
|
1 |
|
foreach ($extensions as $id => $tags) { |
32
|
1 |
|
$definition->addMethodCall('addHandler', [ |
33
|
1 |
|
new Reference($id) |
34
|
|
|
]); |
35
|
|
|
} |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Process links repositories |
40
|
|
|
* |
41
|
|
|
* @param ContainerBuilder $container |
42
|
|
|
*/ |
43
|
1 |
|
protected function processLinkRepositories(ContainerBuilder $container) |
44
|
|
|
{ |
45
|
1 |
|
$definition = $container->findDefinition('mrtn_json_api.object_mapper.link_repository_provider'); |
46
|
1 |
|
$repositories = $container->findTaggedServiceIds('mrtn_json_api.object_mapper.link_repository'); |
47
|
|
|
|
48
|
1 |
|
foreach ($repositories as $id => $tags) { |
49
|
1 |
|
foreach ($tags as $tag) |
50
|
|
|
{ |
51
|
1 |
|
if (! isset($tag['alias'])) { |
52
|
|
|
throw new \LogicException('Alias must be defined for a "link-repository" tag'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$definition->addMethodCall('registerRepository', [ |
56
|
1 |
|
trim($tag['alias']), |
57
|
1 |
|
new Reference($id) |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |