|
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
|
|
|
use Spiral\ORM\ContextualCommandInterface; |
|
12
|
|
|
use Spiral\ORM\Exceptions\ORMException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Command to handle multiple inner commands. |
|
16
|
|
|
*/ |
|
17
|
|
|
class TransactionalCommand implements \IteratorAggregate, ContextualCommandInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Nested commands. |
|
21
|
|
|
* |
|
22
|
|
|
* @var CommandInterface[] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $commands = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ContextualCommandInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $leadingCommand; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function addCommand(CommandInterface $command, bool $leading = false) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($command instanceof NullCommand) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->commands[] = $command; |
|
41
|
|
|
|
|
42
|
|
|
if ($leading) { |
|
43
|
|
|
if (!$command instanceof ContextualCommandInterface) { |
|
44
|
|
|
throw new ORMException("Only contextual commands can be used as leading"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->leadingCommand = $command; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getLeading(): ContextualCommandInterface |
|
55
|
|
|
{ |
|
56
|
|
|
if (empty($this->leadingCommand)) { |
|
57
|
|
|
throw new ORMException("Leading command is not set"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->leadingCommand; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function isEmpty(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getLeading()->isEmpty(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getContext(): array |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getLeading()->getContext(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function addContext(string $name, $value) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->getLeading()->addContext($name, $value); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return mixed|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function primaryKey() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getLeading()->primaryKey(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getIterator() |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($this->commands as $command) { |
|
101
|
|
|
if ($command instanceof \Traversable) { |
|
102
|
|
|
yield from $command; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
yield $command; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Closure to be called after command executing. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \Closure $closure |
|
113
|
|
|
*/ |
|
114
|
|
|
final public function onExecute(\Closure $closure) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->getLeading()->onExecute($closure); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* To be called after parent transaction been commited. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Closure $closure |
|
123
|
|
|
*/ |
|
124
|
|
|
final public function onComplete(\Closure $closure) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->getLeading()->onComplete($closure); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* To be called after parent transaction been rolled back. |
|
131
|
|
|
* |
|
132
|
|
|
* @param \Closure $closure |
|
133
|
|
|
*/ |
|
134
|
|
|
final public function onRollBack(\Closure $closure) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->getLeading()->onRollBack($closure); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function execute() |
|
143
|
|
|
{ |
|
144
|
|
|
//Nothing |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function complete() |
|
151
|
|
|
{ |
|
152
|
|
|
//Nothing |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function rollBack() |
|
159
|
|
|
{ |
|
160
|
|
|
//Nothing |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|