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