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 name and signature of the console command. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $signature = 'datatables:make |
23
|
|
|
{name : The name of the datatable.} |
24
|
|
|
{--model : The name of the model to be used.} |
25
|
|
|
{--model-namespace= : The namespace of the model to be used.} |
26
|
|
|
{--action= : The path of the action view.} |
27
|
|
|
{--columns= : The columns of the datatable.}'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The console command description. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $description = 'Create a new DataTable service class.'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The type of class being generated. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $type = 'DataTable'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Build the class with the given name. |
45
|
|
|
* |
46
|
|
|
* @param string $name |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function buildClass($name) |
50
|
|
|
{ |
51
|
|
|
$stub = parent::buildClass($name); |
52
|
|
|
|
53
|
|
|
return $this->replaceModelImport($stub) |
54
|
|
|
->replaceModel($stub) |
55
|
|
|
->replaceColumns($stub) |
56
|
|
|
->replaceAction($stub) |
57
|
|
|
->replaceFilename($stub); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Replace model name. |
62
|
|
|
* |
63
|
|
|
* @param string $stub |
64
|
|
|
* @return this |
65
|
|
|
*/ |
66
|
|
|
protected function replaceModel(&$stub) |
67
|
|
|
{ |
68
|
|
|
$model = explode('\\', $this->getModel()); |
69
|
|
|
$model = array_pop($model); |
70
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get model name to use. |
77
|
|
|
*/ |
78
|
|
|
protected function getModel() |
79
|
|
|
{ |
80
|
|
|
$name = $this->getNameInput(); |
81
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
|
|
|
|
82
|
|
|
$model = $this->option('model') || $this->option('model-namespace'); |
83
|
|
|
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model'); |
84
|
|
|
|
85
|
|
|
return $model |
86
|
|
|
? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . str_singular($name) |
87
|
|
|
: $rootNamespace . "\\User"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Replace columns. |
92
|
|
|
* |
93
|
|
|
* @param string $stub |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
protected function replaceColumns(&$stub) |
97
|
|
|
{ |
98
|
|
|
$stub = str_replace( |
99
|
|
|
'DummyColumns', $this->getColumns(), $stub |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the columns to be used. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function getColumns() |
111
|
|
|
{ |
112
|
|
|
if($this->option('columns') != ''){ |
113
|
|
|
return $this->parseArray($this->option('columns')); |
|
|
|
|
114
|
|
|
}else{ |
115
|
|
|
return $this->parseArray('id,add your columns,created_at,updated_at'); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parse array from definition |
121
|
|
|
* |
122
|
|
|
* @param string $definition |
123
|
|
|
* @param string $delimiter |
124
|
|
|
* @param int $indentation |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function parseArray($definition, $delimiter = ',', $indentation = 12) |
128
|
|
|
{ |
129
|
|
|
return str_replace($delimiter, "',\n" . str_repeat(' ', $indentation) . "'", $definition); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Replace the action. |
134
|
|
|
* |
135
|
|
|
* @param string $stub |
136
|
|
|
* @return this |
137
|
|
|
*/ |
138
|
|
|
protected function replaceAction(&$stub) |
139
|
|
|
{ |
140
|
|
|
$stub = str_replace( |
141
|
|
|
'DummyAction', $this->getAction(), $stub |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Set the action view to be used. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function getAction() |
153
|
|
|
{ |
154
|
|
|
return $this->option('action') ? $this->option('action') : Str::lower($this->getNameInput()) . '.action'; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Replace model import. |
160
|
|
|
* |
161
|
|
|
* @param string $stub |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
protected function replaceModelImport(&$stub) |
165
|
|
|
{ |
166
|
|
|
$stub = str_replace( |
167
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the stub file for the generator. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
protected function getStub() |
179
|
|
|
{ |
180
|
|
|
$config = $this->laravel['config']; |
181
|
|
|
return $config->get('datatables-buttons.stub') |
182
|
|
|
? base_path() . $config->get('datatables-buttons.stub') . '/datatables.stub' |
183
|
|
|
: __DIR__ . '/stubs/datatables.stub'; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Replace the filename. |
188
|
|
|
* |
189
|
|
|
* @param string $stub |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
protected function replaceFilename(&$stub) |
193
|
|
|
{ |
194
|
|
|
$stub = str_replace( |
195
|
|
|
'DummyFilename', str_slug($this->getNameInput()), $stub |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
return $stub; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get the console command options. |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function getOptions() |
207
|
|
|
{ |
208
|
|
|
return [ |
209
|
|
|
['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null], |
210
|
|
|
['action', null, InputOption::VALUE_OPTIONAL, 'Path to action column template.', null], |
211
|
|
|
['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null], |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Parse the name and format according to the root namespace. |
217
|
|
|
* |
218
|
|
|
* @param string $name |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
protected function qualifyClass($name) |
222
|
|
|
{ |
223
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
|
|
|
|
224
|
|
|
|
225
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
226
|
|
|
return $name; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (Str::contains($name, '/')) { |
230
|
|
|
$name = str_replace('/', '\\', $name); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
234
|
|
|
$name .= 'DataTable'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get the default namespace for the class. |
242
|
|
|
* |
243
|
|
|
* @param string $rootNamespace |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
protected function getDefaultNamespace($rootNamespace) |
247
|
|
|
{ |
248
|
|
|
return $rootNamespace . "\\" . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.