1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Registry\Model; |
13
|
|
|
|
14
|
|
|
use Lug\Component\Registry\Exception\InvalidServiceException; |
15
|
|
|
use Lug\Component\Registry\Exception\ServiceAlreadyExistsException; |
16
|
|
|
use Lug\Component\Registry\Exception\ServiceNotFoundException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Registry implements RegistryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $interface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var object[] |
30
|
|
|
*/ |
31
|
|
|
private $services = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $interface |
35
|
|
|
* @param object[] $services |
36
|
|
|
*/ |
37
|
12 |
|
public function __construct($interface, array $services = []) |
38
|
|
|
{ |
39
|
12 |
|
$this->interface = $interface; |
40
|
|
|
|
41
|
12 |
|
foreach ($services as $type => $service) { |
42
|
12 |
|
$this->offsetSet($type, $service); |
43
|
12 |
|
} |
44
|
12 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
12 |
|
public function offsetExists($type) |
50
|
|
|
{ |
51
|
12 |
|
return array_key_exists($type, $this->services); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
3 |
View Code Duplication |
public function offsetGet($type) |
|
|
|
|
58
|
|
|
{ |
59
|
3 |
|
if (!$this->offsetExists($type)) { |
60
|
1 |
|
throw new ServiceNotFoundException(sprintf('The service "%s" could not be found.', $type)); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $this->services[$type]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
12 |
|
public function offsetSet($type, $service) |
70
|
|
|
{ |
71
|
12 |
|
if ($this->offsetExists($type)) { |
72
|
1 |
|
throw new ServiceAlreadyExistsException(sprintf('The service "%s" already exists.', $type)); |
73
|
|
|
} |
74
|
|
|
|
75
|
12 |
|
if (!$service instanceof $this->interface) { |
76
|
2 |
|
throw new InvalidServiceException(sprintf( |
77
|
2 |
|
'The service for the registry "%s" must be an instance of "%s", got "%s".', |
78
|
2 |
|
get_class($this), |
79
|
2 |
|
$this->interface, |
80
|
2 |
|
is_object($service) ? get_class($service) : gettype($service) |
81
|
2 |
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
$this->services[$type] = $service; |
85
|
12 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
View Code Duplication |
public function offsetUnset($type) |
|
|
|
|
91
|
|
|
{ |
92
|
2 |
|
if (!$this->offsetExists($type)) { |
93
|
1 |
|
throw new ServiceNotFoundException(sprintf('The service "%s" could not be found.', $type)); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
unset($this->services[$type]); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
4 |
|
public function count() |
103
|
|
|
{ |
104
|
4 |
|
return count($this->services); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
4 |
|
public function getIterator() |
111
|
|
|
{ |
112
|
4 |
|
return new \ArrayIterator($this->services); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.