1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Bus package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Bus; |
12
|
|
|
|
13
|
|
|
use Borobudur\Bus\Exception\InvalidArgumentException; |
14
|
|
|
use Borobudur\Bus\Message\SystemMessage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Iqbal Maulana <[email protected]> |
18
|
|
|
* @created 8/17/15 |
19
|
|
|
*/ |
20
|
|
|
class Gate implements GateInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var BusInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $buses = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function enableSystemBus() |
31
|
|
|
{ |
32
|
|
|
if (!isset($this->buses[BusInterface::SYSTEM_BUS])) { |
33
|
|
|
$this->attach(new SystemBus()); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function disableSystemBus() |
41
|
|
|
{ |
42
|
|
|
$this->detach(BusInterface::SYSTEM_BUS); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function attach(BusInterface $bus) |
49
|
|
|
{ |
50
|
|
|
$name = $bus->getName(); |
51
|
|
|
|
52
|
|
|
if (BusInterface::SYSTEM_BUS === $name) { |
53
|
|
|
$this->assertValidSystemBus($bus); |
54
|
|
|
$this->buses[$name] = $bus; |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->assertNotRegisteredBus($name); |
60
|
|
|
|
61
|
|
|
$bus = new BusProxy($bus); |
62
|
|
|
$bus->setGate($this); |
63
|
|
|
$this->buses[$name] = $bus; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function detach($busName) |
70
|
|
|
{ |
71
|
|
|
unset($this->buses[$busName]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function get($busName) |
78
|
|
|
{ |
79
|
|
|
if (!isset($this->buses[$busName])) { |
80
|
|
|
throw new InvalidArgumentException(sprintf('Bus "%s" is not registered.', $busName)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->buses[$busName]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function has($busName) |
90
|
|
|
{ |
91
|
|
|
return array_key_exists($busName, $this->buses); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getSystemMessage() |
98
|
|
|
{ |
99
|
|
|
return new SystemMessage(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Assert that bus with specified name is not registered. |
104
|
|
|
* |
105
|
|
|
* @param string $name |
106
|
|
|
* |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
|
|
private function assertNotRegisteredBus($name) |
110
|
|
|
{ |
111
|
|
|
if (isset($this->buses[$name])) { |
112
|
|
|
throw new InvalidArgumentException(sprintf('Bus with name "%s" is already attached.', $name)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Assert valid system bus. |
118
|
|
|
* |
119
|
|
|
* @param BusInterface $bus |
120
|
|
|
*/ |
121
|
|
|
private function assertValidSystemBus(BusInterface $bus) |
122
|
|
|
{ |
123
|
|
|
if (false === $bus instanceof SystemBus) { |
124
|
|
|
throw new InvalidArgumentException(sprintf('Bus with name "%s" reserved for system bus.', $bus->getName())); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|