1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen\Transformers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException; |
8
|
|
|
use Rexlabs\Smokescreen\Resource\ResourceInterface; |
9
|
|
|
use Rexlabs\Smokescreen\Transformer\TransformerResolverInterface; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class TransformerResolver implements TransformerResolverInterface |
12
|
|
|
{ |
13
|
|
|
/** @var string|null */ |
14
|
|
|
protected $namespace; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TransformerResolver constructor. |
18
|
|
|
* |
19
|
|
|
* @param string $namespace |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $namespace) |
22
|
|
|
{ |
23
|
|
|
$this->namespace = $namespace; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Determines the Transformer object to be used for a particular resource. |
28
|
|
|
* Inspects the underlying Eloquent model to determine an appropriately |
29
|
|
|
* named transformer class, and instantiate the object. |
30
|
|
|
* |
31
|
|
|
* @inheritdoc |
32
|
|
|
* @throws \Rexlabs\Laravel\Smokescreen\Exceptions\UnresolvedTransformerException |
33
|
|
|
*/ |
34
|
|
|
public function resolve(ResourceInterface $resource) |
35
|
|
|
{ |
36
|
|
|
$transformer = null; |
37
|
|
|
|
38
|
|
|
// Find the underlying model of the resource data |
39
|
|
|
$model = null; |
40
|
|
|
$data = $resource->getData(); |
41
|
|
|
if ($data instanceof Model) { |
42
|
|
|
$model = $data; |
43
|
|
|
} elseif ($data instanceof Collection) { |
44
|
|
|
$model = $data->first(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// If no model can be determined from the data |
48
|
|
|
if ($model !== null) { |
49
|
|
|
// Cool, now let's try to find a matching transformer based on our Model class |
50
|
|
|
// We use our configuration value 'transformer_namespace' to determine where to look. |
51
|
|
|
try { |
52
|
|
|
$transformerClass = sprintf('%s\\%sTransformer', |
53
|
|
|
$this->namespace, |
54
|
|
|
(new \ReflectionClass($model))->getShortName()); |
55
|
|
|
$transformer = resolve($transformerClass); |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
throw new UnresolvedTransformerException('Unable to resolve transformer for model: ' . \get_class($model), |
58
|
|
|
0, $e); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $transformer; |
63
|
|
|
} |
64
|
|
|
} |
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