|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cycle\Migrations; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\Database\Database; |
|
15
|
|
|
use Cycle\Database\DatabaseInterface; |
|
16
|
|
|
use Cycle\Database\DatabaseManager; |
|
17
|
|
|
use Cycle\Database\Schema\AbstractTable; |
|
18
|
|
|
use Cycle\Database\TableInterface; |
|
19
|
|
|
use Cycle\Migrations\Exception\CapsuleException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Isolates set of table specific operations and schemas into one place. Kinda repository. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Capsule implements CapsuleInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var DatabaseManager */ |
|
27
|
|
|
private $database = null; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
private $schemas = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Database $database |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Database $database) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->database = $database; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getDatabase(): DatabaseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->database; |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getTable(string $table): TableInterface |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->database->table($table); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSchema(string $table): AbstractTable |
|
60
|
|
|
{ |
|
61
|
|
|
if (!isset($this->schemas[$table])) { |
|
62
|
|
|
//We have to declare existed to prevent dropping existed schema |
|
63
|
|
|
$this->schemas[$table] = $this->database->table($table)->getSchema(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->schemas[$table]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \Throwable |
|
73
|
|
|
*/ |
|
74
|
|
|
public function execute(array $operations): void |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($operations as $operation) { |
|
77
|
|
|
if (!$operation instanceof OperationInterface) { |
|
78
|
|
|
throw new CapsuleException( |
|
79
|
|
|
sprintf( |
|
80
|
|
|
'Migration operation expected to be an instance of `OperationInterface`, `%s` given', |
|
81
|
|
|
get_class($operation) |
|
82
|
|
|
) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$operation->execute($this); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..