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