1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\SerializerBundle\Metadata; |
4
|
|
|
|
5
|
|
|
use Happyr\SerializerBundle\Annotation\SerializerAnnotation; |
6
|
|
|
use Happyr\SerializerBundle\PropertyManager\AttributeExtractor; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Doctrine\Common\Annotations\Reader; |
9
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Read annotations. |
13
|
|
|
* |
14
|
|
|
* @author Tobias Nyholm <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class AnnotationReader implements MetadataReader |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $paths; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Reader |
25
|
|
|
*/ |
26
|
|
|
private $reader; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AttributeExtractor |
30
|
|
|
*/ |
31
|
|
|
private $attributeExtractor; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $paths |
35
|
|
|
* @param Reader $reader |
36
|
|
|
* @param AttributeExtractor $attributeExtractor |
37
|
|
|
*/ |
38
|
23 |
|
public function __construct(array $paths, Reader $reader, AttributeExtractor $attributeExtractor) |
39
|
|
|
{ |
40
|
23 |
|
$this->paths = $paths; |
41
|
23 |
|
$this->reader = $reader; |
42
|
23 |
|
$this->attributeExtractor = $attributeExtractor; |
43
|
23 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return Metadata[] |
47
|
|
|
*/ |
48
|
|
|
public function getMetadata() |
49
|
|
|
{ |
50
|
|
|
// Create a function to filter out our annotations |
51
|
23 |
|
$filterAnnotations = function ($annotation) { |
52
|
23 |
|
return $annotation instanceof SerializerAnnotation; |
53
|
23 |
|
}; |
54
|
|
|
|
55
|
23 |
|
$metadatas = []; |
56
|
23 |
|
foreach ($this->paths as $path) { |
57
|
23 |
|
$finder = new Finder(); |
58
|
23 |
|
$finder->in($path)->notName('*Test.php')->name('*.php'); |
59
|
|
|
/** @var SplFileInfo $file */ |
60
|
23 |
|
foreach ($finder as $file) { |
61
|
23 |
|
if (null === $fqcn = $this->getFullyQualifiedClassName($file->getPathname())) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
23 |
|
$metadata = new Metadata($fqcn); |
65
|
|
|
|
66
|
23 |
|
$attributes = $this->attributeExtractor->getAttributes($fqcn); |
67
|
23 |
|
$reflectionClass = new \ReflectionClass($fqcn); |
68
|
23 |
|
$classAnnotations = array_filter($this->reader->getClassAnnotations($reflectionClass), $filterAnnotations); |
69
|
|
|
|
70
|
23 |
|
$propertyAnnotations = []; |
71
|
23 |
|
foreach ($attributes['property'] as $propertyName => $bool) { |
|
|
|
|
72
|
23 |
|
$propertyAnnotations[$propertyName] = array_filter($this->reader->getPropertyAnnotations($reflectionClass->getProperty($propertyName)), $filterAnnotations); |
73
|
23 |
|
} |
74
|
|
|
|
75
|
23 |
|
$methodAnnotations = []; |
76
|
23 |
|
foreach ($attributes['method'] as $methodName => $bool) { |
|
|
|
|
77
|
23 |
|
$methodAnnotations[$methodName] = array_filter($this->reader->getMethodAnnotations($reflectionClass->getMethod($methodName)), $filterAnnotations); |
78
|
23 |
|
} |
79
|
|
|
|
80
|
23 |
|
if ($this->buildMetadataFromAnnotation($metadata, $classAnnotations, $methodAnnotations, $propertyAnnotations)) { |
81
|
23 |
|
$metadatas[] = $metadata; |
82
|
23 |
|
} |
83
|
23 |
|
} |
84
|
23 |
|
} |
85
|
|
|
|
86
|
23 |
|
return $metadatas; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Build a metadata object from annotations. |
91
|
|
|
* |
92
|
|
|
* @param Metadata $metadata |
93
|
|
|
* @param $classAnnotations |
94
|
|
|
* @param $methodAnnotations |
95
|
|
|
* @param $propertyAnnotations |
96
|
|
|
*/ |
97
|
23 |
|
private function buildMetadataFromAnnotation(Metadata $metadata, $classAnnotations, $methodAnnotations, $propertyAnnotations) |
98
|
|
|
{ |
99
|
23 |
|
$return = false; |
100
|
|
|
$a = [ |
101
|
23 |
|
'setPropertyMetadata' => $propertyAnnotations, |
102
|
23 |
|
'setMethodMetadata' => $methodAnnotations, |
103
|
23 |
|
]; |
104
|
|
|
|
105
|
23 |
|
foreach ($a as $function => $typeAnnotations) { |
106
|
23 |
|
foreach ($typeAnnotations as $name => $annotations) { |
107
|
23 |
|
$data = []; |
108
|
|
|
/** @var SerializerAnnotation $annotation */ |
109
|
23 |
|
foreach ($annotations as $annotation) { |
110
|
23 |
|
$data[$annotation->getName()] = $annotation->getValue(); |
111
|
23 |
|
} |
112
|
|
|
|
113
|
23 |
|
$metadata->$function($name, $data); |
114
|
23 |
|
if (count($data) > 0) { |
115
|
23 |
|
$return = true; |
116
|
23 |
|
} |
117
|
23 |
|
} |
118
|
23 |
|
} |
119
|
|
|
|
120
|
|
|
// Class annotations |
121
|
|
|
/* @var SerializerAnnotation $annotation */ |
122
|
23 |
|
$data = []; |
123
|
23 |
|
foreach ($classAnnotations as $annotation) { |
124
|
23 |
|
$data[$annotation->getName()] = $annotation->getValue(); |
125
|
23 |
|
} |
126
|
23 |
|
$metadata->setClassMetadata($data); |
127
|
23 |
|
if (count($data) > 0) { |
128
|
23 |
|
$return = true; |
129
|
23 |
|
} |
130
|
|
|
|
131
|
23 |
|
return $return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $path |
136
|
|
|
* |
137
|
|
|
* @return null|string |
138
|
|
|
*/ |
139
|
23 |
|
private function getFullyQualifiedClassName($path) |
140
|
|
|
{ |
141
|
23 |
|
$src = file_get_contents($path); |
142
|
23 |
|
$filename = basename($path); |
143
|
23 |
|
$classname = substr($filename, 0, -4); |
144
|
|
|
|
145
|
23 |
|
if (preg_match('|^namespace\s+(.+?);$|sm', $src, $matches)) { |
146
|
23 |
|
return $matches[1].'\\'.$classname; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|