-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathIdentifyCallbackServiceIdsPass.php
More file actions
80 lines (71 loc) · 2.84 KB
/
Copy pathIdentifyCallbackServiceIdsPass.php
File metadata and controls
80 lines (71 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
final class IdentifyCallbackServiceIdsPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('overblog_graphql_types.config')) {
return;
}
/** @var array $configs */
$configs = $container->getParameter('overblog_graphql_types.config');
foreach ($configs as &$typeConfig) {
switch ($typeConfig['type']) {
case 'object':
if (isset($typeConfig['config']['fieldResolver'])) {
$this->resolveServiceIdAndMethod($container, $typeConfig['config']['fieldResolver']);
}
foreach ($typeConfig['config']['fields'] as &$field) {
if (isset($field['resolver'])) {
$this->resolveServiceIdAndMethod($container, $field['resolver']);
}
}
break;
case 'interface':
case 'union':
if (isset($typeConfig['config']['typeResolver'])) {
$this->resolveServiceIdAndMethod($container, $typeConfig['config']['typeResolver']);
}
break;
}
}
$container->setParameter('overblog_graphql_types.config', $configs);
}
private function resolveServiceIdAndMethod(ContainerBuilder $container, ?array &$callback): void
{
if (!isset($callback['function'])) {
return;
}
[$id, $method] = explode('::', $callback['function'], 2) + [null, null];
if (str_starts_with($id, '\\')) {
$id = ltrim($id, '\\');
}
try {
$definition = $container->getDefinition($id);
} catch (ServiceNotFoundException $e) {
// get Alias real service ID
try {
$alias = $container->getAlias($id);
$id = (string) $alias;
$definition = $container->getDefinition($id);
} catch (ServiceNotFoundException|InvalidArgumentException $e) {
return;
}
}
if (
!$definition->hasTag('overblog_graphql.service')
&& !$definition->hasTag('overblog_graphql.global_variable')
) {
$definition->addTag('overblog_graphql.service');
}
$callback['function'] = "$id::$method";
}
}