|
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\Type\Loader; |
|
12
|
|
|
|
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
|
16
|
|
|
use Symfony\Component\Finder\Finder; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class TypeAutoLoader |
|
21
|
|
|
*/ |
|
22
|
|
|
class TypeAutoLoader implements ContainerAwareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use ContainerAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
protected static $loaded = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Autoload all registered types |
|
30
|
|
|
*/ |
|
31
|
21 |
|
public function autoloadTypes() |
|
32
|
|
|
{ |
|
33
|
21 |
|
if (self::$loaded) { |
|
34
|
20 |
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
self::$loaded = true; |
|
38
|
1 |
|
$bundles = $this->container->get('kernel')->getBundles(); |
|
39
|
|
|
|
|
40
|
|
|
//TODO: save in cache for production |
|
41
|
|
|
|
|
42
|
1 |
|
foreach ($bundles as $bundle) { |
|
43
|
1 |
|
$path = $bundle->getPath().'/Type'; |
|
44
|
1 |
|
if (file_exists($path)) { |
|
45
|
1 |
|
$finder = new Finder(); |
|
46
|
1 |
|
foreach ($finder->in($path)->name('/Type.php$/')->getIterator() as $file) { |
|
47
|
1 |
|
$namespace = $bundle->getNamespace(); |
|
48
|
1 |
|
$className = preg_replace('/.php$/', null, $file->getFilename()); |
|
49
|
1 |
|
$name = preg_replace('/Type$/', null, $className); |
|
50
|
|
|
|
|
51
|
1 |
|
if ($file->getRelativePath()) { |
|
52
|
1 |
|
$subNamespace = str_replace('/', '\\', $file->getRelativePath()); |
|
53
|
1 |
|
$fullyClassName = $namespace.'\\Type\\'.$subNamespace.'\\'.$className; |
|
54
|
|
|
} else { |
|
55
|
1 |
|
$fullyClassName = $namespace.'\\Type\\'.$className; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (class_exists($fullyClassName)) { |
|
59
|
1 |
|
$ref = new \ReflectionClass($fullyClassName); |
|
60
|
1 |
|
if ($ref->isSubclassOf(Type::class) |
|
61
|
1 |
|
&& $ref->isInstantiable() |
|
62
|
1 |
|
&& !$ref->getConstructor()->getNumberOfRequiredParameters() |
|
63
|
|
|
) { |
|
64
|
1 |
|
TypeRegistry::addTypeMapping($name, $fullyClassName); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|