1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Generators\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
|
7
|
|
View Code Duplication |
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 {name}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Generate a Backpack CRUD model'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The type of class being generated. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $type = 'Model'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the stub file for the generator. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
protected function getStub() |
43
|
|
|
{ |
44
|
|
|
return __DIR__.'/../stubs/crud-model.stub'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the default namespace for the class. |
49
|
|
|
* |
50
|
|
|
* @param string $rootNamespace |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function getDefaultNamespace($rootNamespace) |
55
|
|
|
{ |
56
|
|
|
return $rootNamespace.'\Models'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Replace the table name for the given stub. |
61
|
|
|
* |
62
|
|
|
* @param string $stub |
63
|
|
|
* @param string $name |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function replaceTable(&$stub, $name) |
68
|
|
|
{ |
69
|
|
|
$table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_').'s'; |
70
|
|
|
|
71
|
|
|
$stub = str_replace('DummyTable', $table, $stub); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Build the class with the given name. |
78
|
|
|
* |
79
|
|
|
* @param string $name |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function buildClass($name) |
84
|
|
|
{ |
85
|
|
|
$stub = $this->files->get($this->getStub()); |
86
|
|
|
|
87
|
|
|
return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the console command options. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function getOptions() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
|
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.