|
| 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 | + public function process(ContainerBuilder $container): void |
| 25 | + { |
| 26 | + foreach ($container->getDefinitions() as $definition) { |
| 27 | + foreach (self::SERVICE_SUBCLASS_TAG_MAPPING as $tagName) { |
| 28 | + $this->addDefinitionTagsFromClassReflection($definition, $tagName); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private function addDefinitionTagsFromClassReflection(Definition $definition, string $tagName): void |
| 34 | + { |
| 35 | + if ($definition->hasTag($tagName)) { |
| 36 | + foreach ($definition->getTag($tagName) as $tag => $attributes) { |
| 37 | + if (!isset($attributes['method'])) { |
| 38 | + $reflectionClass = new \ReflectionClass($definition->getClass()); |
| 39 | + |
| 40 | + if (!$reflectionClass->isAbstract()) { |
| 41 | + $publicReflectionMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC); |
| 42 | + $isAliased = $reflectionClass->implementsInterface(AliasedInterface::class); |
| 43 | + foreach ($publicReflectionMethods as $publicReflectionMethod) { |
| 44 | + if ('__construct' === $publicReflectionMethod->name || $isAliased && 'getAliases' === $publicReflectionMethod->name) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + $definition->addTag($tagName, ['method' => $publicReflectionMethod->name]); |
| 48 | + } |
| 49 | + } |
| 50 | + continue; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments