This repository was archived by the owner on Apr 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainHelper.php
More file actions
141 lines (105 loc) · 2.79 KB
/
ChainHelper.php
File metadata and controls
141 lines (105 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Stoic\Chain;
class ChainHelper {
protected $_nodes = array();
protected $_isEvent = false;
protected $_doDebug = false;
protected $_logger = null;
public function __construct($isEvent = false, $doDebug = false) {
$this->_isEvent = $isEvent;
$this->toggleDebug($doDebug);
return;
}
public function toggleDebug($doDebug) {
$this->_doDebug = ($doDebug) ? true : false;
return $this;
}
public function getNodeList() {
$ret = array();
foreach (array_values($this->_nodes) as $node) {
$ret[] = array(
'key' => $node->getKey(),
'version' => $node->getVersion()
);
}
return $ret;
}
public function hookLogger(callable $callback) {
$this->_logger = $callback;
return true;
}
public function isEvent() {
return $this->_isEvent;
}
public function linkNode(NodeBase $node) {
if (!$node->isValid()) {
if ($this->_doDebug) {
$this->log("Attempted to add invalid node: " . $node);
}
return $this;
}
if ($this->_isEvent) {
if ($this->_doDebug) {
$this->log("Setting event node: " . $node);
}
$this->_nodes = array($node);
} else {
if ($this->_doDebug) {
$this->log("Linking new node: " . $node);
}
$this->_nodes[] = $node;
}
return $this;
}
public function traverse(DispatchBase &$dispatch, $sender = null) {
if (count($this->_nodes) < 1) {
if ($this->_doDebug) {
$this->log("Attempted to traverse chain with no nodes");
}
return false;
}
if (!$dispatch->isValid()) {
if ($this->_doDebug) {
$this->log("Attempted to traverse chain with invalid dispatch: " . $dispatch);
}
return false;
}
if ($dispatch->isConsumable() && $dispatch->isConsumed()) {
if ($this->_doDebug) {
$this->log("Attempted to traverse chain with consumed dispatch: " . $dispatch);
}
return false;
}
if ($sender === null) {
$sender = $this;
}
$isConsumable = $dispatch->isConsumable();
if ($this->_isEvent) {
if ($this->_doDebug) {
$this->log("Sending dispatch (" . $dispatch . ") to event node: " . $this->_nodes[0]);
}
$this->_nodes[0]->process($sender, $dispatch);
} else {
$len = count($this->_nodes);
for ($i = 0; $i < $len; ++$i) {
if ($this->_doDebug) {
$this->log("Sending dispatch (" . $dispatch . ") to node: " . $this->_nodes[$i]);
}
$this->_nodes[$i]->process($sender, $dispatch);
if ($isConsumable && $dispatch->isConsumed()) {
if ($this->_doDebug) {
$this->log("Dispatch (" . $dispatch . ") consumed by node: " . $this->_nodes[$i]);
}
break;
}
}
}
return true;
}
protected function log($message) {
if ($this->_logger !== null) {
call_user_func($this->_logger, $message);
}
return;
}
}