1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Generators\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
|
7
|
|
|
class CrudModelBackpackCommand extends GeneratorCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The console command name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name = 'backpack:crud-model'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'backpack:crud-model |
22
|
|
|
{name : Name of the model} |
23
|
|
|
{--nomigration : Disables creating of the migration file}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Generate a Backpack CRUD model and migration'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The type of class being generated. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $type = 'Model'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the stub file for the generator. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
protected function getStub() |
45
|
|
|
{ |
46
|
|
|
return __DIR__.'/../stubs/crud-model.stub'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the default namespace for the class. |
51
|
|
|
* |
52
|
|
|
* @param string $rootNamespace |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function getDefaultNamespace($rootNamespace) |
57
|
|
|
{ |
58
|
|
|
return $rootNamespace.'\Models'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Replace the table name for the given stub. |
63
|
|
|
* |
64
|
|
|
* @param string $stub |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return string |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
protected function replaceTable(&$stub, $name) |
70
|
|
|
{ |
71
|
|
|
$table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_').'s'; |
|
|
|
|
72
|
|
|
|
73
|
|
|
$stub = str_replace('DummyTable', $table, $stub); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Build the class with the given name. |
80
|
|
|
* |
81
|
|
|
* @param string $name |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function buildClass($name) |
86
|
|
|
{ |
87
|
|
|
$stub = $this->files->get($this->getStub()); |
88
|
|
|
|
89
|
|
|
return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the console command options. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
protected function getOptions() |
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
|
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function buildMigration($name) |
105
|
|
|
{ |
106
|
|
|
\Artisan::call('make:migration', [ |
107
|
|
|
'name' => 'create_'.snake_case($name).'_table', |
108
|
|
|
'--create' => camel_case($name), |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function handle() |
113
|
|
|
{ |
114
|
|
|
$name = $this->argument('name'); |
115
|
|
|
|
116
|
|
|
if (! $this->option('nomigration')) { |
117
|
|
|
$this->buildMigration($name); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.