1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\Type; |
6
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
7
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
8
|
|
|
|
9
|
|
|
class TypeResolver extends AbstractResolver |
10
|
|
|
{ |
11
|
|
|
/** @var CacheItemPoolInterface */ |
12
|
|
|
private $cacheAdapter; |
13
|
|
|
|
14
|
92 |
|
public function __construct(CacheItemPoolInterface $cacheAdapter = null) |
15
|
|
|
{ |
16
|
92 |
|
$this->cacheAdapter = null !== $cacheAdapter ? $cacheAdapter : new ArrayAdapter(0, false); |
17
|
92 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $alias |
21
|
|
|
* |
22
|
|
|
* @return Type |
23
|
|
|
*/ |
24
|
85 |
|
public function resolve($alias) |
25
|
|
|
{ |
26
|
85 |
|
if (null === $alias) { |
27
|
71 |
|
return; |
28
|
|
|
} |
29
|
85 |
|
$item = $this->cacheAdapter->getItem(md5($alias)); |
30
|
|
|
|
31
|
85 |
|
if (!$item->isHit()) { |
32
|
85 |
|
$type = $this->string2Type($alias); |
33
|
81 |
|
$item->set($type); |
34
|
81 |
|
$this->cacheAdapter->save($item); |
35
|
|
|
} |
36
|
|
|
|
37
|
81 |
|
return $item->get(); |
38
|
|
|
} |
39
|
|
|
|
40
|
85 |
|
private function string2Type($alias) |
41
|
|
|
{ |
42
|
85 |
|
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) { |
43
|
6 |
|
return $type; |
44
|
|
|
} |
45
|
|
|
|
46
|
84 |
|
return $this->baseType($alias); |
47
|
|
|
} |
48
|
|
|
|
49
|
84 |
|
private function baseType($alias) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
84 |
|
$type = $this->getSolution($alias); |
53
|
2 |
|
} catch (\Error $error) { |
|
|
|
|
54
|
1 |
|
throw self::createTypeLoadingException($alias, $error); |
55
|
1 |
|
} catch (\Exception $exception) { |
56
|
1 |
|
throw self::createTypeLoadingException($alias, $exception); |
57
|
|
|
} |
58
|
|
|
|
59
|
82 |
|
if (null !== $type) { |
60
|
81 |
|
return $type; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
throw new UnresolvableException( |
64
|
1 |
|
sprintf('Unknown type with alias "%s" (verified service tag)', $alias) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
85 |
|
private function wrapTypeIfNeeded($alias) |
69
|
|
|
{ |
70
|
|
|
// Non-Null |
71
|
85 |
|
if ('!' === $alias[strlen($alias) - 1]) { |
72
|
4 |
|
return Type::nonNull($this->string2Type(substr($alias, 0, -1))); |
73
|
|
|
} |
74
|
|
|
// List |
75
|
85 |
|
if ($this->hasNeedListOfWrapper($alias)) { |
76
|
5 |
|
return Type::listOf($this->string2Type(substr($alias, 1, -1))); |
77
|
|
|
} |
78
|
|
|
|
79
|
84 |
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
85 |
|
private function hasNeedListOfWrapper($alias) |
83
|
|
|
{ |
84
|
85 |
|
if ('[' === $alias[0]) { |
85
|
6 |
|
$got = $alias[strlen($alias) - 1]; |
86
|
6 |
|
if (']' !== $got) { |
87
|
1 |
|
throw new UnresolvableException( |
88
|
1 |
|
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got "%s".', $alias, json_encode($got)) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
84 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $alias |
100
|
|
|
* @param \Exception $errorOrException |
101
|
|
|
* |
102
|
|
|
* @return \RuntimeException |
103
|
|
|
*/ |
104
|
2 |
|
private static function createTypeLoadingException($alias, $errorOrException) |
105
|
|
|
{ |
106
|
2 |
|
return new \RuntimeException( |
107
|
2 |
|
sprintf( |
108
|
2 |
|
'Type class for alias %s could not be load. If you are using your own classLoader verify the path and the namespace please.', |
109
|
2 |
|
json_encode($alias) |
110
|
|
|
), |
111
|
2 |
|
0, |
112
|
2 |
|
$errorOrException |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
85 |
|
protected function supportedSolutionClass() |
117
|
|
|
{ |
118
|
85 |
|
return Type::class; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.