1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Registry; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerExceptionInterface; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Security\AuthorizationServiceInterface; |
10
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The role of the registry is to provide access to all GraphQL types. |
14
|
|
|
* 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. |
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
|
|
|
* @var null|AuthorizationServiceInterface |
30
|
|
|
*/ |
31
|
|
|
private $authorizationService; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ContainerInterface $container The proxied container. |
35
|
|
|
* @param AuthorizationServiceInterface|null $authorizationService |
36
|
|
|
*/ |
37
|
|
|
public function __construct(ContainerInterface $container, AuthorizationServiceInterface $authorizationService = null) |
38
|
|
|
{ |
39
|
|
|
$this->container = $container; |
40
|
|
|
$this->authorizationService = $authorizationService; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Finds an entry of the container by its identifier and returns it. |
45
|
|
|
* |
46
|
|
|
* @param string $id Identifier of the entry to look for. |
47
|
|
|
* |
48
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
49
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
50
|
|
|
* |
51
|
|
|
* @return mixed Entry. |
52
|
|
|
*/ |
53
|
|
|
public function get($id) |
54
|
|
|
{ |
55
|
|
|
if (isset($this->values[$id])) { |
56
|
|
|
return $this->values[$id]; |
57
|
|
|
} |
58
|
|
|
if ($this->container->has($id)) { |
59
|
|
|
return $this->container->get($id); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (is_a($id, AbstractObjectType::class, true)) { |
63
|
|
|
$this->values[$id] = new $id($this); |
64
|
|
|
return $this->values[$id]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
throw NotFoundException::notFound($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns true if the container can return an entry for the given identifier. |
72
|
|
|
* Returns false otherwise. |
73
|
|
|
* |
74
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
75
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
76
|
|
|
* |
77
|
|
|
* @param string $id Identifier of the entry to look for. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function has($id) |
82
|
|
|
{ |
83
|
|
|
if (isset($this->values[$id])) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
if ($this->container->has($id)) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_a($id, AbstractObjectType::class, true)) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the authorization service. |
99
|
|
|
* |
100
|
|
|
* @return AuthorizationServiceInterface|null |
101
|
|
|
*/ |
102
|
|
|
public function getAuthorizationService(): ?AuthorizationServiceInterface |
103
|
|
|
{ |
104
|
|
|
return $this->authorizationService; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|