1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler; |
6
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface; |
8
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\MutationInterface; |
9
|
|
|
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
13
|
|
|
|
14
|
|
|
final class ResolverMethodAliasesPass implements CompilerPassInterface |
15
|
|
|
{ |
16
|
|
|
private const SERVICE_SUBCLASS_TAG_MAPPING = [ |
17
|
|
|
MutationInterface::class => 'overblog_graphql.mutation', |
18
|
|
|
ResolverInterface::class => 'overblog_graphql.resolver', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
32 |
|
public function process(ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
32 |
|
foreach ($container->getDefinitions() as $definition) { |
27
|
32 |
|
foreach (self::SERVICE_SUBCLASS_TAG_MAPPING as $tagName) { |
28
|
32 |
|
$this->addDefinitionTagsFromClassReflection($definition, $tagName); |
29
|
|
|
} |
30
|
|
|
} |
31
|
32 |
|
} |
32
|
|
|
|
33
|
32 |
|
private function addDefinitionTagsFromClassReflection(Definition $definition, string $tagName): void |
34
|
|
|
{ |
35
|
32 |
|
if ($definition->hasTag($tagName)) { |
36
|
32 |
|
foreach ($definition->getTag($tagName) as $tag => $attributes) { |
37
|
32 |
|
if (!isset($attributes['method'])) { |
38
|
32 |
|
$reflectionClass = new \ReflectionClass($definition->getClass()); |
39
|
|
|
|
40
|
32 |
|
if (!$reflectionClass->isAbstract()) { |
41
|
32 |
|
$publicReflectionMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC); |
42
|
32 |
|
$isAliased = $reflectionClass->implementsInterface(AliasedInterface::class); |
43
|
32 |
|
foreach ($publicReflectionMethods as $publicReflectionMethod) { |
44
|
32 |
|
if ('__construct' === $publicReflectionMethod->name || $isAliased && 'getAliases' === $publicReflectionMethod->name) { |
45
|
32 |
|
continue; |
46
|
|
|
} |
47
|
32 |
|
$definition->addTag($tagName, ['method' => $publicReflectionMethod->name]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
32 |
|
continue; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
32 |
|
} |
55
|
|
|
} |
56
|
|
|
|