-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessager.php
More file actions
127 lines (113 loc) · 3.39 KB
/
Messager.php
File metadata and controls
127 lines (113 loc) · 3.39 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
<?php
namespace ProxyDetector;
class Messager
{
static $DEBUG = 0;
protected $receivers = array();
protected $controllers = array();
protected $dependencies = array();
/**
* a list of message names that can be received by this object
*/
function listMessages(array $newmessages)
{
return array_merge(array('attach', 'detach'), $newmessages);
}
function addDependency(Messager $object)
{
$this->dependencies[spl_object_hash($object)] = $object;
}
function attach(Messager $controller)
{
$this->controllers[spl_object_hash($controller)] = $controller;
$controller->addReceiver($this);
if (!count($this->dependencies)) {
return;
}
foreach ($this->dependencies as $dependency) {
$dependency->attach($controller);
}
}
function detach(Messager $controller)
{
unset($this->controllers[spl_object_hash($controller)]);
$controller->removeReceiver($this);
if (!count($this->dependencies)) {
return;
}
foreach ($this->dependencies as $dependency) {
$controller->removeReceiver($dependency);
}
}
function addReceiver(Messager $receiver)
{
$this->receivers[spl_object_hash($receiver)] = $receiver;
}
function removeReceiver(Messager $receiver)
{
unset($this->receivers[spl_object_hash($receiver)]);
}
function receive($message, $content)
{
}
function reply($message, $content)
{
}
/**
* find the first responder and return a single reply
*/
function ask($message, $content = false)
{
foreach ($this->controllers as $controller) {
$reply = $controller->query($message, $content);
if (null !== $reply) {
return $reply;
}
}
}
protected function query($message, $content = false)
{
if (self::$DEBUG) {
var_dump("query sending: $message");
}
foreach ($this->receivers as $receiver) {
if (self::$DEBUG) {
var_dump("receiver is a " . get_class($receiver));
}
if (!in_array($message, $receiver->listMessages(array()))) {
continue;
}
if (self::$DEBUG) {
var_dump("query received by " . get_class($receiver) . ": $message");
}
$response = $receiver->reply($message, $content);
if (null !== $response) {
return $response;
}
}
}
function broadcast($message, $content = false)
{
foreach ($this->controllers as $controller) {
$controller->message($message, $content);
}
}
protected function message($message, $content = false)
{
if (self::$DEBUG) {
var_dump("message sending: $message");
}
foreach ($this->receivers as $receiver) {
if (self::$DEBUG) {
var_dump("receiver is a " . get_class($receiver));
}
if (!in_array($message, $receiver->listMessages(array()))) {
continue;
}
if (self::$DEBUG) {
var_dump("message received by " . get_class($receiver) . ": $message");
}
$receiver->receive($message, $content);
}
}
}