1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Contracts\View\Factory; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class MakeTransformerCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* smokescreen:transformer --for='App\Models\Post' |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'make:transformer {model : The model to transform. e.g. "App\User"}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a new smokescreen transformer class'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The type of class being generated. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $type = 'Transformer'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The view factory. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Contracts\View\Factory |
38
|
|
|
*/ |
39
|
|
|
protected $viewFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $modelClass; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Inject the dependencies. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
50
|
|
|
* @param \Illuminate\Contracts\View\Factory $viewFactory |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
Filesystem $files, |
54
|
|
|
Factory $viewFactory |
55
|
|
|
) { |
56
|
|
|
parent::__construct($files); |
57
|
|
|
|
58
|
|
|
$this->viewFactory = $viewFactory; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function handle() |
65
|
|
|
{ |
66
|
|
|
$this->modelClass = $this->resolveModelClass($this->argument('model')); |
|
|
|
|
67
|
|
|
|
68
|
|
|
parent::handle(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
protected function getStub() |
75
|
|
|
{ |
76
|
|
|
return 'smokescreen::transformer'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Given a model name (or namespace) try to resolve the fully qualified model class |
81
|
|
|
* while checking common model namespaces. |
82
|
|
|
* |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return mixed|null |
86
|
|
|
*/ |
87
|
|
|
protected function resolveModelClass(string $name) |
88
|
|
|
{ |
89
|
|
|
$modelClass = null; |
90
|
|
|
|
91
|
|
|
// If not name-spaced, get a list of classes to search in common model namespaces. |
92
|
|
|
$search = str_contains('\\', $name) ? [$name] : array_map(function ($directory) use ($name) { |
93
|
|
|
return $directory . '\\' . $name; |
94
|
|
|
}, ['App\\Models', 'App\\Model', 'App']); |
95
|
|
|
|
96
|
|
|
// Check for a valid class. |
97
|
|
|
foreach ($search as $class) { |
98
|
|
|
if (class_exists($class)) { |
99
|
|
|
$modelClass = $class; |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// If we didn't find one, exit out. |
105
|
|
|
if ($modelClass === null) { |
106
|
|
|
throw new \InvalidArgumentException("The model [$name] does not exist, please create it first."); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $modelClass; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
|
|
protected function getDefaultNamespace($rootNamespace) |
116
|
|
|
{ |
117
|
|
|
return $this->getTransformerNamespace(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
|
|
protected function buildClass($name) |
124
|
|
|
{ |
125
|
|
|
$modelInspector = new ModelInspector($this->getModel()); |
126
|
|
|
$data = [ |
127
|
|
|
'transformerNamespace' => $this->getTransformerNamespace(), |
128
|
|
|
'transformerName' => $this->getTransformerName(), |
129
|
|
|
'model' => $this->getModel(), |
130
|
|
|
'modelClass' => $this->getModelClass(), |
131
|
|
|
'modelNamespace' => $this->getModelNamespace(), |
132
|
|
|
'modelName' => $this->getModelName(), |
133
|
|
|
'includes' => $modelInspector->getIncludes(), |
134
|
|
|
'properties' => $modelInspector->getDeclaredProperties(), |
135
|
|
|
'defaultProperties' => $modelInspector->getDefaultProperties(), |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return $this->viewFactory->make($this->getStub(), $data) |
139
|
|
|
->render(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
protected function getNameInput() |
146
|
|
|
{ |
147
|
|
|
return $this->getTransformerName(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the transformer class name. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function getTransformerName() |
156
|
|
|
{ |
157
|
|
|
return $this->getModelName() . 'Transformer'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Retrieve the transformer namespace. |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Config\Repository|mixed |
164
|
|
|
*/ |
165
|
|
|
protected function getTransformerNamespace() |
166
|
|
|
{ |
167
|
|
|
return config('smokescreen.transformer_namespace', 'App\Transformers'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Retrieve the transformer class including namespace. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function getTransformerClass() |
176
|
|
|
{ |
177
|
|
|
return $this->getTransformerNamespace() . '\\' . $this->getTransformerName(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Retrieve the model class including namespace. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function getModelClass(): string |
186
|
|
|
{ |
187
|
|
|
return $this->modelClass; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the eloquent model instance. |
192
|
|
|
* |
193
|
|
|
* @return Model |
194
|
|
|
*/ |
195
|
|
|
protected function getModel(): Model |
196
|
|
|
{ |
197
|
|
|
$class = $this->getModelClass(); |
198
|
|
|
|
199
|
|
|
return new $class; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Retrieve the model class name. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
protected function getModelName(): string |
208
|
|
|
{ |
209
|
|
|
return class_basename($this->getModelClass()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the namespace of the model class. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
protected function getModelNamespace() |
218
|
|
|
{ |
219
|
|
|
return $this->getNamespace($this->getModelClass()); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|