1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
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 Overblog\GraphQLBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
|
19
|
|
|
abstract class TaggedServiceMappingPass implements CompilerPassInterface |
20
|
|
|
{ |
21
|
9 |
|
private function getTaggedServiceMapping(ContainerBuilder $container, $tagName) |
22
|
|
|
{ |
23
|
9 |
|
$serviceMapping = []; |
24
|
|
|
|
25
|
9 |
|
$taggedServices = $container->findTaggedServiceIds($tagName); |
26
|
|
|
|
27
|
9 |
|
foreach ($taggedServices as $id => $tags) { |
28
|
9 |
|
foreach ($tags as $tag) { |
29
|
9 |
|
$this->checkRequirements($id, $tag); |
30
|
9 |
|
$serviceMapping[$tag['alias']] = array_merge($tag, ['id' => $id]); |
31
|
9 |
|
} |
32
|
9 |
|
} |
33
|
|
|
|
34
|
9 |
|
return $serviceMapping; |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
public function process(ContainerBuilder $container) |
38
|
|
|
{ |
39
|
9 |
|
$mapping = $this->getTaggedServiceMapping($container, $this->getTagName()); |
40
|
9 |
|
$container->setParameter($this->getParameterName(), $mapping); |
41
|
9 |
|
$resolverDefinition = $container->findDefinition($this->getResolverServiceID()); |
42
|
|
|
|
43
|
9 |
|
foreach ($mapping as $name => $options) { |
44
|
9 |
|
$cleanOptions = $options; |
45
|
9 |
|
$solutionID = $options['id']; |
46
|
9 |
|
$solution = $container->get($solutionID); |
47
|
|
|
|
48
|
9 |
|
if ($solution instanceof ContainerAwareInterface) { |
49
|
|
|
$solutionDefinition = $container->findDefinition($options['id']); |
50
|
|
|
$solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]); |
51
|
|
|
} |
52
|
9 |
|
$resolverDefinition->addMethodCall('addSolution', [$name, new Reference($solutionID), $cleanOptions]); |
53
|
9 |
|
} |
54
|
9 |
|
} |
55
|
|
|
|
56
|
9 |
|
protected function checkRequirements($id, array $tag) |
57
|
|
|
{ |
58
|
9 |
|
if (empty($tag['alias']) || !is_string($tag['alias'])) { |
59
|
|
|
throw new \InvalidArgumentException( |
60
|
|
|
sprintf('Service tagged "%s" must have valid "alias" argument.', $id) |
61
|
|
|
); |
62
|
|
|
} |
63
|
9 |
|
} |
64
|
|
|
|
65
|
|
|
abstract protected function getTagName(); |
66
|
|
|
|
67
|
|
|
abstract protected function getResolverServiceID(); |
68
|
|
|
|
69
|
|
|
abstract protected function getParameterName(); |
70
|
|
|
} |
71
|
|
|
|