1
|
|
|
<?php namespace JSONAPI\Resource\Metadata; |
2
|
|
|
|
3
|
|
|
use JSONAPI\Resource\Attributes\AbstractFieldAttribute; |
|
|
|
|
4
|
|
|
use JSONAPI\Resource\Attributes\Attribute; |
|
|
|
|
5
|
|
|
|
6
|
|
|
use JSONAPI\Resource\Attributes\Relationship; |
|
|
|
|
7
|
|
|
use JSONAPI\Resource\Attributes\Resource as AttrResource; |
|
|
|
|
8
|
|
|
use JSONAPI\Resource\Metadata\Resource as ResourceMeta; |
|
|
|
|
9
|
|
|
use JSONAPI\Resource\Tests\Models\People; |
|
|
|
|
10
|
|
|
use ReflectionAttribute; |
|
|
|
|
11
|
|
|
use ReflectionObject; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
use Reflector; |
15
|
|
|
|
16
|
|
|
class Factory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param object $resource |
20
|
|
|
* @return ResourceMeta |
21
|
|
|
*/ |
22
|
|
|
public function buildResourceMeta(object $resource): ResourceMeta |
23
|
|
|
{ |
24
|
|
|
$ref = new \ReflectionObject($resource); |
25
|
|
|
|
26
|
|
|
/** @var AttrResource[] $attrs */ |
27
|
|
|
$attrs = array_map(fn($attr) => $attr->newInstance(), $ref->getAttributes(AttrResource::class)); |
|
|
|
|
28
|
|
|
|
29
|
|
|
if (empty($attrs)) { |
30
|
|
|
throw new \InvalidArgumentException(sprintf( |
31
|
|
|
'The resource must have "%s" attribute.', |
32
|
|
|
AttrResource::class |
33
|
|
|
)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return new ResourceMeta(current($attrs)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param object $resource |
41
|
|
|
* |
42
|
|
|
* @return Field[] |
43
|
|
|
*/ |
44
|
|
|
public function buildResourceAttributes(object $resource): array |
45
|
|
|
{ |
46
|
|
|
$attributes = []; |
47
|
|
|
|
48
|
|
|
foreach ($this->possibleFieldsReflections($resource) as $ref) { |
49
|
|
|
$refAttributes = $this->buildFieldByAttribute($ref, Attribute::class); |
50
|
|
|
$attributes = array_merge($attributes, $refAttributes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $attributes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param object $resource |
58
|
|
|
* @return Field[] |
59
|
|
|
*/ |
60
|
|
|
public function buildResourceRelationships(object $resource): array |
61
|
|
|
{ |
62
|
|
|
$relationships = []; |
63
|
|
|
|
64
|
|
|
foreach ($this->possibleFieldsReflections($resource) as $ref) { |
65
|
|
|
$refRelationships = $this->buildFieldByAttribute($ref, Relationship::class); |
66
|
|
|
$relationships = array_merge($relationships, $refRelationships); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $relationships; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Find reflection attributes and generate metadata objects from it. |
74
|
|
|
* |
75
|
|
|
* @param ReflectionObject | ReflectionMethod | ReflectionProperty $ref |
76
|
|
|
* @param string $attributeClass |
77
|
|
|
* @return Field[] |
78
|
|
|
*/ |
79
|
|
|
private function buildFieldByAttribute(Reflector $ref, string $attributeClass): array |
80
|
|
|
{ |
81
|
|
|
return array_map( |
82
|
|
|
// Create meta field |
83
|
|
|
fn(AbstractFieldAttribute $field) => new Field($field, $ref), |
|
|
|
|
84
|
|
|
|
85
|
|
|
// Find and convert reflection attribute to attribute instance. |
86
|
|
|
array_map( |
87
|
|
|
fn($attr) => $attr->newInstance(), |
88
|
|
|
$ref->getAttributes($attributeClass, ReflectionAttribute::IS_INSTANCEOF) |
|
|
|
|
89
|
|
|
), |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return reflections of possible places where attribute can be assigned. |
95
|
|
|
* |
96
|
|
|
* @param object $resource |
97
|
|
|
* @return ReflectionObject[] | ReflectionMethod[] | ReflectionProperty[] |
98
|
|
|
*/ |
99
|
|
|
private function possibleFieldsReflections(object $resource): array |
100
|
|
|
{ |
101
|
|
|
$objRef = new ReflectionObject($resource); |
102
|
|
|
|
103
|
|
|
return array_merge( |
104
|
|
|
[$objRef], |
105
|
|
|
$objRef->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC), |
106
|
|
|
$objRef->getProperties(ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PUBLIC), |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
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