1 | <?php |
||
17 | class DataProviderCompilerPass implements CompilerPassInterface |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 4 | public function process(ContainerBuilder $container) |
|
23 | { |
||
24 | 4 | if (!$container->hasDefinition('gbprod.elasticsearch_dataprovider.registry')) { |
|
25 | 1 | return; |
|
26 | } |
||
27 | |||
28 | 3 | $registry = $container->getDefinition( |
|
29 | 'gbprod.elasticsearch_dataprovider.registry' |
||
30 | 3 | ); |
|
31 | |||
32 | 3 | $providers = $container->findTaggedServiceIds( |
|
33 | 'elasticsearch.dataprovider' |
||
34 | 3 | ); |
|
35 | |||
36 | 3 | foreach ($providers as $providerId => $tags) { |
|
37 | 3 | $this->processProvider($container, $registry, $providerId, $tags); |
|
38 | 1 | } |
|
39 | 1 | } |
|
40 | |||
41 | 3 | private function processProvider(ContainerBuilder $container, $registry, $providerId, $tags) |
|
42 | { |
||
43 | 3 | $this->validateIsAProvider($container, $providerId); |
|
44 | |||
45 | 2 | foreach ($tags as $tag) { |
|
46 | 2 | $this->validateTag($tag, $providerId); |
|
47 | 1 | $this->registerProvider($registry, $providerId, $tag); |
|
48 | 1 | } |
|
49 | 1 | } |
|
50 | |||
51 | 3 | private function validateIsAProvider(ContainerBuilder $container, $providerId) |
|
52 | { |
||
53 | 3 | if ($this->isNotAProvider($container->getDefinition($providerId))) { |
|
54 | 1 | throw new \InvalidArgumentException( |
|
55 | 1 | sprintf( |
|
56 | 1 | 'DataProvider "%s" must implements DataProviderInterface.', |
|
57 | $providerId |
||
58 | 1 | ) |
|
59 | 1 | ); |
|
60 | } |
||
61 | 2 | } |
|
62 | |||
63 | 3 | private function isNotAProvider(Definition $definition) |
|
64 | { |
||
65 | 3 | $reflection = new \ReflectionClass($definition->getClass()); |
|
66 | |||
67 | 3 | return !$reflection->implementsInterface(DataProviderInterface::class); |
|
68 | } |
||
69 | |||
70 | 2 | private function validateTag($tag, $providerId) |
|
81 | |||
82 | 2 | private function isTagIncorrect($tag) |
|
90 | |||
91 | 1 | private function registerProvider($registry, $providerId, $tag) |
|
92 | { |
||
93 | 1 | $entryDefinition = new Definition( |
|
103 | } |