|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata\Driver; |
|
3
|
|
|
|
|
4
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Excluded; |
|
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Includable; |
|
7
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Postable; |
|
8
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Puttable; |
|
9
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\RootResource; |
|
10
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\Annotation\SubResource; |
|
11
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; |
|
12
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
|
13
|
|
|
use Metadata\Driver\DriverInterface; |
|
14
|
|
|
|
|
15
|
|
|
class AnnotationDriver implements DriverInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $reader; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var DriverInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $doctrineDriver; |
|
23
|
|
|
|
|
24
|
34 |
|
public function __construct(Reader $reader, DriverInterface $doctrineDriver) |
|
25
|
|
|
{ |
|
26
|
34 |
|
$this->reader = $reader; |
|
27
|
34 |
|
$this->doctrineDriver = $doctrineDriver; |
|
28
|
34 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
30 |
|
public function loadMetadataForClass(\ReflectionClass $class) |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var ClassMetadata $ddrRestClassMetadata */ |
|
36
|
30 |
|
$ddrRestClassMetadata = $this->doctrineDriver->loadMetadataForClass($class); |
|
37
|
30 |
|
if (null === $ddrRestClassMetadata) { |
|
38
|
|
|
$ddrRestClassMetadata = new ClassMetadata($class->getName()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @var RootResource $restResourceAnnotation */ |
|
42
|
30 |
|
$restResourceAnnotation = $this->reader->getClassAnnotation($class, RootResource::class); |
|
43
|
30 |
|
if (null !== $restResourceAnnotation) { |
|
44
|
|
|
|
|
45
|
28 |
|
$ddrRestClassMetadata->setRestResource(true); |
|
46
|
|
|
|
|
47
|
28 |
|
if (null !== $restResourceAnnotation->namePrefix) { |
|
48
|
|
|
$ddrRestClassMetadata->setNamePrefix($restResourceAnnotation->namePrefix); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
28 |
|
if (null !== $restResourceAnnotation->pathPrefix) { |
|
52
|
24 |
|
$ddrRestClassMetadata->setPathPrefix($restResourceAnnotation->pathPrefix); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
28 |
|
if (null !== $restResourceAnnotation->service) { |
|
56
|
|
|
$ddrRestClassMetadata->setService($restResourceAnnotation->service); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
28 |
|
if (null !== $restResourceAnnotation->controller) { |
|
60
|
|
|
$ddrRestClassMetadata->setController($restResourceAnnotation->controller); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
28 |
|
if (null !== $restResourceAnnotation->methods) { |
|
64
|
28 |
|
$methods = []; |
|
65
|
28 |
|
$methodAnnotations = $restResourceAnnotation->methods; |
|
66
|
28 |
|
foreach ($methodAnnotations as $methodAnnotation) { |
|
67
|
28 |
|
$methods[$methodAnnotation->getName()] = $methodAnnotation; |
|
68
|
|
|
} |
|
69
|
28 |
|
$ddrRestClassMetadata->setMethods($methods); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
28 |
|
if (null !== $restResourceAnnotation->methods) { |
|
73
|
28 |
|
$ddrRestClassMetadata->setMethods($restResourceAnnotation->methods); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
30 |
|
foreach ($class->getProperties() as $reflectionProperty) { |
|
78
|
|
|
|
|
79
|
30 |
|
$propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName()); |
|
80
|
30 |
|
if (null === $propertyMetadata) { |
|
81
|
26 |
|
$propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
30 |
|
$puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class); |
|
85
|
30 |
|
if (null !== $puttableAnnotation) { |
|
86
|
24 |
|
$propertyMetadata->setPuttable(true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
30 |
|
$postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class); |
|
90
|
30 |
|
if (null !== $postableAnnotation) { |
|
91
|
|
|
$propertyMetadata->setPostable(true); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
30 |
|
$includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class); |
|
95
|
30 |
|
if (null !== $includableAnnotation) { |
|
96
|
24 |
|
$paths = $includableAnnotation->paths; |
|
97
|
24 |
|
if (null === $paths) { |
|
98
|
24 |
|
$paths = [$reflectionProperty->name]; |
|
99
|
|
|
} |
|
100
|
24 |
|
$propertyMetadata->setIncludable(true); |
|
101
|
24 |
|
$propertyMetadata->setIncludablePaths($paths); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
30 |
|
$excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class); |
|
105
|
30 |
|
if (null !== $excludedAnnotation) { |
|
106
|
28 |
|
$propertyMetadata->setExcluded(true); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** @var SubResource $subResourceAnnotation */ |
|
110
|
30 |
|
$subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class); |
|
111
|
30 |
|
if (null !== $subResourceAnnotation) { |
|
112
|
|
|
|
|
113
|
24 |
|
$propertyMetadata->setSubResource(true); |
|
114
|
24 |
|
if (null !== $subResourceAnnotation->listRight) { |
|
115
|
|
|
$propertyMetadata->setSubResourceListRight($subResourceAnnotation->listRight); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
24 |
|
if (null !== $subResourceAnnotation->path) { |
|
119
|
|
|
$propertyMetadata->setSubResourcePath($subResourceAnnotation->path); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
24 |
|
if (null !== $subResourceAnnotation->postRight) { |
|
123
|
24 |
|
$propertyMetadata->setSubResourcePostRight($subResourceAnnotation->postRight); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
24 |
|
if (null !== $subResourceAnnotation->putRight) { |
|
127
|
24 |
|
$propertyMetadata->setSubResourcePutRight($subResourceAnnotation->putRight); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
24 |
|
if (null !== $subResourceAnnotation->deleteRight) { |
|
131
|
24 |
|
$propertyMetadata->setSubResourceDeleteRight($subResourceAnnotation->deleteRight); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
30 |
|
$ddrRestClassMetadata->addPropertyMetadata($propertyMetadata); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
30 |
|
return $ddrRestClassMetadata; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: