1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Kamil Kokot <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class PrioritizedCompositeServicePass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $serviceId; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $compositeId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $tagName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $methodName; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $serviceId |
46
|
|
|
* @param string $compositeId |
47
|
|
|
* @param string $tagName |
48
|
|
|
* @param string $methodName |
49
|
|
|
*/ |
50
|
|
|
public function __construct($serviceId, $compositeId, $tagName, $methodName) |
51
|
|
|
{ |
52
|
|
|
$this->serviceId = $serviceId; |
53
|
|
|
$this->compositeId = $compositeId; |
54
|
|
|
$this->tagName = $tagName; |
55
|
|
|
$this->methodName = $methodName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function process(ContainerBuilder $container) |
62
|
|
|
{ |
63
|
|
|
if (!$container->hasDefinition($this->compositeId)) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->injectTaggedServicesIntoComposite($container); |
68
|
|
|
$this->addAliasForCompositeIfServiceDoesNotExist($container); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ContainerBuilder $container |
73
|
|
|
*/ |
74
|
|
|
private function injectTaggedServicesIntoComposite(ContainerBuilder $container) |
75
|
|
|
{ |
76
|
|
|
$channelContextDefinition = $container->findDefinition($this->compositeId); |
77
|
|
|
|
78
|
|
|
$taggedServices = $container->findTaggedServiceIds($this->tagName); |
79
|
|
|
foreach ($taggedServices as $id => $tags) { |
80
|
|
|
$this->addMethodCalls($channelContextDefinition, $id, $tags); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ContainerBuilder $container |
86
|
|
|
*/ |
87
|
|
|
private function addAliasForCompositeIfServiceDoesNotExist(ContainerBuilder $container) |
88
|
|
|
{ |
89
|
|
|
if ($container->has($this->serviceId)) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$container->setAlias($this->serviceId, $this->compositeId); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Definition $channelContextDefinition |
98
|
|
|
* @param string $id |
99
|
|
|
* @param array $tags |
100
|
|
|
*/ |
101
|
|
|
private function addMethodCalls(Definition $channelContextDefinition, $id, $tags) |
102
|
|
|
{ |
103
|
|
|
foreach ($tags as $attributes) { |
104
|
|
|
$this->addMethodCall($channelContextDefinition, $id, $attributes); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Definition $channelContextDefinition |
110
|
|
|
* @param string $id |
111
|
|
|
* @param array $attributes |
112
|
|
|
*/ |
113
|
|
|
private function addMethodCall(Definition $channelContextDefinition, $id, $attributes) |
114
|
|
|
{ |
115
|
|
|
$arguments = [new Reference($id)]; |
116
|
|
|
|
117
|
|
|
if (isset($attributes['priority'])) { |
118
|
|
|
$arguments[] = $attributes['priority']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$channelContextDefinition->addMethodCall($this->methodName, $arguments); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|