|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawilk\LaravelModules\Commands\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
use Rawilk\LaravelModules\Support\Migrations\NameParser; |
|
8
|
|
|
use Rawilk\LaravelModules\Support\Migrations\SchemaParser; |
|
9
|
|
|
use Rawilk\LaravelModules\Support\Stub; |
|
10
|
|
|
use Rawilk\LaravelModules\Traits\ModuleCommands; |
|
11
|
|
|
|
|
12
|
|
|
class MigrationMakeCommand extends GeneratorCommand |
|
13
|
|
|
{ |
|
14
|
|
|
use ModuleCommands; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
protected $argumentName = 'name'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $signature = 'module:make-migration |
|
21
|
|
|
{name : The name of the migration} |
|
22
|
|
|
{module? : The name of the module to create the migration for} |
|
23
|
|
|
{--fields= : The fields to migrate} |
|
24
|
|
|
{--p|plain : Create a plain migration}'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $description = 'Create a new migration for the specified module.'; |
|
28
|
|
|
|
|
29
|
|
|
protected function getClass(): string |
|
30
|
|
|
{ |
|
31
|
|
|
return Str::studly($this->argument('name')); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getDestinationFilePath(): string |
|
35
|
|
|
{ |
|
36
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
37
|
|
|
|
|
38
|
|
|
$generatorPath = GenerateConfigReader::read('migration'); |
|
39
|
|
|
|
|
40
|
|
|
return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getFileName(): string |
|
44
|
|
|
{ |
|
45
|
|
|
return date('Y_m_d_His_') . $this->getSchemaName(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getTemplateContents(): string |
|
49
|
|
|
{ |
|
50
|
|
|
$parser = new NameParser($this->argument('name')); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
if ($parser->isCreate()) { |
|
53
|
|
|
return Stub::create('/migration/create.stub', [ |
|
54
|
|
|
'class' => $this->getClass(), |
|
55
|
|
|
'table' => $parser->getTableName(), |
|
56
|
|
|
'fields' => $this->getSchemaParser()->render() |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($parser->isAdd()) { |
|
61
|
|
|
return Stub::create('/migration/add.stub', [ |
|
62
|
|
|
'class' => $this->getClass(), |
|
63
|
|
|
'table' => $parser->getTableName(), |
|
64
|
|
|
'fields_up' => $this->getSchemaParser()->up(), |
|
65
|
|
|
'fields_down' => $this->getSchemaParser()->down() |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($parser->isDelete()) { |
|
70
|
|
|
return Stub::create('/migration/delete.stub', [ |
|
71
|
|
|
'class' => $this->getClass(), |
|
72
|
|
|
'table' => $parser->getTableName(), |
|
73
|
|
|
'fields_down' => $this->getSchemaParser()->up(), |
|
74
|
|
|
'fields_up' => $this->getSchemaParser()->down() |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($parser->isDrop()) { |
|
79
|
|
|
return Stub::create('/migration/drop.stub', [ |
|
80
|
|
|
'class' => $this->getClass(), |
|
81
|
|
|
'table' => $parser->getTableName(), |
|
82
|
|
|
'fields' => $this->getSchemaParser()->render() |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return Stub::create('/migration/plain.stub', [ |
|
87
|
|
|
'class' => $this->getClass() |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getClassName(): string |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
return Str::studly($this->argument('name')); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function getSchemaName(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->argument('name'); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function getSchemaParser(): SchemaParser |
|
102
|
|
|
{ |
|
103
|
|
|
return new SchemaParser($this->option('fields')); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|