1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DataTablesMakeCommand. |
11
|
|
|
* |
12
|
|
|
* @package Yajra\Datatables\Generators |
13
|
|
|
* @author Arjay Angeles <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class DataTablesMakeCommand extends GeneratorCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'datatables:make'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Create a new DataTable service class.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The type of class being generated. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $type = 'DataTable'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The model class to be used by dataTable. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $model; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* DataTable export filename. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $filename; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Build the class with the given name. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function buildClass($name) |
59
|
|
|
{ |
60
|
|
|
$stub = parent::buildClass($name); |
61
|
|
|
|
62
|
|
|
return $this->replaceModelImport($stub) |
63
|
|
|
->replaceModel($stub) |
64
|
|
|
->replaceFilename($stub); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Replace model name. |
69
|
|
|
* |
70
|
|
|
* @param string $stub |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
protected function replaceModel(&$stub) |
74
|
|
|
{ |
75
|
|
|
$model = explode('\\', $this->getModel()); |
76
|
|
|
$model = array_pop($model); |
77
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get model name to use. |
84
|
|
|
*/ |
85
|
|
|
protected function getModel() |
86
|
|
|
{ |
87
|
|
|
$name = $this->getNameInput(); |
88
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
89
|
|
|
$modelNamespace = $this->laravel['config']->get('datatables-buttons.namespace.model'); |
90
|
|
|
|
91
|
|
|
return $this->option('model') |
92
|
|
|
? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . str_singular($name) |
93
|
|
|
: $rootNamespace . "\\User"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Replace model import. |
98
|
|
|
* |
99
|
|
|
* @param string $stub |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
protected function replaceModelImport(&$stub) |
103
|
|
|
{ |
104
|
|
|
$stub = str_replace( |
105
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the stub file for the generator. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function getStub() |
117
|
|
|
{ |
118
|
|
|
return __DIR__ . '/stubs/datatables.stub'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Replace the filename. |
123
|
|
|
* |
124
|
|
|
* @param string $stub |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function replaceFilename(&$stub) |
128
|
|
|
{ |
129
|
|
|
$stub = str_replace( |
130
|
|
|
'DummyFilename', str_slug($this->getNameInput()), $stub |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $stub; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the console command options. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function getOptions() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Parse the name and format according to the root namespace. |
150
|
|
|
* |
151
|
|
|
* @param string $name |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
protected function qualifyClass($name) |
155
|
|
|
{ |
156
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
157
|
|
|
|
158
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
159
|
|
|
return $name; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (Str::contains($name, '/')) { |
163
|
|
|
$name = str_replace('/', '\\', $name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
167
|
|
|
$name .= 'DataTable'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the default namespace for the class. |
175
|
|
|
* |
176
|
|
|
* @param string $rootNamespace |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
protected function getDefaultNamespace($rootNamespace) |
180
|
|
|
{ |
181
|
|
|
return $rootNamespace . "\\" . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|