1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaProviderBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use GBProd\ElasticaProviderBundle\Provider\Provider; |
6
|
|
|
use GBProd\ElasticaProviderBundle\Provider\RegistryEntry; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Compiler path to register providers |
14
|
|
|
* |
15
|
|
|
* @author gbprod <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ProviderCompilerPass implements CompilerPassInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
4 |
|
public function process(ContainerBuilder $container) |
23
|
|
|
{ |
24
|
4 |
|
if (!$container->hasDefinition('gbprod.elastica_provider.registry')) { |
25
|
1 |
|
return; |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
$registry = $container->getDefinition('gbprod.elastica_provider.registry'); |
29
|
3 |
|
$providers = $container->findTaggedServiceIds('elastica.provider'); |
30
|
|
|
|
31
|
3 |
|
foreach ($providers as $providerId => $tags) { |
32
|
3 |
|
$this->processProvider($container, $registry, $providerId, $tags); |
33
|
1 |
|
} |
34
|
1 |
|
} |
35
|
|
|
|
36
|
3 |
|
private function processProvider(ContainerBuilder $container, $registry, $providerId, $tags) |
37
|
|
|
{ |
38
|
3 |
|
$this->validateIsAProvider($container, $providerId); |
39
|
|
|
|
40
|
2 |
|
foreach ($tags as $tag) { |
41
|
2 |
|
$this->validateTag($tag, $providerId); |
42
|
1 |
|
$this->registerProvider($registry, $providerId, $tag); |
43
|
1 |
|
} |
44
|
1 |
|
} |
45
|
|
|
|
46
|
3 |
|
private function validateIsAProvider(ContainerBuilder $container, $providerId) |
47
|
|
|
{ |
48
|
3 |
|
if ($this->isNotAProvider($container->getDefinition($providerId))) { |
49
|
1 |
|
throw new \InvalidArgumentException( |
50
|
1 |
|
sprintf( |
51
|
1 |
|
'provider "%s" must implements provider interface.', |
52
|
|
|
$providerId |
53
|
1 |
|
) |
54
|
1 |
|
); |
55
|
|
|
} |
56
|
2 |
|
} |
57
|
|
|
|
58
|
3 |
|
private function isNotAProvider(Definition $definition) |
59
|
|
|
{ |
60
|
3 |
|
$reflection = new \ReflectionClass($definition->getClass()); |
61
|
|
|
|
62
|
3 |
|
return !$reflection->implementsInterface(Provider::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
private function validateTag($tag, $providerId) |
66
|
|
|
{ |
67
|
2 |
|
if ($this->isTagIncorrect($tag)) { |
68
|
1 |
|
throw new \InvalidArgumentException( |
69
|
1 |
|
sprintf('provider "%s" must specify the "index"'. |
70
|
1 |
|
' and "type" attribute.', |
71
|
|
|
$providerId |
72
|
1 |
|
) |
73
|
1 |
|
); |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
2 |
|
private function isTagIncorrect($tag) |
78
|
|
|
{ |
79
|
2 |
|
return !isset($tag['type']) |
80
|
2 |
|
|| !isset($tag['index']) |
81
|
2 |
|
|| empty($tag['index']) |
82
|
1 |
|
|| empty($tag['type']) |
83
|
2 |
|
; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
private function registerProvider($registry, $providerId, $tag) |
87
|
|
|
{ |
88
|
1 |
|
$entryDefinition = new Definition( |
89
|
1 |
|
RegistryEntry::class, |
90
|
|
|
[ |
91
|
1 |
|
new Reference($providerId), |
92
|
1 |
|
$tag['index'], |
93
|
1 |
|
$tag['type'], |
94
|
|
|
] |
95
|
1 |
|
); |
96
|
|
|
|
97
|
1 |
|
$registry->addMethodCall('add', [$entryDefinition]); |
98
|
1 |
|
} |
99
|
|
|
} |
100
|
|
|
|