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\Reader; |
14
|
|
|
use Symfony\Component\Finder\Finder; |
15
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Loader\Annotation\AnnotationParserInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolve and load definitions based on common annotations |
21
|
|
|
*/ |
22
|
|
|
class AnnotationLoader implements DefinitionLoaderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Folders inside bundles to locate definitions |
26
|
|
|
* TODO: allow add additional mapping on bundle config |
27
|
|
|
*/ |
28
|
|
|
private const DEFINITIONS_LOCATIONS = [ |
29
|
|
|
'Model', //non persistent models like interfaces, or abstract classes |
30
|
|
|
'Entity', //doctrine entities |
31
|
|
|
'Mutation', //custom actions |
32
|
|
|
'Query', //custom actions |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Kernel |
37
|
|
|
*/ |
38
|
|
|
protected $kernel; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Reader |
42
|
|
|
*/ |
43
|
|
|
protected $reader; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var iterable|array|AnnotationParserInterface[] |
47
|
|
|
*/ |
48
|
|
|
protected $annotationParsers = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Kernel $kernel |
52
|
|
|
* @param Reader $reader |
53
|
|
|
* @param iterable|AnnotationParserInterface[] $annotationParsers |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Kernel $kernel, Reader $reader, $annotationParsers = []) |
56
|
|
|
{ |
57
|
|
|
$this->kernel = $kernel; |
58
|
|
|
$this->reader = $reader; |
59
|
|
|
$this->annotationParsers = $annotationParsers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function loadDefinitions(Endpoint $endpoint): void |
66
|
|
|
{ |
67
|
|
|
$classesToLoad = $this->resolveClasses(); |
68
|
|
|
foreach ($this->annotationParsers as $parser) { |
69
|
|
|
if ($parser instanceof AnnotationParserInterface) { |
70
|
|
|
foreach ($classesToLoad as $class) { |
71
|
|
|
$refClass = new \ReflectionClass($class); |
72
|
|
|
$annotations = $this->reader->getClassAnnotations($refClass); |
73
|
|
|
foreach ($annotations as $annotation) { |
74
|
|
|
if ($parser->supports($annotation)) { |
75
|
|
|
$parser->parse($annotation, $refClass, $endpoint); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function resolveClasses(): array |
87
|
|
|
{ |
88
|
|
|
$bundles = $this->kernel->getBundles(); |
89
|
|
|
$classes = []; |
90
|
|
|
foreach (self::DEFINITIONS_LOCATIONS as $definitionLocation) { |
91
|
|
|
foreach ($bundles as $bundle) { |
92
|
|
|
$path = $bundle->getPath().'/'.$definitionLocation; |
93
|
|
|
if (file_exists($path)) { |
94
|
|
|
$finder = new Finder(); |
95
|
|
|
foreach ($finder->in($path)->name('/.php$/')->getIterator() as $file) { |
96
|
|
|
$namespace = $bundle->getNamespace(); |
97
|
|
|
$className = preg_replace('/.php$/', null, $file->getFilename()); |
98
|
|
|
|
99
|
|
|
if ($file->getRelativePath()) { |
100
|
|
|
$subNamespace = str_replace('/', '\\', $file->getRelativePath()); |
101
|
|
|
$fullyClassName = $namespace.'\\'.$definitionLocation.'\\'.$subNamespace.'\\'.$className; |
102
|
|
|
} else { |
103
|
|
|
$fullyClassName = $namespace.'\\'.$definitionLocation.'\\'.$className; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (class_exists($fullyClassName) || interface_exists($fullyClassName)) { |
107
|
|
|
$classes[] = $fullyClassName; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return array_unique($classes); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|