|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cycle\Migrations; |
|
4
|
|
|
|
|
5
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
|
6
|
|
|
use Spiral\Migrations\Migration; |
|
7
|
|
|
use Spiral\Reactor\ClassDeclaration; |
|
8
|
|
|
use Spiral\Reactor\FileDeclaration; |
|
9
|
|
|
|
|
10
|
|
|
class MigrationImage |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var ClassDeclaration */ |
|
13
|
|
|
protected $class; |
|
14
|
|
|
/** @var FileDeclaration */ |
|
15
|
|
|
protected $file; |
|
16
|
|
|
/** @var MigrationConfig */ |
|
17
|
|
|
protected $migrationConfig; |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
protected $database; |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $name; |
|
22
|
|
|
|
|
23
|
|
|
public $fileNamePattern = '{database}_{name}'; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(MigrationConfig $config, string $database) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->migrationConfig = $config; |
|
28
|
|
|
$this->class = new ClassDeclaration('newMigration', 'Migration'); |
|
29
|
|
|
|
|
30
|
|
|
$this->class->method('up')->setPublic(); |
|
31
|
|
|
$this->class->method('down')->setPublic(); |
|
32
|
|
|
|
|
33
|
|
|
$this->file = new FileDeclaration($config->getNamespace()); |
|
34
|
|
|
$this->file->addUse(Migration::class); |
|
35
|
|
|
$this->file->addElement($this->class); |
|
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
|
|
|
$this->class->constant('DATABASE')->setProtected()->setValue($database); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function buildFileName(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return str_replace(['{database}', '{name}'], [$this->database, $this->name], $this->fileNamePattern); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function setName(string $name): void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->name = $name; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|