|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pim\Bundle\CustomEntityBundle\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Duplicates the pim serializer |
|
11
|
|
|
* |
|
12
|
|
|
* @author Antoine Guigan <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class SerializerPass implements CompilerPassInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @staticvar string |
|
18
|
|
|
*/ |
|
19
|
|
|
const TAG_NAME = 'pim_serializer'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @staticvar integer The default priority for services |
|
23
|
|
|
*/ |
|
24
|
|
|
const DEFAULT_PRIORITY = 100; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tagPrefix; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function process(ContainerBuilder $container) |
|
35
|
|
|
{ |
|
36
|
|
|
foreach ($container->findTaggedServiceIds(static::TAG_NAME) as $serviceId => $tags) { |
|
37
|
|
|
$serializerTags = []; |
|
38
|
|
|
foreach ($tags as $tag) { |
|
39
|
|
|
$serializerTags[] = isset($tag['alias']) ? $tag['alias'] : $serviceId; |
|
40
|
|
|
} |
|
41
|
|
|
$this->processService($container, $serviceId, $serializerTags); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* process a serializer service |
|
47
|
|
|
* |
|
48
|
|
|
* @param ContainerBuilder $container |
|
49
|
|
|
* @param string $serviceId |
|
50
|
|
|
* @param array $tags |
|
51
|
|
|
*/ |
|
52
|
|
|
public function processService(ContainerBuilder $container, $serviceId, array $tags) |
|
53
|
|
|
{ |
|
54
|
|
|
$tagArguments = []; |
|
55
|
|
|
// Looks for all the services tagged "serializer.normalizer" and adds them to the Serializer service |
|
56
|
|
|
$tagArguments[0] = $this->findAndSortTaggedServices( |
|
57
|
|
|
$container, |
|
58
|
|
|
array_map( |
|
59
|
|
|
function ($tag) { |
|
60
|
|
|
return sprintf("%s.normalizer", $tag); |
|
61
|
|
|
}, |
|
62
|
|
|
$tags |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service |
|
67
|
|
|
$tagArguments[1] = $this->findAndSortTaggedServices( |
|
68
|
|
|
$container, |
|
69
|
|
|
array_map( |
|
70
|
|
|
function ($tag) { |
|
71
|
|
|
return sprintf("%s.encoder", $tag); |
|
72
|
|
|
}, |
|
73
|
|
|
$tags |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$definition = $container->getDefinition($serviceId); |
|
78
|
|
|
$arguments = $definition->getArguments(); |
|
79
|
|
|
foreach ($tagArguments as $index => $argument) { |
|
80
|
|
|
if (isset($arguments[$index])) { |
|
81
|
|
|
$arguments[$index] = array_merge($arguments[$index], $argument); |
|
82
|
|
|
} else { |
|
83
|
|
|
$arguments[$index] = $argument; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$definition->setArguments($arguments); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns an array of service references for a specified tag name |
|
92
|
|
|
* |
|
93
|
|
|
* @param ContainerBuilder $container |
|
94
|
|
|
* @param array $tagNames |
|
95
|
|
|
* |
|
96
|
|
|
* @return Reference[] |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function findAndSortTaggedServices(ContainerBuilder $container, $tagNames) |
|
99
|
|
|
{ |
|
100
|
|
|
$sortedServices = []; |
|
101
|
|
|
foreach ($tagNames as $tagName) { |
|
102
|
|
|
$services = $container->findTaggedServiceIds($tagName); |
|
103
|
|
|
foreach ($services as $serviceId => $tags) { |
|
104
|
|
|
foreach ($tags as $tag) { |
|
105
|
|
|
$priority = isset($tag['priority']) ? $tag['priority'] : self::DEFAULT_PRIORITY; |
|
106
|
|
|
$sortedServices[$priority][] = new Reference($serviceId); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
krsort($sortedServices); |
|
112
|
|
|
|
|
113
|
|
|
// Flatten the array |
|
114
|
|
|
return call_user_func_array('array_merge', $sortedServices); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|