Passed
Push — master ( 36097f...93304f )
by Rafael
03:14
created

AnnotationLoader::resolveClasses()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 26
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 8
nop 0
crap 72
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Loader;
12
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\DependencyInjection\Definition;
16
use Symfony\Component\Finder\Finder;
17
use Ynlo\GraphQLBundle\Component\TaggedServices\TaggedServices;
18
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationParserInterface;
19
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
20
21
/**
22
 * Resolve and load definitions based on common annotations
23
 */
24
class AnnotationLoader implements DefinitionLoaderInterface
25
{
26
    /**
27
     * Folders inside bundles to locate definitions
28
     * TODO: allow add additional mapping on bundle config
29
     */
30
    private const DEFINITIONS_LOCATIONS = [
31
        'Model', //non persistent models like interfaces, or abstract classes
32
        'Entity', //doctrine entities
33
        'Mutation', //custom actions
34
        'Query', //custom actions
35
    ];
36
37
    /**
38
     * @var ContainerInterface
39
     */
40
    protected $container;
41
42
    /**
43
     * @var AnnotationReader
44
     */
45
    protected $reader;
46
47
    /**
48
     * @param ContainerInterface $container
49
     */
50
    public function __construct(ContainerInterface $container)
51
    {
52
        $this->container = $container;
53
        $this->reader = $container->get('annotations.reader');
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function loadDefinitions(Endpoint $endpoint): void
60
    {
61
        /** @var Definition $resolversServiceDefinition */
62
        $resolverDefinitions = $this->container
63
            ->get(TaggedServices::class)
64
            ->findTaggedServices('graphql.definition_annotation_parser');
65
66
        $resolvers = [];
67
        foreach ($resolverDefinitions as $resolverDefinition) {
68
            $attr = $resolverDefinition->getAttributes();
69
            $priority = 0;
70
            if (isset($attr['priority'])) {
71
                $priority = $attr['priority'];
72
            }
73
74
            $resolvers[] = [$priority, $resolverDefinition->getService()];
75
        }
76
77
        //sort by priority
78
        usort(
79
            $resolvers,
80
            function ($service1, $service2) {
81
                list($priority1) = $service1;
82
                list($priority2) = $service2;
83
84
                return version_compare($priority2, $priority1);
85
            }
86
        );
87
88
        $classesToLoad = $this->resolveClasses();
89
        foreach ($resolvers as $resolver) {
90
            list(, $resolver) = $resolver;
91
            if ($resolver instanceof AnnotationParserInterface) {
92
                foreach ($classesToLoad as $class) {
93
                    $refClass = new \ReflectionClass($class);
94
                    $annotations = $this->reader->getClassAnnotations($refClass);
95
                    foreach ($annotations as $annotation) {
96
                        if ($resolver->supports($annotation)) {
97
                            $resolver->parse($annotation, $refClass, $endpoint);
98
                        }
99
                    }
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    protected function resolveClasses(): array
109
    {
110
        $bundles = $this->container->get('kernel')->getBundles();
111
        $classes = [];
112
        foreach (self::DEFINITIONS_LOCATIONS as $definitionLocation) {
113
            foreach ($bundles as $bundle) {
114
                $path = $bundle->getPath().'/'.$definitionLocation;
115
                if (file_exists($path)) {
116
                    $finder = new Finder();
117
                    foreach ($finder->in($path)->name('/.php$/')->getIterator() as $file) {
118
                        $namespace = $bundle->getNamespace();
119
                        $className = preg_replace('/.php$/', null, $file->getFilename());
120
121
                        if ($file->getRelativePath()) {
122
                            $subNamespace = str_replace('/', '\\', $file->getRelativePath());
123
                            $fullyClassName = $namespace.'\\'.$definitionLocation.'\\'.$subNamespace.'\\'.$className;
124
                        } else {
125
                            $fullyClassName = $namespace.'\\'.$definitionLocation.'\\'.$className;
126
                        }
127
128
                        if (class_exists($fullyClassName) || interface_exists($fullyClassName)) {
129
                            $classes[] = $fullyClassName;
130
                        }
131
                    }
132
                }
133
            }
134
        }
135
136
        return array_unique($classes);
137
    }
138
}
139