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 GraphQL\Type\Definition\Type; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
abstract class TaggedServiceMappingPass implements CompilerPassInterface |
21
|
|
|
{ |
22
|
14 |
|
private function getTaggedServiceMapping(ContainerBuilder $container, $tagName) |
23
|
|
|
{ |
24
|
14 |
|
$serviceMapping = []; |
25
|
|
|
|
26
|
14 |
|
$taggedServices = $container->findTaggedServiceIds($tagName); |
27
|
|
|
|
28
|
14 |
|
foreach ($taggedServices as $id => $tags) { |
29
|
14 |
|
$className = $container->findDefinition($id)->getClass(); |
30
|
14 |
|
$isType = is_subclass_of($className, Type::class); |
|
|
|
|
31
|
14 |
|
foreach ($tags as $tag) { |
32
|
14 |
|
$this->checkRequirements($id, $tag); |
33
|
14 |
|
$tag = array_merge($tag, ['id' => $id]); |
34
|
14 |
|
if (!$isType) { |
35
|
14 |
|
$tag['method'] = isset($tag['method']) ? $tag['method'] : '__invoke'; |
36
|
14 |
|
} |
37
|
14 |
|
if (isset($tag['alias'])) { |
38
|
14 |
|
$serviceMapping[$tag['alias']] = $tag; |
39
|
14 |
|
} |
40
|
|
|
|
41
|
|
|
// add FQCN alias |
42
|
14 |
|
$alias = $className; |
43
|
14 |
|
if (!$isType && '__invoke' !== $tag['method']) { |
44
|
8 |
|
$alias .= '::'.$tag['method']; |
45
|
8 |
|
} |
46
|
14 |
|
$tag['alias'] = $alias; |
47
|
14 |
|
$serviceMapping[$tag['alias']] = $tag; |
48
|
14 |
|
} |
49
|
14 |
|
} |
50
|
|
|
|
51
|
14 |
|
return $serviceMapping; |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
public function process(ContainerBuilder $container) |
55
|
|
|
{ |
56
|
14 |
|
$mapping = $this->getTaggedServiceMapping($container, $this->getTagName()); |
57
|
14 |
|
$container->setParameter($this->getParameterName(), $mapping); |
58
|
14 |
|
$resolverDefinition = $container->findDefinition($this->getResolverServiceID()); |
59
|
|
|
|
60
|
14 |
|
foreach ($mapping as $name => $options) { |
61
|
14 |
|
$cleanOptions = $options; |
62
|
14 |
|
$solutionID = $options['id']; |
63
|
|
|
|
64
|
14 |
|
$solutionDefinition = $container->findDefinition($options['id']); |
65
|
|
|
|
66
|
14 |
|
$methods = array_map( |
67
|
14 |
|
function ($methodCall) { |
68
|
3 |
|
return $methodCall[0]; |
69
|
14 |
|
}, |
70
|
14 |
|
$solutionDefinition->getMethodCalls() |
71
|
14 |
|
); |
72
|
|
|
if ( |
73
|
14 |
|
is_subclass_of($solutionDefinition->getClass(), ContainerAwareInterface::class) |
|
|
|
|
74
|
14 |
|
&& !in_array('setContainer', $methods) |
75
|
14 |
|
) { |
76
|
2 |
|
@trigger_error( |
|
|
|
|
77
|
2 |
|
'Autowire custom tagged (type, resolver or mutation) services is deprecated as of 0.9 and will be removed in 1.0. Use AutoMapping or set it manually instead.', |
78
|
|
|
E_USER_DEPRECATED |
79
|
2 |
|
); |
80
|
2 |
|
$solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
14 |
|
$resolverDefinition->addMethodCall('addSolution', [$name, new Reference($solutionID), $cleanOptions]); |
84
|
14 |
|
} |
85
|
14 |
|
} |
86
|
|
|
|
87
|
14 |
|
protected function checkRequirements($id, array $tag) |
88
|
|
|
{ |
89
|
14 |
View Code Duplication |
if (isset($tag['alias']) && !is_string($tag['alias'])) { |
|
|
|
|
90
|
|
|
throw new \InvalidArgumentException( |
91
|
|
|
sprintf('Service tagged "%s" must have valid "alias" argument.', $id) |
92
|
|
|
); |
93
|
|
|
} |
94
|
14 |
|
} |
95
|
|
|
|
96
|
|
|
abstract protected function getTagName(); |
97
|
|
|
|
98
|
|
|
abstract protected function getResolverServiceID(); |
99
|
|
|
|
100
|
|
|
abstract protected function getParameterName(); |
101
|
|
|
} |
102
|
|
|
|