1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cycle\Migrations; |
6
|
|
|
|
7
|
|
|
use Cycle\Database\Database; |
8
|
|
|
use Cycle\Database\DatabaseInterface; |
9
|
|
|
use Cycle\Migrations\Exception\MigrationException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Simple migration class with shortcut for database and blueprint instances. |
13
|
|
|
*/ |
14
|
|
|
abstract class Migration implements MigrationInterface |
15
|
|
|
{ |
16
|
|
|
// Target migration database |
17
|
|
|
protected const DATABASE = null; |
18
|
|
|
|
19
|
|
|
private ?State $state = null; |
20
|
|
|
private CapsuleInterface $capsule; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
296 |
|
public function getDatabase(): ?string |
26
|
|
|
{ |
27
|
296 |
|
return static::DATABASE; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
304 |
|
public function withCapsule(CapsuleInterface $capsule): MigrationInterface |
34
|
|
|
{ |
35
|
304 |
|
$migration = clone $this; |
36
|
304 |
|
$migration->capsule = $capsule; |
37
|
|
|
|
38
|
304 |
|
return $migration; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
328 |
|
public function withState(State $state): MigrationInterface |
45
|
|
|
{ |
46
|
328 |
|
$migration = clone $this; |
47
|
328 |
|
$migration->state = $state; |
48
|
|
|
|
49
|
328 |
|
return $migration; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
320 |
|
public function getState(): State |
56
|
|
|
{ |
57
|
320 |
|
if (empty($this->state)) { |
58
|
8 |
|
throw new MigrationException('Unable to get migration state, no state are set'); |
59
|
|
|
} |
60
|
|
|
|
61
|
312 |
|
return $this->state; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Database |
66
|
|
|
*/ |
67
|
16 |
|
protected function database(): DatabaseInterface |
68
|
|
|
{ |
69
|
16 |
|
if (empty($this->capsule)) { |
70
|
8 |
|
throw new MigrationException('Unable to get database, no capsule are set'); |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
return $this->capsule->getDatabase(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get table schema builder (blueprint). |
78
|
|
|
*/ |
79
|
304 |
|
protected function table(string $table): TableBlueprint |
80
|
|
|
{ |
81
|
304 |
|
if (empty($this->capsule)) { |
82
|
8 |
|
throw new MigrationException('Unable to get table blueprint, no capsule are set'); |
83
|
|
|
} |
84
|
|
|
|
85
|
296 |
|
return new TableBlueprint($this->capsule, $table); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|