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