1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Pipe; |
4
|
|
|
|
5
|
|
|
namespace PEIP\Pipe; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* This file is part of the PEIP package. |
9
|
|
|
* (c) 2009-2016 Timo Michna <timomichna/yahoo.de> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/* |
16
|
|
|
* Pipe |
17
|
|
|
* |
18
|
|
|
* @author Timo Michna <timomichna/yahoo.de> |
19
|
|
|
* @package PEIP |
20
|
|
|
* @subpackage pipe |
21
|
|
|
* @extends \PEIP\ABS\Handler\ReplyProducingMessageHandler |
22
|
|
|
* @implements \PEIP\INF\Message\MessageBuilder, \PEIP\INF\Handler\Handler, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Event\Connectable |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
use PEIP\Util\Test; |
26
|
|
|
|
27
|
|
|
class Pipe extends \PEIP\ABS\Handler\ReplyProducingMessageHandler implements |
28
|
|
|
\PEIP\INF\Channel\Channel, |
29
|
|
|
\PEIP\INF\Channel\SubscribableChannel, |
30
|
|
|
\PEIP\INF\Event\Connectable, |
31
|
|
|
\PEIP\INF\Message\MessageBuilder |
32
|
|
|
{ |
33
|
|
|
const |
34
|
|
|
DEFAULT_CLASS_MESSAGE_DISPATCHER = 'PEIP\Dispatcher\Dispatcher', |
35
|
|
|
DEFAULT_EVENT_CLASS = 'PEIP\Event\Event', |
36
|
|
|
EVENT_PRE_PUBLISH = 'prePublish', |
37
|
|
|
EVENT_POST_PUBLISH = 'postPublish', |
38
|
|
|
EVENT_SUBSCRIBE = 'subscribe', |
39
|
|
|
EVENT_UNSUBSCRIBE = 'unsubscribe', |
40
|
|
|
EVENT_PRE_COMMAND = 'preCommand', |
41
|
|
|
EVENT_POST_COMMAND = 'postCommand', |
42
|
|
|
EVENT_SET_MESSAGE_DISPATCHER = 'setMessageDispatcher', |
43
|
|
|
EVENT_SET_EVENT_DISPATCHER = 'setEventDispatcher', |
44
|
|
|
HEADER_MESSAGE = 'MESSAGE', |
45
|
|
|
HEADER_SUBSCRIBER = 'SUBSCRIBER'; |
46
|
|
|
|
47
|
|
|
protected $eventDispatcher, |
48
|
|
|
$messageDispatcher, |
49
|
|
|
$name, |
50
|
|
|
$commands = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $name |
54
|
|
|
* |
55
|
|
|
* @return |
56
|
|
|
*/ |
57
|
|
|
public function setName($name) |
58
|
|
|
{ |
59
|
|
|
$this->name = $name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return |
64
|
|
|
*/ |
65
|
|
|
public function getName() |
66
|
|
|
{ |
67
|
|
|
return $this->name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $message |
72
|
|
|
* @param $timeout |
73
|
|
|
* |
74
|
|
|
* @return |
75
|
|
|
*/ |
76
|
|
|
public function send(\PEIP\INF\Message\Message $message, $timeout = -1) |
77
|
|
|
{ |
78
|
|
|
return $this->handle($message); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $message |
83
|
|
|
* |
84
|
|
|
* @return |
85
|
|
|
*/ |
86
|
|
|
protected function doSend(\PEIP\INF\Message\Message $message) |
87
|
|
|
{ |
88
|
|
|
$this->doFireEvent(self::EVENT_PRE_PUBLISH, [self::HEADER_MESSAGE => $message]); |
89
|
|
|
$this->getMessageDispatcher()->notify($message); |
90
|
|
|
$this->doFireEvent(self::EVENT_POST_PUBLISH, [self::HEADER_MESSAGE => $message]); |
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $content |
97
|
|
|
* |
98
|
|
|
* @return |
99
|
|
|
*/ |
100
|
|
|
protected function replyMessage($message) |
101
|
|
|
{ |
102
|
|
|
$message = $this->ensureMessage($message); |
103
|
|
|
//if(\PEIP\Util\Test::assertMessage($message)){ |
|
|
|
|
104
|
|
|
if ($this->getOutputChannel()) { |
105
|
|
|
$this->getOutputChannel()->send($message); |
106
|
|
|
} else { |
107
|
|
|
$this->doSend($message); |
108
|
|
|
} |
109
|
|
|
//} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $message |
114
|
|
|
* |
115
|
|
|
* @return |
116
|
|
|
*/ |
117
|
|
|
protected function doReply(\PEIP\INF\Message\Message $message) |
118
|
|
|
{ |
119
|
|
|
$this->replyMessage($message); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $handler |
124
|
|
|
* |
125
|
|
|
* @return |
126
|
|
|
*/ |
127
|
|
|
public function subscribe($handler) |
128
|
|
|
{ |
129
|
|
|
Test::ensureHandler($handler); |
130
|
|
|
$this->getMessageDispatcher()->connect($handler); |
131
|
|
|
$this->doFireEvent(self::EVENT_SUBSCRIBE, [self::HEADER_SUBSCRIBER => $handler]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $handler e |
136
|
|
|
* |
137
|
|
|
* @return |
138
|
|
|
*/ |
139
|
|
|
public function unsubscribe($handler) |
140
|
|
|
{ |
141
|
|
|
Test::ensureHandler($handler); |
142
|
|
|
$this->getMessageDispatcher()->disconnect($handler); |
143
|
|
|
$this->doFireEvent( |
144
|
|
|
self::EVENT_UNSUBSCRIBE, |
145
|
|
|
[ |
146
|
|
|
self::HEADER_SUBSCRIBER => $handler, |
147
|
|
|
] |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $dispatcher |
153
|
|
|
* @param $transferListeners |
154
|
|
|
* |
155
|
|
|
* @return |
156
|
|
|
*/ |
157
|
|
|
public function setMessageDispatcher(\PEIP\INF\Dispatcher\Dispatcher $dispatcher, $transferListeners = true) |
158
|
|
|
{ |
159
|
|
View Code Duplication |
if (isset($this->dispatcher) && $transferListeners) { |
|
|
|
|
160
|
|
|
foreach ($this->dispatcher->getListeners() as $listener) { |
|
|
|
|
161
|
|
|
$dispatcher->connect($listener); |
162
|
|
|
$this->dispatcher->disconnect($listener); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$this->dispatcher = $dispatcher; |
|
|
|
|
166
|
|
|
$this->doFireEvent(self::EVENT_SET_MESSAGE_DISPATCHER, [self::HEADER_DISPATCHER => $dispatcher]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return |
171
|
|
|
*/ |
172
|
|
|
public function getMessageDispatcher() |
173
|
|
|
{ |
174
|
|
|
$defaultDispatcher = self::DEFAULT_CLASS_MESSAGE_DISPATCHER; |
175
|
|
|
|
176
|
|
|
return isset($this->dispatcher) ? $this->dispatcher : $this->dispatcher = new $defaultDispatcher(); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $commandName |
181
|
|
|
* @param $callable |
182
|
|
|
* |
183
|
|
|
* @return |
184
|
|
|
*/ |
185
|
|
|
protected function registerCommand($commandName, $callable) |
186
|
|
|
{ |
187
|
|
|
$this->commands[$commandName] = $callable; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param $cmdMessage |
192
|
|
|
* |
193
|
|
|
* @return |
194
|
|
|
*/ |
195
|
|
|
public function command(\PEIP\INF\Message\Message $cmdMessage) |
196
|
|
|
{ |
197
|
|
|
$this->doFireEvent(self::EVENT_PRE_COMMAND, [self::HEADER_MESSAGE => $cmdMessage]); |
198
|
|
|
$commandName = trim((string) $cmdMessage->getHeader('COMMAND')); |
199
|
|
|
if ($commandName != '' && array_key_exists($commandName, $this->commands)) { |
200
|
|
|
call_user_func($this->commands[$commandName], $cmdMessage->getContent()); |
201
|
|
|
} |
202
|
|
|
$this->doFireEvent(self::EVENT_POST_COMMAND, [self::HEADER_MESSAGE => $cmdMessage]); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.