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