|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Generator; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
8
|
|
|
use Doctrine\Migrations\Generator\Exception\InvalidTemplateSpecified; |
|
9
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\MigrationDirectoryHelper; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use function explode; |
|
12
|
|
|
use function file_get_contents; |
|
13
|
|
|
use function file_put_contents; |
|
14
|
|
|
use function implode; |
|
15
|
|
|
use function is_file; |
|
16
|
|
|
use function is_readable; |
|
17
|
|
|
use function preg_match; |
|
18
|
|
|
use function preg_replace; |
|
19
|
|
|
use function sprintf; |
|
20
|
|
|
use function strtr; |
|
21
|
|
|
use function trim; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The Generator class is responsible for generating a migration class. |
|
25
|
|
|
* |
|
26
|
|
|
* @internal |
|
27
|
|
|
*/ |
|
28
|
|
|
class Generator |
|
29
|
|
|
{ |
|
30
|
|
|
private const MIGRATION_TEMPLATE = <<<'TEMPLATE' |
|
31
|
|
|
<?php |
|
32
|
|
|
|
|
33
|
|
|
declare(strict_types=1); |
|
34
|
|
|
|
|
35
|
|
|
namespace <namespace>; |
|
36
|
|
|
|
|
37
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
38
|
|
|
use Doctrine\Migrations\AbstractMigration; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Auto-generated Migration: Please modify to your needs! |
|
42
|
|
|
*/ |
|
43
|
|
|
final class <className> extends AbstractMigration |
|
44
|
|
|
{ |
|
45
|
|
|
public function getDescription() : string |
|
46
|
|
|
{ |
|
47
|
|
|
return ''; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function up(Schema $schema) : void |
|
51
|
|
|
{ |
|
52
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
|
53
|
|
|
<up> |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function down(Schema $schema) : void |
|
57
|
|
|
{ |
|
58
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
|
59
|
|
|
<down> |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
TEMPLATE; |
|
64
|
|
|
|
|
65
|
|
|
/** @var Configuration */ |
|
66
|
|
|
private $configuration; |
|
67
|
|
|
|
|
68
|
|
|
/** @var string|null */ |
|
69
|
|
|
private $template; |
|
70
|
|
|
|
|
71
|
4 |
|
public function __construct(Configuration $configuration) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$this->configuration = $configuration; |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
public function generateMigration( |
|
77
|
|
|
string $fqcn, |
|
78
|
|
|
?string $up = null, |
|
79
|
|
|
?string $down = null |
|
80
|
|
|
) : string { |
|
81
|
4 |
|
$mch = []; |
|
82
|
4 |
|
if (preg_match('~(.*)\\\\([^\\\\]+)~', $fqcn, $mch) === 0) { |
|
83
|
|
|
throw new InvalidArgumentException(sprintf('Invalid FQCN')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
[$fqcn, $namespace, $className] = $mch; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
4 |
|
$dirs = $this->configuration->getMigrationDirectories(); |
|
89
|
4 |
|
if (! isset($dirs[$namespace])) { |
|
90
|
|
|
throw new InvalidArgumentException(sprintf('Path not defined for the namespace "%s"', $namespace)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
$dir = $dirs[$namespace]; |
|
94
|
|
|
|
|
95
|
|
|
$replacements = [ |
|
96
|
4 |
|
'<namespace>' => $namespace, |
|
97
|
4 |
|
'<className>' => $className, |
|
98
|
2 |
|
'<up>' => $up !== null ? ' ' . implode("\n ", explode("\n", $up)) : null, |
|
99
|
2 |
|
'<down>' => $down !== null ? ' ' . implode("\n ", explode("\n", $down)) : null, |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
4 |
|
$code = strtr($this->getTemplate(), $replacements); |
|
103
|
2 |
|
$code = preg_replace('/^ +$/m', '', $code); |
|
104
|
|
|
|
|
105
|
2 |
|
$directoryHelper = new MigrationDirectoryHelper(); |
|
106
|
2 |
|
$dir = $directoryHelper->getMigrationDirectory($this->configuration, $dir); |
|
107
|
2 |
|
$path = $dir . '/' . $className . '.php'; |
|
108
|
|
|
|
|
109
|
2 |
|
file_put_contents($path, $code); |
|
110
|
|
|
|
|
111
|
2 |
|
return $path; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
private function getTemplate() : string |
|
115
|
|
|
{ |
|
116
|
4 |
|
if ($this->template === null) { |
|
117
|
4 |
|
$this->template = $this->loadCustomTemplate(); |
|
118
|
|
|
|
|
119
|
2 |
|
if ($this->template === null) { |
|
120
|
1 |
|
$this->template = self::MIGRATION_TEMPLATE; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
2 |
|
return $this->template; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @throws InvalidTemplateSpecified |
|
129
|
|
|
*/ |
|
130
|
4 |
|
private function loadCustomTemplate() : ?string |
|
131
|
|
|
{ |
|
132
|
4 |
|
$customTemplate = $this->configuration->getCustomTemplate(); |
|
133
|
|
|
|
|
134
|
4 |
|
if ($customTemplate === null) { |
|
135
|
1 |
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
if (! is_file($customTemplate) || ! is_readable($customTemplate)) { |
|
139
|
1 |
|
throw InvalidTemplateSpecified::notFoundOrNotReadable($customTemplate); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
$content = file_get_contents($customTemplate); |
|
143
|
|
|
|
|
144
|
2 |
|
if ($content === false) { |
|
145
|
|
|
throw InvalidTemplateSpecified::notReadable($customTemplate); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
if (trim($content) === '') { |
|
149
|
1 |
|
throw InvalidTemplateSpecified::empty($customTemplate); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
return $content; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.