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