|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Finite\Event; |
|
4
|
|
|
|
|
5
|
|
|
use Finite\Event\Callback\Callback; |
|
6
|
|
|
use Finite\Event\Callback\CallbackBuilder; |
|
7
|
|
|
use Finite\StateMachine\StateMachineInterface; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Manage callback-to-event bindings by trigger spec definition |
|
14
|
|
|
* |
|
15
|
|
|
* @author Yohan Giarelli <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class CallbackHandler |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @deprecated To be removed in 2.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
const ALL = 'all'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EventDispatcherInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dispatcher; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var OptionsResolver |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $specResolver; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
36
|
|
|
*/ |
|
37
|
35 |
|
public function __construct(EventDispatcherInterface $dispatcher) |
|
38
|
|
|
{ |
|
39
|
35 |
|
$this->dispatcher = $dispatcher; |
|
40
|
35 |
|
$this->specResolver = new OptionsResolver; |
|
41
|
35 |
|
$this->specResolver->setDefaults( |
|
42
|
|
|
array( |
|
43
|
35 |
|
'on' => self::ALL, |
|
44
|
35 |
|
'from' => self::ALL, |
|
45
|
35 |
|
'to' => self::ALL, |
|
46
|
|
|
) |
|
47
|
28 |
|
); |
|
48
|
|
|
|
|
49
|
35 |
|
$this->specResolver->setAllowedTypes('on', array('string', 'array')); |
|
50
|
35 |
|
$this->specResolver->setAllowedTypes('from', array('string', 'array')); |
|
51
|
35 |
|
$this->specResolver->setAllowedTypes('to', array('string', 'array')); |
|
52
|
|
|
|
|
53
|
35 |
|
$toArrayNormalizer = function (Options $options, $value) { |
|
54
|
|
|
return (array) $value; |
|
55
|
35 |
|
}; |
|
56
|
|
|
|
|
57
|
35 |
|
$this->specResolver->setNormalizer('on', $toArrayNormalizer); |
|
58
|
35 |
|
$this->specResolver->setNormalizer('from', $toArrayNormalizer); |
|
59
|
35 |
|
$this->specResolver->setNormalizer('to', $toArrayNormalizer); |
|
60
|
35 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param StateMachineInterface|Callback $smOrCallback |
|
64
|
|
|
* @param callable $callback |
|
65
|
|
|
* @param array $spec |
|
66
|
|
|
* |
|
67
|
|
|
* @return CallbackHandler |
|
68
|
|
|
*/ |
|
69
|
30 |
|
public function addBefore($smOrCallback, $callback = null, array $spec = array()) |
|
70
|
|
|
{ |
|
71
|
30 |
|
$this->add($smOrCallback, FiniteEvents::PRE_TRANSITION, $callback, $spec); |
|
72
|
|
|
|
|
73
|
30 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param StateMachineInterface|Callback $smOrCallback |
|
78
|
|
|
* @param callable $callback |
|
79
|
|
|
* @param array $spec |
|
80
|
|
|
* |
|
81
|
|
|
* @return CallbackHandler |
|
82
|
|
|
*/ |
|
83
|
5 |
|
public function addAfter($smOrCallback, $callback = null, array $spec = array()) |
|
84
|
|
|
{ |
|
85
|
5 |
|
$this->add($smOrCallback, FiniteEvents::POST_TRANSITION, $callback, $spec); |
|
86
|
|
|
|
|
87
|
5 |
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param StateMachineInterface|Callback $smOrCallback |
|
92
|
|
|
* @param string $event |
|
93
|
|
|
* @param callable $callable |
|
94
|
|
|
* @param array $specs |
|
95
|
|
|
* |
|
96
|
|
|
* @return CallbackHandler |
|
97
|
|
|
*/ |
|
98
|
35 |
|
protected function add($smOrCallback, $event, $callable = null, array $specs = array()) |
|
99
|
|
|
{ |
|
100
|
35 |
|
if ($smOrCallback instanceof Callback) { |
|
101
|
35 |
|
$this->dispatcher->addListener($event, $smOrCallback); |
|
102
|
|
|
|
|
103
|
35 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
trigger_error( |
|
107
|
|
|
'Use of CallbackHandler::add without a Callback instance is deprecated and will be removed in 2.0', |
|
108
|
|
|
E_USER_DEPRECATED |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$specs = $this->specResolver->resolve($specs); |
|
112
|
|
|
$callback = CallbackBuilder::create($smOrCallback, $specs['from'], $specs['to'], $specs['on'], $callable)->getCallback(); |
|
113
|
|
|
|
|
114
|
|
|
$this->dispatcher->addListener($event, $callback); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|