1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral, Core Components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spiral\ORM\Commands; |
9
|
|
|
|
10
|
|
|
use Spiral\ORM\CommandInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provides support for command events. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractCommand implements CommandInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Closure[] |
19
|
|
|
*/ |
20
|
|
|
private $onExecute = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Closure[] |
24
|
|
|
*/ |
25
|
|
|
private $onComplete = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Closure[] |
29
|
|
|
*/ |
30
|
|
|
private $onRollBack = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Closure to be called after command executing. |
34
|
|
|
* |
35
|
|
|
* @param \Closure $closure |
36
|
|
|
*/ |
37
|
|
|
final public function onExecute(\Closure $closure) |
38
|
|
|
{ |
39
|
|
|
$this->onExecute[] = $closure; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* To be called after parent transaction been commited. |
44
|
|
|
* |
45
|
|
|
* @param \Closure $closure |
46
|
|
|
*/ |
47
|
|
|
final public function onComplete(\Closure $closure) |
48
|
|
|
{ |
49
|
|
|
$this->onComplete[] = $closure; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* To be called after parent transaction been rolled back. |
54
|
|
|
* |
55
|
|
|
* @param \Closure $closure |
56
|
|
|
*/ |
57
|
|
|
final public function onRollBack(\Closure $closure) |
58
|
|
|
{ |
59
|
|
|
$this->onRollBack[] = $closure; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function execute() |
66
|
|
|
{ |
67
|
|
|
foreach ($this->onExecute as $closure) { |
68
|
|
|
call_user_func($closure, $this); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function complete() |
76
|
|
|
{ |
77
|
|
|
foreach ($this->onComplete as $closure) { |
78
|
|
|
call_user_func($closure, $this); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function rollBack() |
86
|
|
|
{ |
87
|
|
|
foreach ($this->onRollBack as $closure) { |
88
|
|
|
call_user_func($closure, $this); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |