1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelModular\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Mnabialek\LaravelModular\Models\Module; |
7
|
|
|
use Mnabialek\LaravelModular\Traits\Replacer; |
8
|
|
|
use Mnabialek\LaravelModular\Console\Traits\ModuleCreator; |
9
|
|
|
use Mnabialek\LaravelModular\Console\Traits\ModuleVerification; |
10
|
|
|
|
11
|
|
|
class ModuleMakeMigration extends BaseCommand |
12
|
|
|
{ |
13
|
|
|
use ModuleVerification; |
14
|
|
|
use Replacer; |
15
|
|
|
use ModuleCreator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'module:make-migration |
23
|
|
|
{module : Module name} |
24
|
|
|
{name : Migration full name (ex. create_users_table)} |
25
|
|
|
{--type= : Type of migration (default options: create, edit)} |
26
|
|
|
{--table= : Table name (use with --type)} |
27
|
|
|
{--group= : Stub group name that will be used for creating this migration}'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Create migration in selected module'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function proceed() |
40
|
|
|
{ |
41
|
|
|
$module = $this->argument('module'); |
42
|
|
|
$name = $this->argument('name'); |
43
|
|
|
$type = $this->option('type'); |
44
|
|
|
$table = $this->option('table'); |
45
|
|
|
|
46
|
|
|
// verify whether both type and table used |
47
|
|
|
if ($type && !$table || $table && !$type) { |
48
|
|
|
throw new Exception('You need to use both options --type and --table when using any of them'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// verify whether module exists |
52
|
|
|
$modules = $this->verifyExisting(collect((array)$module)); |
53
|
|
|
|
54
|
|
|
$this->createMigrationFile($modules->first(), $name, $type, $table); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create migration file |
59
|
|
|
* |
60
|
|
|
* @param Module $module |
61
|
|
|
* @param string $name |
62
|
|
|
* @param string $type |
63
|
|
|
* @param string $table |
64
|
|
|
* |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
protected function createMigrationFile(Module $module, $name, $type, $table) |
68
|
|
|
{ |
69
|
|
|
$stubGroup = $this->getStubGroup(); |
70
|
|
|
$type = |
71
|
|
|
$type ?: $this->laravel['modular.config']->migrationDefaultType(); |
72
|
|
|
$stubFile = |
73
|
|
|
$this->laravel['modular.config']->migrationStubFileName($type); |
74
|
|
|
|
75
|
|
|
if (!$stubFile) { |
76
|
|
|
throw new Exception("There is no {$type} in module_migrations.types registered in configuration file"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// migration file name |
80
|
|
|
$filename = $this->getMigrationFileName($name); |
81
|
|
|
|
82
|
|
|
// migration class name |
83
|
|
|
$migrationClass = studly_case($name); |
84
|
|
|
|
85
|
|
|
$this->copyStubFileIntoModule($module, $stubFile, $stubGroup, |
86
|
|
|
$module->migrationsPath(true) . DIRECTORY_SEPARATOR . $filename, |
87
|
|
|
['migrationClass' => $migrationClass, 'table' => $table] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->info("[Module {$module->name()}] Created migration file: {$filename}"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get migration file name based on user given migration name |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
protected function getMigrationFileName($name) |
101
|
|
|
{ |
102
|
|
|
return date('Y_m_d_His') . '_' . snake_case($name) . '.php'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.