1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\Navigation; |
6
|
|
|
|
7
|
|
|
use Everlution\Navigation\Item\RegistrableItemInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Registry. |
11
|
|
|
* |
12
|
|
|
* @author Ivan Barlog <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Registry |
15
|
|
|
{ |
16
|
|
|
/** @var MutableContainerInterface[] */ |
17
|
|
|
private $registry = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param MutableContainerInterface $container |
21
|
|
|
* |
22
|
|
|
* @throws ContainerAlreadyRegisteredException |
23
|
|
|
*/ |
24
|
|
|
public function addContainer(MutableContainerInterface $container): void |
25
|
|
|
{ |
26
|
|
|
$containerName = get_class($container); |
27
|
|
|
if (array_key_exists($containerName, $this->registry)) { |
28
|
|
|
throw new ContainerAlreadyRegisteredException($containerName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->registry[$containerName] = $container; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param RegistrableItemInterface $item |
36
|
|
|
* |
37
|
|
|
* @throws ContainerIsNotRegisteredException |
38
|
|
|
*/ |
39
|
|
|
public function register(RegistrableItemInterface $item): void |
40
|
|
|
{ |
41
|
|
|
foreach ($item->getRegisteredContainerNames() as $containerName) { |
42
|
|
|
if (false === array_key_exists($containerName, $this->registry)) { |
43
|
|
|
throw new ContainerIsNotRegisteredException($containerName); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->registry[$containerName]->add($item); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $containerName |
52
|
|
|
* |
53
|
|
|
* @return MutableContainerInterface |
54
|
|
|
* |
55
|
|
|
* @throws ContainerIsNotRegisteredException |
56
|
|
|
*/ |
57
|
|
|
public function getContainer(string $containerName): MutableContainerInterface |
58
|
|
|
{ |
59
|
|
|
if (false === array_key_exists($containerName, $this->registry)) { |
60
|
|
|
throw new ContainerIsNotRegisteredException($containerName); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->registry[$containerName]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|