1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the phpflo/phpflo-flowtrace package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
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 PhpFlo\Common; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractNetworkDecorator |
15
|
|
|
* |
16
|
|
|
* @package PhpFlo\Common |
17
|
|
|
* @author Marc Aschmann <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractNetworkDecorator implements NetworkInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var NetworkInterface |
23
|
|
|
*/ |
24
|
|
|
protected $network; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AbstractNetworkAdapter constructor. |
28
|
|
|
* |
29
|
|
|
* @param NetworkInterface $network |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(NetworkInterface $network) |
32
|
|
|
{ |
33
|
2 |
|
$this->network = $network; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return NetworkInterface |
38
|
|
|
*/ |
39
|
2 |
|
public function getNetwork() |
40
|
|
|
{ |
41
|
2 |
|
return $this->network; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return null|Graph |
46
|
|
|
*/ |
47
|
1 |
|
public function getGraph() |
48
|
|
|
{ |
49
|
1 |
|
return $this->network->getGraph(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Cleanup network state after runs. |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
1 |
|
public function shutdown() |
58
|
|
|
{ |
59
|
1 |
|
$this->network->shutdown(); |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $data |
66
|
|
|
* @param string $node |
67
|
|
|
* @param string $port |
68
|
|
|
* @return $this |
69
|
|
|
* @throws InvalidDefinitionException |
70
|
|
|
*/ |
71
|
|
|
public function addInitial($data, $node, $port) |
72
|
|
|
{ |
73
|
|
|
$this->network->addInitial($data, $node, $port); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add a closure to an event |
80
|
|
|
* |
81
|
|
|
* Accepted events are connect, disconnect and data |
82
|
|
|
* Closures will be given the |
83
|
|
|
* |
84
|
|
|
* @param string $alias |
85
|
|
|
* @param string $event |
86
|
|
|
* @param \Closure $closure |
87
|
|
|
* @throws FlowException |
88
|
|
|
* @throws InvalidTypeException |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
1 |
|
public function hook($alias, $event, \Closure $closure) |
92
|
|
|
{ |
93
|
1 |
|
$this->network->hook($alias, $event, $closure); |
94
|
|
|
|
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get all defined custom event hooks |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
1 |
|
public function hooks() |
104
|
|
|
{ |
105
|
1 |
|
return $this->network->hooks(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool|\DateInterval |
110
|
|
|
*/ |
111
|
1 |
|
public function uptime() |
112
|
|
|
{ |
113
|
1 |
|
return $this->network->uptime(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $node |
118
|
|
|
* @return $this |
119
|
|
|
* @throws \PhpFlo\Common\InvalidDefinitionException |
120
|
|
|
*/ |
121
|
1 |
|
public function addNode(array $node) |
122
|
|
|
{ |
123
|
1 |
|
$this->network->addNode($node); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $node |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
1 |
|
public function removeNode(array $node) |
133
|
|
|
{ |
134
|
1 |
|
$this->network->removeNode($node); |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $id |
141
|
|
|
* @return mixed|null |
142
|
|
|
*/ |
143
|
1 |
|
public function getNode($id) |
144
|
|
|
{ |
145
|
1 |
|
return $this->network->getNode($id); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $edge |
150
|
|
|
* @return Network |
151
|
|
|
* @throws \PhpFlo\Common\InvalidDefinitionException |
152
|
|
|
*/ |
153
|
1 |
|
public function addEdge(array $edge) |
154
|
|
|
{ |
155
|
1 |
|
$this->network->addEdge($edge); |
156
|
|
|
|
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $edge |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
1 |
|
public function removeEdge(array $edge) |
165
|
|
|
{ |
166
|
1 |
|
$this->network->removeEdge($edge); |
167
|
|
|
|
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add a flow definition as Graph object or definition file/string |
173
|
|
|
* and initialize the network processes/connections |
174
|
|
|
* |
175
|
|
|
* @param mixed $graph |
176
|
|
|
* @return Network |
177
|
|
|
* @throws \PhpFlo\Common\InvalidDefinitionException |
178
|
|
|
*/ |
179
|
1 |
|
public function boot($graph) |
180
|
|
|
{ |
181
|
1 |
|
$this->network->boot($graph); |
182
|
|
|
|
183
|
1 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $data |
188
|
|
|
* @param string $node |
189
|
|
|
* @param string $port |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
1 |
|
public function run($data, $node, $port) |
193
|
|
|
{ |
194
|
1 |
|
$this->network->run($data, $node, $port); |
195
|
|
|
|
196
|
1 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|