|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Laravel Love. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Anton Komarev <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Love\Support\Database; |
|
15
|
|
|
|
|
16
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
final class MigrationCreator |
|
20
|
|
|
{ |
|
21
|
|
|
private Filesystem $files; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct( |
|
24
|
|
|
Filesystem $files, |
|
25
|
|
|
) { |
|
26
|
|
|
$this->files = $files; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new migration at the given path. |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function create( |
|
35
|
|
|
string $basePath, |
|
36
|
|
|
AddForeignColumnStub $migrationStub, |
|
37
|
|
|
): void { |
|
38
|
|
|
$this->ensureMigrationDoesntAlreadyExist($migrationStub->getClass()); |
|
39
|
|
|
|
|
40
|
|
|
$this->files->put( |
|
41
|
|
|
$this->getPath($basePath, $migrationStub->getFilename()), |
|
42
|
|
|
$migrationStub->getPopulatedContent(), |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Ensure that a migration with the given name doesn't already exist. |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
|
|
private function ensureMigrationDoesntAlreadyExist( |
|
52
|
|
|
string $className, |
|
53
|
|
|
): void { |
|
54
|
|
|
if (class_exists($className)) { |
|
55
|
|
|
// TODO: (?) Throw custom exception? |
|
56
|
|
|
throw new InvalidArgumentException("Migration class `{$className}` already exists."); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the full path to the migration. |
|
62
|
|
|
*/ |
|
63
|
|
|
private function getPath( |
|
64
|
|
|
string $basePath, |
|
65
|
|
|
string $name, |
|
66
|
|
|
): string { |
|
67
|
|
|
return sprintf('%s/%s_%s.php', $basePath, $this->getDatePrefix(), $name); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the date prefix for the migration. |
|
72
|
|
|
*/ |
|
73
|
|
|
private function getDatePrefix(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return date('Y_m_d_His'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|