1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* NextFlow (http://github.com/nextflow) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/nextflow/nextflow-php for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow) |
7
|
|
|
* @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace NextFlow\Core\Node; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The representation of a socket on a node. |
14
|
|
|
*/ |
15
|
|
|
final class Socket implements SocketInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The id of the socket. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $id; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Whether or not the socket is enabled. |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $enabled; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The name of the socket. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The nodes that are connected to this socket. |
40
|
|
|
* |
41
|
|
|
* @var NodeInterface[] |
42
|
|
|
*/ |
43
|
|
|
private $nodes; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initializes a new instance of this class. |
47
|
|
|
* |
48
|
|
|
* @param string $name The name of the socket. |
49
|
|
|
*/ |
50
|
|
|
public function __construct($name) |
51
|
|
|
{ |
52
|
|
|
$this->name = $name; |
53
|
|
|
$this->enabled = true; |
54
|
|
|
$this->nodes = array(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Activates this socket. |
59
|
|
|
*/ |
60
|
|
|
public function activate() |
61
|
|
|
{ |
62
|
|
|
if ($this->isEnabled()) { |
63
|
|
|
foreach ($this->nodes as $node) { |
64
|
|
|
$node->execute(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Adds a node to the connection list. |
71
|
|
|
* |
72
|
|
|
* @param NodeInterface $node The node to add. |
73
|
|
|
*/ |
74
|
|
|
public function addNode(NodeInterface $node) |
75
|
|
|
{ |
76
|
|
|
$this->nodes[] = $node; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Removes all the connections from this socket. |
81
|
|
|
*/ |
82
|
|
|
public function clearNodes() |
83
|
|
|
{ |
84
|
|
|
$this->nodes = array(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets the id of the node. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getId() |
93
|
|
|
{ |
94
|
|
|
if ($this->id === null) { |
95
|
|
|
$this->id = spl_object_hash($this); |
96
|
|
|
} |
97
|
|
|
return $this->id; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Gets the name of the socket. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getName() |
106
|
|
|
{ |
107
|
|
|
return $this->name; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets the node at the given index. |
112
|
|
|
* |
113
|
|
|
* @param int $index The index of the node to get. |
114
|
|
|
* @return NodeInterface |
115
|
|
|
*/ |
116
|
|
|
public function getNode($index) |
117
|
|
|
{ |
118
|
|
|
if (!array_key_exists($index, $this->nodes)) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->nodes[$index]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the amount of nodes that this socket has. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function getNodeCount() |
131
|
|
|
{ |
132
|
|
|
return count($this->nodes); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets a list with all the connected nodes. |
137
|
|
|
* |
138
|
|
|
* @return NodeInterface[] |
139
|
|
|
*/ |
140
|
|
|
public function getNodes() |
141
|
|
|
{ |
142
|
|
|
return $this->nodes; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Checks if there are nodes bound to this socket. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function hasNodes() |
151
|
|
|
{ |
152
|
|
|
return count($this->nodes) != 0; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Checks if the socket is enabled. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
public function isEnabled() |
161
|
|
|
{ |
162
|
|
|
return $this->enabled; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Removes the given node from the connections list. |
167
|
|
|
* |
168
|
|
|
* @param NodeInterface $node The node to remove. |
169
|
|
|
*/ |
170
|
|
|
public function removeNode(NodeInterface $node) |
171
|
|
|
{ |
172
|
|
|
foreach ($this->nodes as $k => $temp) { |
173
|
|
|
if ($temp == $node) { |
174
|
|
|
unset($this->nodes[$k]); |
175
|
|
|
break; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Reinitialize the indices: |
180
|
|
|
$this->nodes = array_values($this->nodes); |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Enables or disables the socket. |
187
|
|
|
* |
188
|
|
|
* @param bool $enabled The flag to set. |
189
|
|
|
* @return Socket |
190
|
|
|
*/ |
191
|
|
|
public function setEnabled($enabled) |
192
|
|
|
{ |
193
|
|
|
$this->enabled = $enabled; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Sets the node on the given index. |
198
|
|
|
* |
199
|
|
|
* @param int $index The index in the list to set the node for. |
200
|
|
|
* @param NodeInterface $node The node to set. |
201
|
|
|
*/ |
202
|
|
|
public function setNode($index, NodeInterface $node) |
203
|
|
|
{ |
204
|
|
|
$this->nodes[$index] = $node; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|