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 = $this->files->get($this->getStub()); |
61
|
|
|
$stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
62
|
|
|
|
63
|
|
|
return $this->replaceModelImport($stub)->replaceModel($stub)->replaceFilename($stub); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the stub file for the generator. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function getStub() |
72
|
|
|
{ |
73
|
|
|
return __DIR__ . '/stubs/datatables.stub'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Replace model name. |
78
|
|
|
* |
79
|
|
|
* @param string $stub |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
protected function replaceModel(&$stub) |
83
|
|
|
{ |
84
|
|
|
$model = explode('\\', $this->model); |
85
|
|
|
$model = array_pop($model); |
86
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Replace model import. |
93
|
|
|
* |
94
|
|
|
* @param string $stub |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
protected function replaceModelImport(&$stub) |
98
|
|
|
{ |
99
|
|
|
$stub = str_replace( |
100
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->model), $stub |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Replace the filename. |
108
|
|
|
* |
109
|
|
|
* @param string $stub |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function replaceFilename(&$stub) |
113
|
|
|
{ |
114
|
|
|
$stub = str_replace( |
115
|
|
|
'DummyFilename', $this->filename, $stub |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
return $stub; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the console command options. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
protected function getOptions() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null], |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determine if the class already exists. |
135
|
|
|
* |
136
|
|
|
* @param string $rawName |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
protected function alreadyExists($rawName) |
140
|
|
|
{ |
141
|
|
|
$name = $this->parseName($rawName); |
142
|
|
|
|
143
|
|
|
$this->setModel($rawName); |
144
|
|
|
$this->setFilename($rawName); |
145
|
|
|
|
146
|
|
|
return $this->files->exists($this->getPath($name)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Parse the name and format according to the root namespace. |
151
|
|
|
* |
152
|
|
|
* @param string $name |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function parseName($name) |
156
|
|
|
{ |
157
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
158
|
|
|
|
159
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
160
|
|
|
return $name; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (Str::contains($name, '/')) { |
164
|
|
|
$name = str_replace('/', '\\', $name); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
168
|
|
|
$name .= 'DataTable'; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->parseName($this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the default namespace for the class. |
176
|
|
|
* |
177
|
|
|
* @param string $rootNamespace |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function getDefaultNamespace($rootNamespace) |
181
|
|
|
{ |
182
|
|
|
return $rootNamespace . "\\" . $this->laravel['config']->get('datatables.namespace.base', 'DataTables'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Set the model to be used. |
187
|
|
|
* |
188
|
|
|
* @param string $name |
189
|
|
|
*/ |
190
|
|
|
protected function setModel($name) |
191
|
|
|
{ |
192
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
193
|
|
|
$modelNamespace = $this->laravel['config']->get('datatables.namespace.model'); |
194
|
|
|
$this->model = $this->option('model') |
195
|
|
|
? $rootNamespace . "\\" . ($modelNamespace ? $modelNamespace . "\\" : "") . $name |
196
|
|
|
: $rootNamespace . "\\User"; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the filename for export. |
201
|
|
|
* |
202
|
|
|
* @param string $name |
203
|
|
|
*/ |
204
|
|
|
protected function setFilename($name) |
205
|
|
|
{ |
206
|
|
|
$this->filename = Str::lower(Str::plural($name)); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|