1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Console\Commands; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\Console\GeneratorCommand; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Cortex\Foundation\Traits\ConsoleMakeModuleCommand; |
12
|
|
|
|
13
|
|
|
class DataTableMakeCommand extends GeneratorCommand |
14
|
|
|
{ |
15
|
|
|
use ConsoleMakeModuleCommand; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The model name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $modelName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The transformer name. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $transformerName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The console command name. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $name = 'make:datatable'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The console command description. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $description = 'Create a new datatable class'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The type of class being generated. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $type = 'Datatable'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Build the class with the given name. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function buildClass($name) |
60
|
|
|
{ |
61
|
|
|
$stub = parent::buildClass($name); |
62
|
|
|
|
63
|
|
|
$model = $this->option('model') ?? $this->modelName = $this->ask('What is your model?'); |
64
|
|
|
|
65
|
|
|
$transformer = $this->option('transformer') ?? $this->transformerName = $this->ask('What is your transformer?'); |
66
|
|
|
|
67
|
|
|
return $this->replaceClasses($stub, $model, $transformer); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Replace the model and transformer for the given stub. |
72
|
|
|
* |
73
|
|
|
* @param string $stub |
74
|
|
|
* @param string $model |
75
|
|
|
* @param string $transformer |
|
|
|
|
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function replaceClasses($stub, $model, $transformer = null) |
80
|
|
|
{ |
81
|
|
|
if ($transformer) { |
|
|
|
|
82
|
|
|
$transformer = str_replace('/', '\\', $transformer); |
83
|
|
|
|
84
|
|
|
$namespaceTransformer = $this->rootNamespace().'\Transformers\\'.$transformer; |
85
|
|
|
|
86
|
|
|
if (Str::startsWith($transformer, '\\')) { |
87
|
|
|
$stub = str_replace('NamespacedDummyTransformer', trim($transformer, '\\'), $stub); |
88
|
|
|
} else { |
89
|
|
|
$stub = str_replace('NamespacedDummyTransformer', $namespaceTransformer, $stub); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$stub = str_replace( |
93
|
|
|
"use {$namespaceTransformer};\nuse {$namespaceTransformer};", "use {$namespaceTransformer};", $stub |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$transformer = class_basename(trim($transformer, '\\')); |
97
|
|
|
|
98
|
|
|
$stub = str_replace('DummyTransformer', $transformer, $stub); |
99
|
|
|
|
100
|
|
|
$stub = str_replace('dummyTransformer', Str::camel($transformer), $stub); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$model = str_replace('/', '\\', $model); |
104
|
|
|
|
105
|
|
|
$namespaceModel = $this->rootNamespace().'\Models\\'.$model; |
106
|
|
|
|
107
|
|
|
if (Str::startsWith($model, '\\')) { |
108
|
|
|
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); |
109
|
|
|
} else { |
110
|
|
|
$stub = str_replace('NamespacedDummyModel', $namespaceModel, $stub); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$stub = str_replace( |
114
|
|
|
"use {$namespaceModel};\nuse {$namespaceModel};", "use {$namespaceModel};", $stub |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$model = class_basename(trim($model, '\\')); |
118
|
|
|
|
119
|
|
|
$stub = str_replace('DummyModel', $model, $stub); |
120
|
|
|
|
121
|
|
|
return str_replace('dummyModel', Str::camel($model), $stub); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the stub file for the generator. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function getStub() |
130
|
|
|
{ |
131
|
|
|
return __DIR__.'/../../../resources/stubs/datatable.stub'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the default namespace for the class. |
136
|
|
|
* |
137
|
|
|
* @param string $rootNamespace |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getDefaultNamespace($rootNamespace) |
142
|
|
|
{ |
143
|
|
|
return $rootNamespace.'\Datatables'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the console command arguments. |
148
|
|
|
* |
149
|
|
|
* @return array |
|
|
|
|
150
|
|
|
*/ |
151
|
|
|
protected function getArguments() |
152
|
|
|
{ |
153
|
|
|
return [ |
154
|
|
|
['name', InputArgument::REQUIRED, 'The name of the datatable.'], |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the console command arguments. |
160
|
|
|
* |
161
|
|
|
* @return array |
|
|
|
|
162
|
|
|
*/ |
163
|
|
|
protected function getOptions() |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the datatable applies to.'], |
167
|
|
|
['module', 'd', InputOption::VALUE_REQUIRED, 'The module name to generate the file within.'], |
168
|
|
|
['transformer', 't', InputOption::VALUE_REQUIRED, 'The transformer that the datatable applies to.'], |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.