|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cycle\Schema\Generator\Migrations; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Migrations\Config\MigrationConfig; |
|
8
|
|
|
use Cycle\Migrations\Migration; |
|
9
|
|
|
use Spiral\Reactor\ClassDeclaration; |
|
10
|
|
|
use Spiral\Reactor\FileDeclaration; |
|
11
|
|
|
use Spiral\Reactor\Partial\PhpNamespace; |
|
12
|
|
|
|
|
13
|
|
|
class MigrationImage |
|
14
|
|
|
{ |
|
15
|
|
|
public string $fileNamePattern = '{database}_{name}'; |
|
16
|
|
|
protected ClassDeclaration $class; |
|
17
|
|
|
protected FileDeclaration $file; |
|
18
|
|
|
protected ?string $database = null; |
|
19
|
|
|
protected string $name = ''; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct( |
|
22
|
|
|
protected MigrationConfig $migrationConfig, |
|
23
|
|
|
string $database, |
|
24
|
|
|
) { |
|
25
|
|
|
$namespace = new PhpNamespace($migrationConfig->getNamespace()); |
|
26
|
|
|
$namespace->addUse(Migration::class); |
|
27
|
|
|
|
|
28
|
|
|
$this->class = $namespace->addClass('newMigration'); |
|
29
|
|
|
$this->class->setExtends(Migration::class); |
|
30
|
|
|
|
|
31
|
|
|
$this->class->addMethod('up')->setPublic()->setReturnType('void'); |
|
32
|
|
|
$this->class->addMethod('down')->setPublic()->setReturnType('void'); |
|
33
|
|
|
|
|
34
|
|
|
$this->file = new FileDeclaration(); |
|
35
|
|
|
$this->file->addNamespace($namespace); |
|
36
|
|
|
|
|
37
|
|
|
$this->setDatabase($database); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getClass(): ClassDeclaration |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->class; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getFile(): FileDeclaration |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->file; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getMigrationConfig(): MigrationConfig |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->migrationConfig; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDatabase(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->database; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setDatabase(string $database): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->database = $database; |
|
63
|
|
|
|
|
64
|
|
|
$className = sprintf( |
|
65
|
|
|
'orm_%s_%s', |
|
66
|
|
|
$database, |
|
67
|
|
|
md5(microtime(true) . microtime(false)), |
|
68
|
|
|
); |
|
69
|
|
|
$this->class->setName($className); |
|
70
|
|
|
|
|
71
|
|
|
if (!$this->class->getConstants()->has('DATABASE')) { |
|
72
|
|
|
$this->class->addConstant('DATABASE', $database)->setProtected(); |
|
73
|
|
|
} |
|
74
|
|
|
$this->class->getConstant('DATABASE')->setValue($database); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function buildFileName(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return str_replace(['{database}', '{name}'], [$this->database, $this->name], $this->fileNamePattern); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getName(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->name; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setName(string $name): void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->name = $name; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|