1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class DataTablesEditorCommand extends GeneratorCommand |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'datatables:editor |
16
|
|
|
{name : The name of the dataTable editor.} |
17
|
|
|
{--model : The name given will be used as the model is singular form.} |
18
|
|
|
{--model-namespace= : The namespace of the model to be used.}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a new DataTables Editor class.'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The type of class being generated. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type = 'DataTableEditor'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Build the class with the given name. |
36
|
|
|
* |
37
|
|
|
* @param string $name |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
protected function buildClass($name) |
41
|
|
|
{ |
42
|
|
|
$stub = parent::buildClass($name); |
43
|
|
|
|
44
|
|
|
return $this->replaceModelImport($stub)->replaceModel($stub); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Replace model name. |
49
|
|
|
* |
50
|
|
|
* @param string $stub |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
protected function replaceModel(&$stub) |
54
|
|
|
{ |
55
|
|
|
$model = explode('\\', $this->getModel()); |
56
|
|
|
$model = array_pop($model); |
57
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
58
|
|
|
|
59
|
|
|
return $stub; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get model name to use. |
64
|
|
|
*/ |
65
|
|
|
protected function getModel() |
66
|
|
|
{ |
67
|
|
|
$name = $this->getNameInput(); |
68
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
69
|
|
|
$model = $this->option('model') || $this->option('model-namespace'); |
70
|
|
|
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model'); |
71
|
|
|
|
72
|
|
|
return $model |
73
|
|
|
? $rootNamespace . '\\' . ($modelNamespace ? $modelNamespace . '\\' : '') . Str::singular($name) |
74
|
|
|
: $rootNamespace . '\\User'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Replace model import. |
79
|
|
|
* |
80
|
|
|
* @param string $stub |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
protected function replaceModelImport(&$stub) |
84
|
|
|
{ |
85
|
|
|
$stub = str_replace( |
86
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the stub file for the generator. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
protected function getStub() |
98
|
|
|
{ |
99
|
|
|
$path = $this->laravel['config']->get('datatables-buttons.stub'); |
100
|
|
|
|
101
|
|
|
return $path ? base_path($path) . '/editor.stub' : __DIR__ . '/stubs/editor.stub'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Replace the filename. |
106
|
|
|
* |
107
|
|
|
* @param string $stub |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function replaceFilename(&$stub) |
111
|
|
|
{ |
112
|
|
|
$stub = str_replace( |
113
|
|
|
'DummyFilename', Str::slug($this->getNameInput()), $stub |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return $stub; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parse the name and format according to the root namespace. |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function qualifyClass($name) |
126
|
|
|
{ |
127
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
128
|
|
|
|
129
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
130
|
|
|
return $name; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (Str::contains($name, '/')) { |
134
|
|
|
$name = str_replace('/', '\\', $name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
138
|
|
|
$name .= $this->type; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the default namespace for the class. |
146
|
|
|
* |
147
|
|
|
* @param string $rootNamespace |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function getDefaultNamespace($rootNamespace) |
151
|
|
|
{ |
152
|
|
|
return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|