1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of GraphQL project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 9/22/16 7:00 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Execution\Container; |
10
|
|
|
|
11
|
|
|
class Container implements ContainerInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $keyset = []; |
15
|
|
|
private $values = []; |
16
|
|
|
private $services = []; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $id |
21
|
|
|
* @return mixed |
22
|
|
|
* @throws \Exception if there was no value set under specified id |
23
|
|
|
*/ |
24
|
1 |
|
public function get($id) |
25
|
|
|
{ |
26
|
1 |
|
$this->assertIdentifierSet($id); |
27
|
1 |
|
if (isset($this->services['id'])) { |
28
|
|
|
return $this->services['id']($this); |
29
|
|
|
} |
30
|
1 |
|
return $this->values[$id]; |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function set($id, $value) |
34
|
|
|
{ |
35
|
1 |
|
$this->values[$id] = $value; |
36
|
1 |
|
$this->keyset[$id] = true; |
37
|
1 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function setAsService($id, $service) |
41
|
|
|
{ |
42
|
|
|
if (!is_object($service)) { |
43
|
|
|
throw new \RuntimeException(sprintf('Service %s has to be an object', $id)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->services[$id] = $service; |
47
|
|
|
if (isset($this->values[$id])) { |
48
|
|
|
unset($this->values[$id]); |
49
|
|
|
} |
50
|
|
|
$this->keyset[$id] = true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function remove($id) |
54
|
|
|
{ |
55
|
|
|
$this->assertIdentifierSet($id); |
56
|
|
|
if (array_key_exists($id, $this->values)) { |
57
|
|
|
unset($this->values[$id]); |
58
|
|
|
} |
59
|
|
|
if (array_key_exists($id, $this->services)) { |
60
|
|
|
unset($this->services[$id]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function has($id) |
65
|
|
|
{ |
66
|
1 |
|
return isset($this->keyset[$id]); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
private function assertIdentifierSet($id) |
70
|
|
|
{ |
71
|
1 |
|
if (!$this->has($id)) { |
72
|
|
|
throw new \RuntimeException(sprintf('Container item "%s" was not set', $id)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |