|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\ORM\Command\Special; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Cycle\ORM\Command\CommandInterface; |
|
9
|
|
|
use Cycle\ORM\Command\Database\Insert; |
|
10
|
|
|
use Cycle\ORM\Command\Database\Update; |
|
11
|
|
|
use Cycle\ORM\Heap\State; |
|
12
|
|
|
use Cycle\Database\DatabaseInterface; |
|
13
|
|
|
use Cycle\ORM\MapperInterface; |
|
14
|
|
|
|
|
15
|
|
|
class WrappedCommand implements CommandInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private ?Closure $beforeExecute = null; |
|
18
|
|
|
|
|
19
|
|
|
private ?Closure $afterExecute = null; |
|
20
|
|
|
|
|
21
|
200 |
|
protected function __construct( |
|
22
|
|
|
protected CommandInterface $command |
|
23
|
|
|
) { |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
16 |
|
public static function createInsert( |
|
27
|
|
|
DatabaseInterface $db, |
|
28
|
|
|
string $table, |
|
29
|
|
|
State $state, |
|
30
|
|
|
?MapperInterface $mapper, |
|
31
|
|
|
array $primaryKeys = [], |
|
32
|
|
|
string $pkColumn = null |
|
33
|
|
|
): WrappedStoreCommand { |
|
34
|
16 |
|
return new WrappedStoreCommand(new Insert($db, $table, $state, $mapper, $primaryKeys, $pkColumn)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function createUpdate( |
|
38
|
|
|
DatabaseInterface $db, |
|
39
|
|
|
string $table, |
|
40
|
|
|
State $state, |
|
41
|
|
|
?MapperInterface $mapper, |
|
42
|
|
|
array $primaryKeys = [] |
|
43
|
|
|
): WrappedStoreCommand { |
|
44
|
|
|
return new WrappedStoreCommand(new Update($db, $table, $state, $mapper, $primaryKeys)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
32 |
|
public static function wrapCommand(CommandInterface $command): static |
|
48
|
|
|
{ |
|
49
|
32 |
|
return new static($command); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
168 |
|
public function withBeforeExecution(?callable $callable): static |
|
53
|
|
|
{ |
|
54
|
168 |
|
$clone = clone $this; |
|
55
|
168 |
|
$clone->beforeExecute = $callable instanceof Closure ? $callable : Closure::fromCallable($callable); |
|
|
|
|
|
|
56
|
168 |
|
return $clone; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
32 |
|
public function withAfterExecution(?callable $callable): static |
|
60
|
|
|
{ |
|
61
|
32 |
|
$clone = clone $this; |
|
62
|
32 |
|
$clone->afterExecute = $callable instanceof Closure ? $callable : Closure::fromCallable($callable); |
|
|
|
|
|
|
63
|
32 |
|
return $clone; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function isReady(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->command->isReady(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
200 |
|
public function isExecuted(): bool |
|
72
|
|
|
{ |
|
73
|
200 |
|
return $this->command->isExecuted(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
200 |
|
public function execute(): void |
|
77
|
|
|
{ |
|
78
|
200 |
|
if ($this->beforeExecute !== null) { |
|
79
|
168 |
|
Closure::bind($this->beforeExecute, null, static::class)($this->command); |
|
80
|
|
|
} |
|
81
|
200 |
|
$this->command->execute(); |
|
82
|
200 |
|
if ($this->afterExecute !== null) { |
|
83
|
32 |
|
Closure::bind($this->afterExecute, null, static::class)($this->command); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
200 |
|
public function getDatabase(): ?DatabaseInterface |
|
88
|
|
|
{ |
|
89
|
200 |
|
return $this->command->getDatabase(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getCommand(): CommandInterface |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->command; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|