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 Illuminate\Support\Str; |
18
|
|
|
|
19
|
|
|
final class AddForeignColumnStub |
20
|
|
|
{ |
21
|
|
|
private Filesystem $files; |
22
|
|
|
|
23
|
|
|
private string $table; |
24
|
|
|
|
25
|
|
|
private string $referencedTable; |
26
|
|
|
|
27
|
|
|
private string $foreignColumn; |
28
|
|
|
|
29
|
|
|
private bool $isForeignColumnNullable; |
30
|
|
|
|
31
|
|
|
private string $referencedColumn; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
Filesystem $files, |
35
|
|
|
string $table, |
36
|
|
|
string $referencedTable, |
37
|
|
|
string $foreignColumn, |
38
|
|
|
string $referencedColumn, |
39
|
|
|
bool $isForeignColumnNullable, |
40
|
|
|
) { |
41
|
|
|
$this->files = $files; |
42
|
|
|
$this->table = $table; |
43
|
|
|
$this->referencedTable = $referencedTable; |
44
|
|
|
$this->foreignColumn = $foreignColumn; |
45
|
|
|
$this->referencedColumn = $referencedColumn; |
46
|
|
|
$this->isForeignColumnNullable = $isForeignColumnNullable; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getFilename(): string |
50
|
|
|
{ |
51
|
|
|
return sprintf('add_%s_to_%s_table', $this->foreignColumn, $this->table); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getClass(): string |
55
|
|
|
{ |
56
|
|
|
return Str::studly($this->getFilename()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function getStubsPath(): string |
60
|
|
|
{ |
61
|
|
|
return __DIR__ . '/Stubs'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
66
|
|
|
*/ |
67
|
|
|
private function getStub(): string |
68
|
|
|
{ |
69
|
|
|
$filename = $this->isForeignColumnNullable |
70
|
|
|
? 'AddForeignNullableColumn.stub' |
71
|
|
|
: 'AddForeignColumn.stub'; |
72
|
|
|
|
73
|
|
|
return $this->files->get("{$this->getStubsPath()}/{$filename}"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function getPopulatedContent(): string |
80
|
|
|
{ |
81
|
|
|
$stub = $this->getStub(); |
82
|
|
|
|
83
|
|
|
$stub = str_replace('DummyClass', $this->getClass(), $stub); |
84
|
|
|
$stub = str_replace('DummyTable', $this->table, $stub); |
85
|
|
|
$stub = str_replace('DummyForeignColumn', $this->foreignColumn, $stub); |
86
|
|
|
$stub = str_replace('DummyReferencedTable', $this->referencedTable, $stub); |
87
|
|
|
$stub = str_replace('DummyReferencedColumn', $this->referencedColumn, $stub); |
88
|
|
|
|
89
|
|
|
return $stub; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|