1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling; |
25
|
|
|
|
26
|
|
|
use ArrayIterator; |
27
|
|
|
use Closure; |
28
|
|
|
use InvalidArgumentException; |
29
|
|
|
use precore\lang\Object; |
30
|
|
|
use precore\util\Preconditions; |
31
|
|
|
use predaddy\messagehandling\util\MessageCallbackClosures; |
32
|
|
|
use RuntimeException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Janos Szurovecz <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
abstract class InterceptableMessageBus extends Object implements MessageBus |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var MessageCallback |
41
|
|
|
*/ |
42
|
|
|
private static $emptyCallback; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DispatchInterceptor[] |
46
|
|
|
*/ |
47
|
|
|
private $interceptors; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Should not be called directly! |
51
|
|
|
*/ |
52
|
|
|
public static function init() |
53
|
|
|
{ |
54
|
|
|
self::$emptyCallback = MessageCallbackClosures::builder()->build(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function __construct(array $interceptors = []) |
58
|
|
|
{ |
59
|
|
|
$this->interceptors = $interceptors; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Dispatches the message to all handlers. |
64
|
|
|
* |
65
|
|
|
* All exceptions thrown by handlers must be caught and should be forwarded to $callback. |
66
|
|
|
* |
67
|
|
|
* @param $message |
68
|
|
|
* @param MessageCallback $callback |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
abstract protected function dispatch($message, MessageCallback $callback); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Post a message on this bus. It is dispatched to all subscribed handlers. |
75
|
|
|
* MessageCallback will be notified with each message handler calls. |
76
|
|
|
* |
77
|
|
|
* MessageCallback is not necessarily supported by all implementations! |
78
|
|
|
* |
79
|
|
|
* @param object $message |
80
|
|
|
* @param MessageCallback $callback |
81
|
|
|
* @return void |
82
|
|
|
* @throws InvalidArgumentException If $message is not an object |
83
|
|
|
* @throws RuntimeException that may be thrown by an interceptor |
84
|
|
|
*/ |
85
|
|
|
final public function post($message, MessageCallback $callback = null) |
86
|
|
|
{ |
87
|
|
|
Preconditions::checkArgument(is_object($message), 'Incoming message is not an object'); |
88
|
|
|
if ($callback === null) { |
89
|
|
|
$callback = self::emptyCallback(); |
90
|
|
|
} |
91
|
|
|
$dispatchClosure = function () use ($message, $callback) { |
92
|
|
|
$this->dispatch($message, $callback); |
93
|
|
|
}; |
94
|
|
|
$this->createChain($message, $dispatchClosure)->proceed(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return MessageCallback |
99
|
|
|
*/ |
100
|
|
|
final protected static function emptyCallback() |
101
|
|
|
{ |
102
|
|
|
return self::$emptyCallback; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $message |
107
|
|
|
* @param Closure $dispatchClosure |
108
|
|
|
* @return InterceptorChain |
109
|
|
|
*/ |
110
|
|
|
private function createChain($message, Closure $dispatchClosure) |
111
|
|
|
{ |
112
|
|
|
return new DefaultInterceptorChain( |
113
|
|
|
$message, |
114
|
|
|
new ArrayIterator($this->interceptors), |
115
|
|
|
$dispatchClosure |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
InterceptableMessageBus::init(); |
120
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.