1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\Migrations; |
||
6 | |||
7 | use Cycle\Database\DatabaseInterface; |
||
8 | use Cycle\Database\Schema\AbstractTable; |
||
9 | use Cycle\Database\TableInterface; |
||
10 | use Cycle\Migrations\Exception\CapsuleException; |
||
11 | |||
12 | /** |
||
13 | * Isolates set of table specific operations and schemas into one place. Kinda repository. |
||
14 | */ |
||
15 | final class Capsule implements CapsuleInterface |
||
16 | { |
||
17 | private array $schemas = []; |
||
18 | |||
19 | 432 | public function __construct(private DatabaseInterface $database) |
|
20 | { |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | 312 | public function getDatabase(): DatabaseInterface |
|
27 | { |
||
28 | 312 | return $this->database; |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 8 | public function getTable(string $table): TableInterface |
|
35 | { |
||
36 | 8 | return $this->database->table($table); |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 408 | public function getSchema(string $table): AbstractTable |
|
43 | { |
||
44 | 408 | if (!isset($this->schemas[$table])) { |
|
45 | //We have to declare existed to prevent dropping existed schema |
||
46 | 408 | $this->schemas[$table] = $this->database->table($table)->getSchema(); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
47 | } |
||
48 | |||
49 | 408 | return $this->schemas[$table]; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | * |
||
55 | * @throws \Throwable |
||
56 | */ |
||
57 | 408 | public function execute(array $operations): void |
|
58 | { |
||
59 | 408 | foreach ($operations as $operation) { |
|
60 | 408 | if (!$operation instanceof OperationInterface) { |
|
61 | 8 | throw new CapsuleException( |
|
62 | 8 | sprintf( |
|
63 | 'Migration operation expected to be an instance of `OperationInterface`, `%s` given', |
||
64 | $operation::class |
||
65 | ) |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | 400 | $operation->execute($this); |
|
70 | } |
||
71 | } |
||
72 | } |
||
73 |