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