1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticsearchDataProviderBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProvider; |
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
|
|
|
public function process(ContainerBuilder $container) |
21
|
|
|
{ |
22
|
|
|
if (!$container->hasDefinition('gbprod.elasticsearch_dataprovider.registry')) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$registry = $container->getDefinition( |
27
|
|
|
'gbprod.elasticsearch_dataprovider.registry' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
$providers = $container->findTaggedServiceIds( |
31
|
|
|
'elasticsearch_dataprovider.provider' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
foreach ($providers as $providerId => $tags) { |
35
|
|
|
$this->processProvider($container, $registry, $providerId, $tags); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function processProvider(ContainerBuilder $container, $registry, $providerId, $tags) |
40
|
|
|
{ |
41
|
|
|
$this->validateIsAProvider( |
42
|
|
|
$container->getDefinition($providerId) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
foreach ($tags as $tag) { |
46
|
|
|
$this->validateTag($tag); |
47
|
|
|
$this->registerProvider($registry, $providerId, $tag); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function validateIsAProvider(Definition $definition) |
52
|
|
|
{ |
53
|
|
|
if ($this->isNotAProvider($definition)) { |
54
|
|
|
throw new \InvalidArgumentException( |
55
|
|
|
sprintf( |
56
|
|
|
'DataProvider "%s" must extends DataProvider.', |
57
|
|
|
$providerId |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function isNotAProvider(Definition $definition) |
64
|
|
|
{ |
65
|
|
|
$reflection = new \ReflectionClass($definition->getClass()); |
66
|
|
|
|
67
|
|
|
return false === $reflection->isSubclassOf(DataProvider::class) |
68
|
|
|
&& DataProvider::class !== $definition->getClass() |
69
|
|
|
; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function validateTag($tag) |
73
|
|
|
{ |
74
|
|
|
if ($this->isTagIncorrect($tag)) { |
75
|
|
|
throw new \InvalidArgumentException( |
76
|
|
|
sprintf('DataProvider "%s" must specify the "index"'. |
77
|
|
|
' and "type" attribute.', |
78
|
|
|
$providerId |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function isTagIncorrect($tag) |
85
|
|
|
{ |
86
|
|
|
return !isset($tag['type']) |
87
|
|
|
|| !isset($tag['index']) |
88
|
|
|
|| empty($tag['index']) |
89
|
|
|
|| empty($tag['type']) |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function registerProvider($registry, $providerId, $tag) |
94
|
|
|
{ |
95
|
|
|
$registry->addMethodCall('addProvider', [ |
96
|
|
|
new Reference($providerId), |
97
|
|
|
$tag['index'], |
98
|
|
|
$tag['type'] |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
} |