1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
|
10
|
|
|
final class AliasedPass implements CompilerPassInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
23 |
|
public function process(ContainerBuilder $container) |
16
|
|
|
{ |
17
|
23 |
|
$definitions = $this->filterDefinitions($container->getDefinitions()); |
18
|
23 |
|
foreach ($definitions as $definition) { |
19
|
23 |
|
$this->addDefinitionTagsFromAliases($definition); |
20
|
|
|
} |
21
|
23 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Definition[] $definitions |
25
|
|
|
* |
26
|
|
|
* @return Definition[] |
27
|
|
|
*/ |
28
|
|
|
private function filterDefinitions($definitions) |
29
|
|
|
{ |
30
|
23 |
|
return array_filter($definitions, function (Definition $definition) { |
31
|
23 |
|
foreach (AutoMappingPass::SERVICE_SUBCLASS_TAG_MAPPING as $tagName) { |
32
|
23 |
|
if ($definition->hasTag($tagName)) { |
33
|
23 |
|
return is_subclass_of($definition->getClass(), AliasedInterface::class); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
23 |
|
return false; |
38
|
23 |
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Definition $definition |
43
|
|
|
*/ |
44
|
23 |
|
private function addDefinitionTagsFromAliases(Definition $definition) |
45
|
|
|
{ |
46
|
23 |
|
$aliases = call_user_func([$definition->getClass(), 'getAliases']); |
47
|
23 |
|
$tagName = $this->guessTagName($definition); |
48
|
23 |
|
$withMethod = TypeTaggedServiceMappingPass::TAG_NAME !== $tagName; |
49
|
|
|
|
50
|
23 |
|
foreach ($aliases as $key => $alias) { |
51
|
23 |
|
$definition->addTag($tagName, $withMethod ? ['alias' => $alias, 'method' => $key] : ['alias' => $alias]); |
52
|
|
|
} |
53
|
23 |
|
} |
54
|
|
|
|
55
|
23 |
|
private function guessTagName(Definition $definition) |
56
|
|
|
{ |
57
|
23 |
|
$tagName = null; |
58
|
23 |
|
foreach (AutoMappingPass::SERVICE_SUBCLASS_TAG_MAPPING as $refClassName => $tag) { |
59
|
23 |
|
if (is_subclass_of($definition->getClass(), $refClassName)) { |
|
|
|
|
60
|
23 |
|
$tagName = $tag; |
61
|
23 |
|
break; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
23 |
|
return $tagName; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|