|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
6
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Responsable; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
15
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
|
16
|
|
|
use Illuminate\Http\JsonResponse; |
|
17
|
|
|
use Illuminate\Http\Request; |
|
18
|
|
|
use Illuminate\Http\Response; |
|
19
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
20
|
|
|
use Illuminate\Support\Arr; |
|
21
|
|
|
use Rexlabs\Laravel\Smokescreen\Pagination\Paginator as PaginatorBridge; |
|
22
|
|
|
use Rexlabs\Laravel\Smokescreen\Relations\RelationLoader; |
|
23
|
|
|
use Rexlabs\Laravel\Smokescreen\Resources\CollectionResource; |
|
24
|
|
|
use Rexlabs\Laravel\Smokescreen\Resources\ItemResource; |
|
25
|
|
|
use Rexlabs\Laravel\Smokescreen\Transformers\TransformerResolver; |
|
26
|
|
|
use Rexlabs\Smokescreen\Exception\MissingResourceException; |
|
27
|
|
|
use Rexlabs\Smokescreen\Helpers\JsonHelper; |
|
28
|
|
|
use Rexlabs\Smokescreen\Relations\RelationLoaderInterface; |
|
29
|
|
|
use Rexlabs\Smokescreen\Resource\Item; |
|
30
|
|
|
use Rexlabs\Smokescreen\Resource\ResourceInterface; |
|
31
|
|
|
use Rexlabs\Smokescreen\Serializer\SerializerInterface; |
|
32
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerInterface; |
|
33
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerResolverInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Smokescreen for Laravel. |
|
37
|
|
|
* Tightly integrates the rexlabs/smokescreen resource transformation library with the Laravel framework. |
|
38
|
|
|
* |
|
39
|
|
|
* @author Jodie Dunlop <[email protected]> |
|
40
|
|
|
* @copyright Rex Software 2018 |
|
41
|
|
|
*/ |
|
42
|
|
|
class Smokescreen implements \JsonSerializable, Jsonable, Arrayable, Responsable |
|
43
|
|
|
{ |
|
44
|
|
|
public const TYPE_ITEM_RESOURCE = 'item'; |
|
45
|
|
|
public const TYPE_COLLECTION_RESOURCE = 'collection'; |
|
46
|
|
|
public const TYPE_AMBIGUOUS_RESOURCE = 'ambiguous'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Rexlabs\Smokescreen\Smokescreen |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $smokescreen; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string|null |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $includes; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string|bool Whether includes should be parsed from a request key |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $autoParseIncludes = true; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var SerializerInterface|null |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $serializer; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var Request|null |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $request; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var Response|null |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $response; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var ResourceInterface|null |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $resource; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $config; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var array |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $injections; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Smokescreen constructor. |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Rexlabs\Smokescreen\Smokescreen $smokescreen |
|
97
|
|
|
* @param array $config |
|
98
|
|
|
*/ |
|
99
|
47 |
|
public function __construct(\Rexlabs\Smokescreen\Smokescreen $smokescreen, array $config = []) |
|
100
|
|
|
{ |
|
101
|
47 |
|
$this->smokescreen = $smokescreen; |
|
102
|
47 |
|
$this->setConfig($config); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Creates a new Smokescreen object. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Rexlabs\Smokescreen\Smokescreen|null $smokescreen |
|
109
|
|
|
* @param array $config |
|
110
|
|
|
* |
|
111
|
|
|
* @return static |
|
112
|
|
|
*/ |
|
113
|
39 |
|
public static function make(\Rexlabs\Smokescreen\Smokescreen $smokescreen = null, array $config = []) |
|
114
|
|
|
{ |
|
115
|
39 |
|
return new static($smokescreen ?? new \Rexlabs\Smokescreen\Smokescreen(), $config); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Set the resource (item or collection) data to be transformed. |
|
120
|
|
|
* You should pass in an instance of a Model. |
|
121
|
|
|
* |
|
122
|
|
|
* @param mixed|Model|array $data |
|
123
|
|
|
* @param callable|TransformerInterface|string|null $transformer |
|
124
|
|
|
* @param string|null $resourceKey |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
127
|
|
|
* |
|
128
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
129
|
|
|
*/ |
|
130
|
18 |
|
public function transform($data, $transformer = null, $resourceKey = null) |
|
131
|
|
|
{ |
|
132
|
18 |
|
switch ($this->determineResourceType($data)) { |
|
133
|
18 |
|
case self::TYPE_ITEM_RESOURCE: |
|
134
|
6 |
|
$this->item($data, $transformer, $resourceKey); |
|
135
|
6 |
|
break; |
|
136
|
12 |
|
case self::TYPE_COLLECTION_RESOURCE: |
|
137
|
11 |
|
$this->collection($data, $transformer, $resourceKey); |
|
138
|
11 |
|
break; |
|
139
|
|
|
default: |
|
140
|
1 |
|
$this->item($data, $transformer, $resourceKey); |
|
141
|
1 |
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
18 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param mixed $data |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
21 |
|
public function determineResourceType($data): string |
|
153
|
|
|
{ |
|
154
|
21 |
|
if ($data instanceof ItemResource) { |
|
155
|
|
|
// Explicitly declared itself as an Item |
|
156
|
2 |
|
return self::TYPE_ITEM_RESOURCE; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
19 |
|
if ($data instanceof CollectionResource) { |
|
160
|
|
|
// Explicitly declared itself as a Collection |
|
161
|
2 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
17 |
|
if ($data instanceof Model) { |
|
165
|
|
|
// Eloquent model treated as an item by default |
|
166
|
5 |
|
return self::TYPE_ITEM_RESOURCE; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
13 |
|
if ($data instanceof Collection) { |
|
170
|
|
|
// Is an instance or extended class of Laravel Support\Collection |
|
171
|
2 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
12 |
|
if ($data instanceof EloquentBuilder || $data instanceof QueryBuilder) { |
|
175
|
|
|
// Treat query builders as a collection |
|
176
|
1 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
12 |
|
if ($data instanceof LengthAwarePaginator) { |
|
180
|
|
|
// Is an instance of Pagination |
|
181
|
2 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
11 |
|
if ($data instanceof HasMany || $data instanceof HasManyThrough || $data instanceof BelongsToMany) { |
|
185
|
|
|
// Many relationships are treated as a collection |
|
186
|
1 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
11 |
|
if ($data instanceof Arrayable) { |
|
190
|
|
|
// Get array data for Arrayable so that we can determine resource type |
|
191
|
1 |
|
$data = $data->toArray(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
11 |
|
if (\is_array($data)) { |
|
195
|
|
|
// Handle plain arrays |
|
196
|
10 |
|
if (Arr::isAssoc($data)) { |
|
197
|
|
|
// Associative arrays are treated as items |
|
198
|
2 |
|
return self::TYPE_ITEM_RESOURCE; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// All other arrays are considered collections |
|
202
|
9 |
|
return self::TYPE_COLLECTION_RESOURCE; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// Everything else is ambiguous resource type |
|
206
|
2 |
|
return self::TYPE_AMBIGUOUS_RESOURCE; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Set an item resource to be transformed. |
|
211
|
|
|
* |
|
212
|
|
|
* @param mixed $data |
|
213
|
|
|
* @param callable|TransformerInterface|string||null $transformer |
|
|
|
|
|
|
214
|
|
|
* @param string|null $resourceKey |
|
215
|
|
|
* |
|
216
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
217
|
|
|
* |
|
218
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
219
|
|
|
*/ |
|
220
|
11 |
|
public function item($data, $transformer = null, $resourceKey = null) |
|
221
|
|
|
{ |
|
222
|
|
|
// Autoload transformer if required |
|
223
|
11 |
|
$transformer = $this->injectTransformer($transformer); |
|
224
|
|
|
|
|
225
|
11 |
|
$this->setResource(new Item($data, $transformer, $resourceKey)); |
|
226
|
|
|
|
|
227
|
11 |
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Set a collection resource to be transformed. |
|
232
|
|
|
* |
|
233
|
|
|
* @param mixed $data |
|
234
|
|
|
* @param callable|TransformerInterface|string|null $transformer |
|
235
|
|
|
* @param string|null $resourceKey |
|
236
|
|
|
* |
|
237
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
238
|
|
|
* |
|
239
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
240
|
|
|
*/ |
|
241
|
21 |
|
public function collection($data, $transformer = null, $resourceKey = null) |
|
242
|
|
|
{ |
|
243
|
|
|
// Autoload transformer if required |
|
244
|
21 |
|
$transformer = $this->injectTransformer($transformer); |
|
245
|
|
|
|
|
246
|
21 |
|
$paginator = null; |
|
247
|
|
|
|
|
248
|
21 |
|
if ($data instanceof LengthAwarePaginator) { |
|
249
|
2 |
|
$paginator = $data; |
|
250
|
2 |
|
$data = $data->getCollection(); |
|
251
|
19 |
|
} elseif ($data instanceof Relation) { |
|
252
|
1 |
|
$data = $data->get(); |
|
253
|
18 |
|
} elseif ($data instanceof EloquentBuilder) { |
|
254
|
1 |
|
$data = $data->get(); |
|
255
|
17 |
|
} elseif ($data instanceof Model) { |
|
256
|
1 |
|
$data = new Collection([$data]); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
// Create a new collection resource |
|
260
|
21 |
|
$resource = new \Rexlabs\Smokescreen\Resource\Collection($data, $transformer, $resourceKey); |
|
261
|
21 |
|
if ($paginator !== null) { |
|
262
|
|
|
// Assign any paginator to the resource |
|
263
|
2 |
|
$resource->setPaginator(new PaginatorBridge($paginator)); |
|
264
|
|
|
} |
|
265
|
21 |
|
$this->setResource($resource); |
|
266
|
|
|
|
|
267
|
21 |
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Resolve transformer from container if it's a class string |
|
272
|
|
|
* |
|
273
|
|
|
* @param callable|TransformerInterface|string|null $transformer |
|
274
|
|
|
* |
|
275
|
|
|
* @return callable|TransformerInterface|string|null |
|
276
|
|
|
*/ |
|
277
|
32 |
|
private function injectTransformer($transformer = null) |
|
278
|
|
|
{ |
|
279
|
32 |
|
if (is_string($transformer)) { |
|
280
|
2 |
|
return app($transformer); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
30 |
|
return $transformer; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Set the transformer used to transform the resource(s). |
|
288
|
|
|
* |
|
289
|
|
|
* @param TransformerInterface|callable|null $transformer |
|
290
|
|
|
* |
|
291
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
292
|
|
|
* |
|
293
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
294
|
|
|
*/ |
|
295
|
2 |
|
public function transformWith($transformer) |
|
296
|
|
|
{ |
|
297
|
2 |
|
if ($this->resource === null) { |
|
298
|
1 |
|
throw new MissingResourceException('Cannot set transformer before setting resource'); |
|
299
|
|
|
} |
|
300
|
1 |
|
$this->resource->setTransformer($transformer); |
|
301
|
|
|
|
|
302
|
1 |
|
return $this; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Set the default serializer to be used for resources which do not have an explictly set serializer. |
|
307
|
|
|
* |
|
308
|
|
|
* @param SerializerInterface|null $serializer |
|
309
|
|
|
* |
|
310
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
311
|
|
|
*/ |
|
312
|
3 |
|
public function serializeWith($serializer) |
|
313
|
|
|
{ |
|
314
|
3 |
|
$this->serializer = $serializer; |
|
315
|
|
|
|
|
316
|
3 |
|
return $this; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Set the relationship loader. |
|
321
|
|
|
* The relationship loader takes the relationships defined on a transformer, and eager-loads them. |
|
322
|
|
|
* |
|
323
|
|
|
* @param RelationLoaderInterface $relationLoader |
|
324
|
|
|
* |
|
325
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
326
|
|
|
*/ |
|
327
|
1 |
|
public function loadRelationsVia(RelationLoaderInterface $relationLoader) |
|
328
|
|
|
{ |
|
329
|
1 |
|
$this->smokescreen->setRelationLoader($relationLoader); |
|
330
|
|
|
|
|
331
|
1 |
|
return $this; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* Sets the resolver to be used for locating transformers for resources. |
|
336
|
|
|
* |
|
337
|
|
|
* @param TransformerResolverInterface $transformerResolver |
|
338
|
|
|
* |
|
339
|
|
|
* @return $this |
|
340
|
|
|
*/ |
|
341
|
1 |
|
public function resolveTransformerVia(TransformerResolverInterface $transformerResolver) |
|
342
|
|
|
{ |
|
343
|
1 |
|
$this->smokescreen->setTransformerResolver($transformerResolver); |
|
344
|
|
|
|
|
345
|
1 |
|
return $this; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* Returns an object representation of the transformed/serialized data. |
|
350
|
|
|
* |
|
351
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException |
|
352
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
353
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
354
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
355
|
|
|
* |
|
356
|
|
|
* @return \stdClass |
|
357
|
|
|
*/ |
|
358
|
1 |
|
public function toObject(): \stdClass |
|
359
|
|
|
{ |
|
360
|
1 |
|
return json_decode($this->toJson(), false); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Outputs a JSON string of the resulting transformed and serialized data. |
|
365
|
|
|
* Implements Laravel's Jsonable interface. |
|
366
|
|
|
* |
|
367
|
|
|
* @param int $options |
|
368
|
|
|
* |
|
369
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
370
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
371
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\JsonEncodeException |
|
372
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
373
|
|
|
* |
|
374
|
|
|
* @return string |
|
375
|
|
|
*/ |
|
376
|
1 |
|
public function toJson($options = 0): string |
|
377
|
|
|
{ |
|
378
|
1 |
|
return JsonHelper::encode($this->jsonSerialize(), $options); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Output the transformed and serialized data as an array. |
|
383
|
|
|
* Implements PHP's JsonSerializable interface. |
|
384
|
|
|
* |
|
385
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
386
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
387
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
388
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\IncludeException |
|
389
|
|
|
* |
|
390
|
|
|
* @return array |
|
391
|
|
|
* |
|
392
|
|
|
* @see Smokescreen::toArray() |
|
393
|
|
|
*/ |
|
394
|
1 |
|
public function jsonSerialize(): array |
|
395
|
|
|
{ |
|
396
|
1 |
|
return $this->toArray(); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Inject some data into the payload under given key (supports dot-notation). |
|
401
|
|
|
* This method can be called multiple times. |
|
402
|
|
|
* |
|
403
|
|
|
* @param string $key |
|
404
|
|
|
* @param mixed $data |
|
405
|
|
|
* |
|
406
|
|
|
* @return $this |
|
407
|
|
|
*/ |
|
408
|
1 |
|
public function inject($key, $data) |
|
409
|
|
|
{ |
|
410
|
1 |
|
$this->injections[$key] = $data; |
|
411
|
|
|
|
|
412
|
1 |
|
return $this; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Output the transformed and serialized data as an array. |
|
417
|
|
|
* This kicks off the transformation via the base Smokescreen object. |
|
418
|
|
|
* |
|
419
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
420
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
421
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
422
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
423
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\IncludeException |
|
424
|
|
|
* |
|
425
|
|
|
* @return array |
|
426
|
|
|
*/ |
|
427
|
18 |
|
public function toArray(): array |
|
428
|
|
|
{ |
|
429
|
|
|
// We must have a resource provided to transform. |
|
430
|
18 |
|
if ($this->resource === null) { |
|
431
|
1 |
|
throw new MissingResourceException('Resource is not defined'); |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
// Assign the resource in the base instance. |
|
435
|
17 |
|
$this->smokescreen->setResource($this->resource); |
|
436
|
|
|
|
|
437
|
|
|
// Serializer may be overridden via config. |
|
438
|
|
|
// We may be setting the serializer to null, in which case a default |
|
439
|
|
|
// will be provided. |
|
440
|
17 |
|
$serializer = $this->serializer ?? null; |
|
441
|
17 |
|
$this->smokescreen->setSerializer($serializer); |
|
442
|
|
|
|
|
443
|
|
|
// Assign any includes. |
|
444
|
17 |
|
if ($this->includes) { |
|
445
|
|
|
// Includes have been set explicitly. |
|
446
|
2 |
|
$this->smokescreen->parseIncludes($this->includes); |
|
447
|
16 |
|
} elseif ($this->autoParseIncludes) { |
|
448
|
|
|
// If autoParseIncludes is not false, then try to parse from the |
|
449
|
|
|
// request object. |
|
450
|
15 |
|
$this->smokescreen->parseIncludes((string) $this->request()->query($this->getIncludeKey())); |
|
451
|
|
|
} else { |
|
452
|
|
|
// Empty includes |
|
453
|
1 |
|
$this->smokescreen->parseIncludes(''); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
// Provide a custom transformer resolver which can interrogate the |
|
457
|
|
|
// underlying model and attempt to resolve a transformer class. |
|
458
|
17 |
|
if ($this->smokescreen->getTransformerResolver() === null) { |
|
459
|
16 |
|
$this->smokescreen->setTransformerResolver( |
|
460
|
16 |
|
new TransformerResolver( |
|
461
|
16 |
|
$this->config['transformer_namespace'] ?? 'App\\Transformers', |
|
462
|
16 |
|
$this->config['transformer_name'] ?? '{ModelName}Transformer' |
|
463
|
16 |
|
) |
|
464
|
16 |
|
); |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
// We will provide the Laravel relationship loader if none has already |
|
468
|
|
|
// been explicitly defined. |
|
469
|
17 |
|
if (!$this->smokescreen->hasRelationLoader()) { |
|
470
|
17 |
|
$this->smokescreen->setRelationLoader(new RelationLoader()); |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
// Kick off the transformation via the Smokescreen base library. |
|
474
|
17 |
|
$data = $this->smokescreen->toArray(); |
|
475
|
16 |
|
if (!empty($this->injections)) { |
|
476
|
1 |
|
foreach ($this->injections as $key => $inject) { |
|
477
|
1 |
|
Arr::set($data, $key, $inject); |
|
478
|
|
|
} |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
16 |
|
return $data; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
/** |
|
485
|
|
|
* Get a Laravel request object. If not set explicitly via setRequest(...) then |
|
486
|
|
|
* it will be automatically resolved out of the container. You're welcome. |
|
487
|
|
|
* |
|
488
|
|
|
* @return \Illuminate\Http\Request |
|
489
|
|
|
*/ |
|
490
|
16 |
|
public function request(): Request |
|
491
|
|
|
{ |
|
492
|
16 |
|
if ($this->request === null) { |
|
493
|
|
|
// Resolve request out of the container. |
|
494
|
15 |
|
$this->request = app('request'); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
16 |
|
return $this->request; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Determine which key is used for the includes when passing from the Request |
|
502
|
|
|
* If the autoParseIncludes property is set to a string value this will be used |
|
503
|
|
|
* otherwise, the 'include_key' from the configuration. |
|
504
|
|
|
* Defaults to 'include'. |
|
505
|
|
|
* |
|
506
|
|
|
* @return string |
|
507
|
|
|
*/ |
|
508
|
18 |
|
public function getIncludeKey(): string |
|
509
|
|
|
{ |
|
510
|
18 |
|
if (\is_string($this->autoParseIncludes)) { |
|
511
|
|
|
// When set to a string value, indicates the include key |
|
512
|
1 |
|
return $this->autoParseIncludes; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
17 |
|
return $this->config['include_key'] ?? 'include'; |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/** |
|
519
|
|
|
* Generates a Response object. |
|
520
|
|
|
* Implements Laravel's Responsable contract, so that you can return smokescreen object from a controller. |
|
521
|
|
|
* |
|
522
|
|
|
* @param \Illuminate\Http\Request $request |
|
523
|
|
|
* |
|
524
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
525
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
526
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
527
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
528
|
|
|
* |
|
529
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response |
|
530
|
|
|
*/ |
|
531
|
1 |
|
public function toResponse($request) |
|
532
|
|
|
{ |
|
533
|
1 |
|
return $this->response(); |
|
534
|
|
|
} |
|
535
|
|
|
|
|
536
|
|
|
/** |
|
537
|
|
|
* Return a JsonResponse object containing the resolved/compiled JSON data. |
|
538
|
|
|
* Note, since the generated Response is cached, consecutive calls to response() will not change the |
|
539
|
|
|
* response based on the given parameters. You can use withResponse($callback) to easily modify the response, |
|
540
|
|
|
* or via $this->response()->setStatusCode() etc. |
|
541
|
|
|
* |
|
542
|
|
|
* @param int $statusCode |
|
543
|
|
|
* @param array $headers |
|
544
|
|
|
* @param int $options |
|
545
|
|
|
* |
|
546
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
547
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
548
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
549
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
550
|
|
|
* |
|
551
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
552
|
|
|
* |
|
553
|
|
|
* @see Smokescreen::toArray() |
|
554
|
|
|
*/ |
|
555
|
4 |
|
public function response(int $statusCode = 200, array $headers = [], int $options = 0): JsonResponse |
|
556
|
|
|
{ |
|
557
|
|
|
// Response will only be generated once. use clearResponse() to clear. |
|
558
|
4 |
|
if ($this->response === null) { |
|
559
|
4 |
|
$this->response = new JsonResponse($this->toArray(), $statusCode, $headers, $options); |
|
|
|
|
|
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
4 |
|
return $this->response; |
|
|
|
|
|
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* Returns a fresh (uncached) response. |
|
567
|
|
|
* See the response() method. |
|
568
|
|
|
* |
|
569
|
|
|
* @param int $statusCode |
|
570
|
|
|
* @param array $headers |
|
571
|
|
|
* @param int $options |
|
572
|
|
|
* |
|
573
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
574
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
575
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
576
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
577
|
|
|
* |
|
578
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
579
|
|
|
* |
|
580
|
|
|
* @see Smokescreen::toArray() |
|
581
|
|
|
* @see Smokescreen::response() |
|
582
|
|
|
*/ |
|
583
|
1 |
|
public function freshResponse(int $statusCode = 200, array $headers = [], int $options = 0): JsonResponse |
|
584
|
|
|
{ |
|
585
|
1 |
|
$this->clearResponse(); |
|
586
|
|
|
|
|
587
|
1 |
|
return $this->response($statusCode, $headers, $options); |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
/** |
|
591
|
|
|
* Clear the cached response object. |
|
592
|
|
|
* |
|
593
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
594
|
|
|
*/ |
|
595
|
32 |
|
public function clearResponse() |
|
596
|
|
|
{ |
|
597
|
32 |
|
$this->response = null; |
|
598
|
|
|
|
|
599
|
32 |
|
return $this; |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* Apply a callback to the response. The response will be generated if it has not already been. |
|
604
|
|
|
* |
|
605
|
|
|
* @param callable $apply |
|
606
|
|
|
* |
|
607
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\UnhandledResourceType |
|
608
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\InvalidTransformerException |
|
609
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
|
610
|
|
|
* @throws \Rexlabs\Smokescreen\Exception\MissingResourceException |
|
611
|
|
|
* |
|
612
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
613
|
|
|
*/ |
|
614
|
1 |
|
public function withResponse(callable $apply) |
|
615
|
|
|
{ |
|
616
|
1 |
|
$apply($this->response()); |
|
617
|
|
|
|
|
618
|
1 |
|
return $this; |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Set the include string. |
|
623
|
|
|
* |
|
624
|
|
|
* @param string|null $includes |
|
625
|
|
|
* |
|
626
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
627
|
|
|
*/ |
|
628
|
2 |
|
public function include($includes) |
|
629
|
|
|
{ |
|
630
|
2 |
|
$this->includes = $includes === null ? $includes : (string) $includes; |
|
631
|
|
|
|
|
632
|
2 |
|
return $this; |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
/** |
|
636
|
|
|
* Disable all includes. |
|
637
|
|
|
* |
|
638
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
639
|
|
|
*/ |
|
640
|
1 |
|
public function noIncludes() |
|
641
|
|
|
{ |
|
642
|
1 |
|
$this->includes = null; |
|
643
|
1 |
|
$this->autoParseIncludes = false; |
|
644
|
|
|
|
|
645
|
1 |
|
return $this; |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* Set the Laravel request object which will be used to resolve parameters. |
|
650
|
|
|
* |
|
651
|
|
|
* @param \Illuminate\Http\Request $request |
|
652
|
|
|
* |
|
653
|
|
|
* @return $this|\Illuminate\Contracts\Support\Responsable |
|
654
|
|
|
*/ |
|
655
|
1 |
|
public function setRequest(Request $request) |
|
656
|
|
|
{ |
|
657
|
1 |
|
$this->request = $request; |
|
658
|
|
|
|
|
659
|
1 |
|
return $this; |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
/** |
|
663
|
|
|
* @return null|ResourceInterface |
|
664
|
|
|
*/ |
|
665
|
17 |
|
public function getResource() |
|
666
|
|
|
{ |
|
667
|
17 |
|
return $this->resource; |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* @param $resource null|ResourceInterface |
|
672
|
|
|
* |
|
673
|
|
|
* @return Smokescreen |
|
674
|
|
|
*/ |
|
675
|
32 |
|
public function setResource($resource) |
|
676
|
|
|
{ |
|
677
|
32 |
|
$this->resource = $resource; |
|
678
|
|
|
|
|
679
|
|
|
// Clear any cached response when the resource changes |
|
680
|
32 |
|
$this->clearResponse(); |
|
681
|
|
|
|
|
682
|
32 |
|
return $this; |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
/** |
|
686
|
|
|
* Get the underlying Smokescreen instance that we are wrapping in our laravel-friendly layer. |
|
687
|
|
|
* |
|
688
|
|
|
* @return \Rexlabs\Smokescreen\Smokescreen |
|
689
|
|
|
*/ |
|
690
|
1 |
|
public function getBaseSmokescreen(): \Rexlabs\Smokescreen\Smokescreen |
|
691
|
|
|
{ |
|
692
|
1 |
|
return $this->smokescreen; |
|
693
|
|
|
} |
|
694
|
|
|
|
|
695
|
|
|
/** |
|
696
|
|
|
* @param array $config |
|
697
|
|
|
*/ |
|
698
|
47 |
|
protected function setConfig(array $config) |
|
699
|
|
|
{ |
|
700
|
47 |
|
if (!empty($config['default_serializer'])) { |
|
701
|
2 |
|
$serializer = $config['default_serializer']; |
|
702
|
2 |
|
if (\is_string($serializer)) { |
|
703
|
|
|
// Given serializer is expected to be a class path |
|
704
|
|
|
// Instantiate via the container |
|
705
|
1 |
|
$serializer = app()->make($serializer); |
|
706
|
|
|
} |
|
707
|
2 |
|
$this->serializeWith($serializer); |
|
708
|
2 |
|
unset($config['default_serializer']); |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
47 |
|
if (!empty($config['default_transformer_resolver'])) { |
|
712
|
2 |
|
$transformerResolver = $config['default_transformer_resolver']; |
|
713
|
2 |
|
if (\is_string($transformerResolver)) { |
|
714
|
|
|
// Given transformer resolver is expected to be a class path |
|
715
|
|
|
// Instantiate via the container |
|
716
|
1 |
|
$transformerResolver = app()->make($transformerResolver); |
|
717
|
|
|
} |
|
718
|
2 |
|
$this->resolveTransformerVia($transformerResolver); |
|
719
|
2 |
|
unset($config['default_transformer_resolver']); |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
47 |
|
$this->config = $config; |
|
723
|
|
|
} |
|
724
|
|
|
} |
|
725
|
|
|
|