|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onoi\EventDispatcher\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Onoi\EventDispatcher\EventListener; |
|
6
|
|
|
use Onoi\EventDispatcher\DispatchContext; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @license GNU GPL v2+ |
|
11
|
|
|
* @since 1.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @author mwjames |
|
14
|
|
|
*/ |
|
15
|
|
|
class GenericCallbackEventListener implements EventListener { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $callbacks = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var boolean |
|
24
|
|
|
*/ |
|
25
|
|
|
private $propagationStopState = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @since 1.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @param Closure|callable|null $callback |
|
31
|
|
|
*/ |
|
32
|
8 |
|
public function __construct( $callback = null ) { |
|
33
|
8 |
|
if ( $callback !== null ) { |
|
34
|
4 |
|
$this->registerCallback( $callback ); |
|
35
|
4 |
|
} |
|
36
|
8 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @since 1.0 |
|
40
|
|
|
* |
|
41
|
|
|
* @param Closure|callable $callback |
|
42
|
|
|
* @throws RuntimeException |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function registerCallback( $callback ) { |
|
45
|
|
|
|
|
46
|
6 |
|
if ( !is_callable( $callback ) ) { |
|
47
|
1 |
|
throw new RuntimeException( "Invoked object is not a valid callback or Closure" ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// While this does not build a real dependency chain, it allows for atomic |
|
51
|
|
|
// event handling by following FIFO |
|
52
|
5 |
|
$this->callbacks[] = $callback; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @since 1.0 |
|
57
|
|
|
* |
|
58
|
|
|
* {@inheritDoc} |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function execute( DispatchContext $dispatchContext = null ) { |
|
61
|
5 |
|
foreach ( $this->callbacks as $callback ) { |
|
62
|
5 |
|
call_user_func_array( $callback, array( $dispatchContext ) ); |
|
63
|
5 |
|
} |
|
64
|
5 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @since 1.0 |
|
68
|
|
|
* |
|
69
|
|
|
* @param boolean $propagationStopState |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function setPropagationStopState( $propagationStopState ) { |
|
72
|
1 |
|
$this->propagationStopState = (bool)$propagationStopState; |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @since 1.0 |
|
77
|
|
|
* |
|
78
|
|
|
* {@inheritDoc} |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function isPropagationStopped() { |
|
81
|
3 |
|
return $this->propagationStopState; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|