1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cycle\ORM\Command\Branch; |
10
|
|
|
|
11
|
|
|
use Cycle\ORM\Command\CommandInterface; |
12
|
|
|
use Cycle\ORM\Command\ContextCarrierInterface; |
13
|
|
|
use Cycle\ORM\Exception\CommandException; |
14
|
|
|
use Cycle\ORM\Heap\State; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Wraps the sequence with commands and provides an ability to mock access to the primary command. |
18
|
|
|
*/ |
19
|
|
|
final class ContextSequence implements CommandInterface, \IteratorAggregate, \Countable |
20
|
|
|
{ |
21
|
|
|
/** @var ContextCarrierInterface */ |
22
|
|
|
protected $primary; |
23
|
|
|
|
24
|
|
|
/** @var CommandInterface[] */ |
25
|
|
|
protected $commands = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Add primary command to the sequence. |
29
|
|
|
* |
30
|
|
|
* @param ContextCarrierInterface $command |
31
|
|
|
*/ |
32
|
|
|
public function addPrimary(ContextCarrierInterface $command) |
33
|
|
|
{ |
34
|
|
|
$this->addCommand($command); |
35
|
|
|
$this->primary = $command; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return ContextCarrierInterface |
40
|
|
|
*/ |
41
|
|
|
public function getPrimary(): ContextCarrierInterface |
42
|
|
|
{ |
43
|
|
|
if (empty($this->primary)) { |
44
|
|
|
throw new CommandException("Primary sequence command is not set"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->primary; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function waitContext(string $key, bool $required = true) |
54
|
|
|
{ |
55
|
|
|
return $this->getPrimary()->waitContext($key, $required); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getContext(): array |
62
|
|
|
{ |
63
|
|
|
return $this->getPrimary()->getContext(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function register( |
70
|
|
|
string $key, |
71
|
|
|
$value, |
72
|
|
|
bool $fresh = false, |
73
|
|
|
int $stream = State::DATA |
74
|
|
|
) { |
75
|
|
|
$this->getPrimary()->register($key, $value, $fresh, $stream); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function isExecuted(): bool |
82
|
|
|
{ |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function isReady(): bool |
90
|
|
|
{ |
91
|
|
|
// always ready since check will be delegated to underlying nodes |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function addCommand(CommandInterface $command) |
99
|
|
|
{ |
100
|
|
|
if ($command instanceof Nil) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->commands[] = $command; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get array of underlying commands. |
109
|
|
|
* |
110
|
|
|
* @return CommandInterface[] |
111
|
|
|
*/ |
112
|
|
|
public function getCommands(): array |
113
|
|
|
{ |
114
|
|
|
return $this->commands; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getIterator(): \Generator |
121
|
|
|
{ |
122
|
|
|
foreach ($this->commands as $command) { |
123
|
|
|
if ($command instanceof \Traversable) { |
124
|
|
|
yield from $command; |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
yield $command; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
|
|
public function count(): int |
136
|
|
|
{ |
137
|
|
|
return count($this->commands); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function execute() |
144
|
|
|
{ |
145
|
|
|
// nothing |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function complete() |
152
|
|
|
{ |
153
|
|
|
// nothing |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function rollBack() |
160
|
|
|
{ |
161
|
|
|
// nothing |
162
|
|
|
} |
163
|
|
|
} |