1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Overblog\GraphQLBundle\Definition\GraphQLServices; |
9
|
|
|
use Overblog\GraphQLBundle\Generator\TypeGenerator; |
10
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use function is_string; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
final class GraphQLServicesPass implements CompilerPassInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function process(ContainerBuilder $container): void |
22
|
|
|
{ |
23
|
|
|
$taggedServices = $container->findTaggedServiceIds('overblog_graphql.service', true); |
24
|
|
|
|
25
|
|
|
// TODO: remove following if-block in 1.0 |
26
|
|
|
if (count($deprecatedTaggedServices = $container->findTaggedServiceIds('overblog_graphql.global_variable', true)) > 0) { |
27
|
|
|
@trigger_error( |
28
|
|
|
"The tag 'overblog_graphql.global_variable' is deprecated since 0.14 and will be removed in 1.0. Use 'overblog_graphql.service' instead. For more info visit: https://github.com/overblog/GraphQLBundle/issues/775", |
29
|
|
|
E_USER_DEPRECATED |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$taggedServices = array_merge($taggedServices, $deprecatedTaggedServices); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$serviceContainer = ['container' => new Reference('service_container')]; |
36
|
|
|
$expressionLanguageDefinition = $container->findDefinition('overblog_graphql.expression_language'); |
37
|
|
|
|
38
|
|
|
foreach ($taggedServices as $id => $tags) { |
39
|
|
|
foreach ($tags as $attributes) { |
40
|
|
|
if (empty($attributes['alias']) || !is_string($attributes['alias'])) { |
41
|
|
|
throw new InvalidArgumentException( |
42
|
|
|
sprintf('Service "%s" tagged "overblog_graphql.service" should have a valid "alias" attribute.', $id) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
$serviceContainer[$attributes['alias']] = new Reference($id); |
46
|
|
|
|
47
|
|
|
$isPublic = isset($attributes['public']) ? (bool) $attributes['public'] : true; |
48
|
|
|
if ($isPublic) { |
49
|
|
|
$expressionLanguageDefinition->addMethodCall( |
50
|
|
|
'addGlobalName', |
51
|
|
|
[ |
52
|
|
|
sprintf(TypeGenerator::GRAPHQL_SERVICES.'->get(\'%s\')', $attributes['alias']), |
53
|
|
|
$attributes['alias'], |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
$container->findDefinition(GraphQLServices::class)->addArgument($serviceContainer); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|