1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Generators\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
|
7
|
|
|
class ModelBackpackCommand extends GeneratorCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The console command name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name = 'backpack:model'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'backpack:model |
22
|
|
|
{name : Name of the model} |
23
|
|
|
{--softdelete : Add soft deletes to the model} |
24
|
|
|
{--nomigration : Disables creating of the migration file}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Generate a backpack templated model and migration'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The type of class being generated. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $type = 'Model'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the stub file for the generator. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
protected function getStub() |
46
|
|
|
{ |
47
|
|
|
if ($this->option('softdelete')) { |
48
|
|
|
return __DIR__.'/../stubs/model-softdelete.stub'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return __DIR__.'/../stubs/model.stub'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the default namespace for the class. |
56
|
|
|
* |
57
|
|
|
* @param string $rootNamespace |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function getDefaultNamespace($rootNamespace) |
62
|
|
|
{ |
63
|
|
|
return $rootNamespace; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Replace the table name for the given stub. |
68
|
|
|
* |
69
|
|
|
* @param string $stub |
70
|
|
|
* @param string $name |
71
|
|
|
* |
72
|
|
|
* @return string |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
protected function replaceTable(&$stub, $name) |
75
|
|
|
{ |
76
|
|
|
$table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_').'s'; |
|
|
|
|
77
|
|
|
|
78
|
|
|
$stub = str_replace('DummyTable', $table, $stub); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Build the class with the given name. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function buildClass($name) |
91
|
|
|
{ |
92
|
|
|
$stub = $this->files->get($this->getStub()); |
93
|
|
|
|
94
|
|
|
return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the console command options. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function getOptions() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
|
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function buildMigration($name) |
110
|
|
|
{ |
111
|
|
|
\Artisan::call('make:migration', [ |
112
|
|
|
'name' => 'create_'.snake_case($name).'_table', |
113
|
|
|
'--create' => camel_case($name), |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
if ($this->option('softdelete')) { |
117
|
|
|
$migration = collect(\File::allFiles('database/migrations'))->last(); |
118
|
|
|
$migrationContent = file_get_contents($migration->getRealPath()); |
119
|
|
|
$newMigrationContent = str_replace("\$table->increments('id');\n", "\$table->increments('id');\n \$table->softDeletes();\n", $migrationContent); |
|
|
|
|
120
|
|
|
file_put_contents($migration->getRealPath(), $newMigrationContent); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function handle() |
125
|
|
|
{ |
126
|
|
|
$name = $this->argument('name'); |
127
|
|
|
|
128
|
|
|
if (! $this->option('nomigration')) { |
129
|
|
|
$this->buildMigration($name); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.