1
|
|
|
<?php |
2
|
|
|
namespace Caffeinated\Modules\Console\Generators; |
3
|
|
|
|
4
|
|
|
use Caffeinated\Modules\Modules; |
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
|
10
|
|
|
class MakeMigrationCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'make:module:migration'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new module migration file'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array to store the configuration details. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new command instance. |
35
|
|
|
* |
36
|
|
|
* @param Filesystem $files |
37
|
|
|
* @param Modules $module |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Filesystem $files, Modules $module) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
|
43
|
|
|
$this->files = $files; |
44
|
|
|
$this->module = $module; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function fire() |
53
|
|
|
{ |
54
|
|
|
$this->container['slug'] = strtolower($this->argument('slug')); |
55
|
|
|
$this->container['table'] = strtolower($this->argument('table')); |
56
|
|
|
$this->container['migrationName'] = snake_case($this->container['table']); |
57
|
|
|
$this->container['className'] = studly_case($this->container['migrationName']); |
58
|
|
|
|
59
|
|
|
if ($this->module->exists($this->container['slug'])) { |
60
|
|
|
$this->makeFile(); |
61
|
|
|
|
62
|
|
|
$this->info('Create Module Migration: '.$this->getFilename()); |
63
|
|
|
|
64
|
|
|
return exec('composer dump-autoload'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->info('Module does not exist.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a new migration file. |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
protected function makeFile() |
76
|
|
|
{ |
77
|
|
|
return $this->files->put($this->getDestinationFile(), $this->getStubContent()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get file destination. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function getDestinationFile() |
86
|
|
|
{ |
87
|
|
|
return $this->getPath().$this->formatContent($this->getFilename()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get module migration path. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getPath() |
96
|
|
|
{ |
97
|
|
|
$path = $this->module->getModulePath($this->container['slug']); |
98
|
|
|
|
99
|
|
|
return $path.'Database/Migrations/'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the migration filename. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function getFilename() |
108
|
|
|
{ |
109
|
|
|
return date('Y_m_d_His').'_'.$this->container['migrationName'].'.php'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the stub content. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getStubContent() |
118
|
|
|
{ |
119
|
|
|
return $this->formatContent($this->files->get(__DIR__.'/../../../resources/stubs/migration.stub')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Replace placeholder text with correct values. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function formatContent($content) |
128
|
|
|
{ |
129
|
|
|
return str_replace( |
130
|
|
|
['{{migrationName}}', '{{table}}'], |
131
|
|
|
[$this->container['migrationName'], $this->container['table']], |
132
|
|
|
$content |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the console command arguments. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function getArguments() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
['slug', InputArgument::REQUIRED, 'The slug of the module'], |
145
|
|
|
['table', InputArgument::REQUIRED, 'The name of the database table'] |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|