1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
11
|
|
|
|
12
|
|
|
final class IdentifyCallbackServiceIdsPass implements CompilerPassInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
38 |
|
public function process(ContainerBuilder $container): void |
18
|
|
|
{ |
19
|
38 |
|
if (!$container->hasParameter('overblog_graphql_types.config')) { |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
/** @var array $configs */ |
23
|
38 |
|
$configs = $container->getParameter('overblog_graphql_types.config'); |
24
|
38 |
|
foreach ($configs as &$typeConfig) { |
25
|
37 |
|
switch ($typeConfig['type']) { |
26
|
37 |
|
case 'object': |
27
|
37 |
|
if (isset($typeConfig['config']['fieldResolver'])) { |
28
|
2 |
|
$this->resolveServiceIdAndMethod($container, $typeConfig['config']['fieldResolver']); |
29
|
|
|
} |
30
|
|
|
|
31
|
37 |
|
foreach ($typeConfig['config']['fields'] as &$field) { |
32
|
37 |
|
if (isset($field['resolver'])) { |
33
|
30 |
|
$this->resolveServiceIdAndMethod($container, $field['resolver']); |
34
|
|
|
} |
35
|
|
|
} |
36
|
37 |
|
break; |
37
|
|
|
|
38
|
13 |
|
case 'interface': |
39
|
12 |
|
case 'union': |
40
|
5 |
|
if (isset($typeConfig['config']['typeResolver'])) { |
41
|
3 |
|
$this->resolveServiceIdAndMethod($container, $typeConfig['config']['typeResolver']); |
42
|
|
|
} |
43
|
5 |
|
break; |
44
|
|
|
} |
45
|
|
|
} |
46
|
38 |
|
$container->setParameter('overblog_graphql_types.config', $configs); |
47
|
38 |
|
} |
48
|
|
|
|
49
|
31 |
|
private function resolveServiceIdAndMethod(ContainerBuilder $container, ?array &$callback): void |
50
|
|
|
{ |
51
|
31 |
|
if (!isset($callback['id']) && !isset($callback['method'])) { |
52
|
30 |
|
return; |
53
|
|
|
} |
54
|
8 |
|
$originalId = $callback['id'] ?? null; |
55
|
8 |
|
$originalMethod = $callback['method'] ?? null; |
56
|
|
|
|
57
|
8 |
|
if (null === $originalId) { |
58
|
8 |
|
[$id, $method] = explode('::', $originalMethod, 2) + [null, null]; |
|
|
|
|
59
|
8 |
|
$throw = false; |
60
|
|
|
} else { |
61
|
7 |
|
$id = $originalId; |
62
|
7 |
|
$method = $originalMethod; |
63
|
7 |
|
$throw = true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
8 |
|
$definition = $container->getDefinition($id); |
68
|
8 |
|
} catch (ServiceNotFoundException $e) { |
69
|
|
|
// get Alias real service ID |
70
|
|
|
try { |
71
|
8 |
|
$alias = $container->getAlias($id); |
72
|
7 |
|
$id = (string) $alias; |
73
|
7 |
|
$definition = $container->getDefinition($id); |
74
|
1 |
|
} catch (ServiceNotFoundException | InvalidArgumentException $e) { |
75
|
1 |
|
if ($throw) { |
76
|
|
|
throw $e; |
77
|
|
|
} |
78
|
1 |
|
$callback['id'] = null; |
79
|
1 |
|
$callback['method'] = $originalMethod; |
80
|
|
|
|
81
|
1 |
|
return; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if ( |
85
|
7 |
|
!$definition->hasTag('overblog_graphql.service') |
86
|
7 |
|
&& !$definition->hasTag('overblog_graphql.global_variable') |
87
|
|
|
) { |
88
|
7 |
|
$definition->addTag('overblog_graphql.service'); |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
$callback['id'] = $id; |
92
|
7 |
|
$callback['method'] = $method; |
93
|
7 |
|
} |
94
|
|
|
} |
95
|
|
|
|