1 | <?php |
||
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() |
||
56 | |||
57 | public function __construct(array $interceptors = []) |
||
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) |
||
96 | |||
97 | /** |
||
98 | * @return MessageCallback |
||
99 | */ |
||
100 | final protected static function emptyCallback() |
||
104 | |||
105 | /** |
||
106 | * @param $message |
||
107 | * @param Closure $dispatchClosure |
||
108 | * @return InterceptorChain |
||
109 | */ |
||
110 | private function createChain($message, Closure $dispatchClosure) |
||
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.