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