1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Tdbm\GraphQL\Registry; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerExceptionInterface; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
9
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The role of the registry is to provide access to all GraphQL types. |
13
|
|
|
* If the type is not found, it can be queried from the container, or if not in the container, it can be created from the Registry itself. |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Registry implements ContainerInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ContainerInterface |
21
|
|
|
*/ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var AbstractObjectType[] |
26
|
|
|
*/ |
27
|
|
|
private $values = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ContainerInterface $container The proxied container. |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ContainerInterface $container) |
33
|
|
|
{ |
34
|
|
|
$this->container = $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Finds an entry of the container by its identifier and returns it. |
39
|
|
|
* |
40
|
|
|
* @param string $id Identifier of the entry to look for. |
41
|
|
|
* |
42
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
43
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
44
|
|
|
* |
45
|
|
|
* @return mixed Entry. |
46
|
|
|
*/ |
47
|
|
|
public function get($id) |
48
|
|
|
{ |
49
|
|
|
if (isset($this->values[$id])) { |
50
|
|
|
return $this->values[$id]; |
51
|
|
|
} |
52
|
|
|
if ($this->container->has($id)) { |
53
|
|
|
return $this->container->get($id); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (is_a($id, AbstractObjectType::class, true)) { |
57
|
|
|
$this->values[$id] = new $id($this); |
58
|
|
|
return $this->values[$id]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw NotFoundException::notFound($id); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns true if the container can return an entry for the given identifier. |
66
|
|
|
* Returns false otherwise. |
67
|
|
|
* |
68
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
69
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
70
|
|
|
* |
71
|
|
|
* @param string $id Identifier of the entry to look for. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function has($id) |
76
|
|
|
{ |
77
|
|
|
if (isset($this->values[$id])) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
if ($this->container->has($id)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (is_a($id, AbstractObjectType::class, true)) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|