|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: root |
|
5
|
|
|
* Date: 29.07.2016 |
|
6
|
|
|
* Time: 21:38. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonframework\container\resolver; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
11
|
|
|
use samsonframework\container\annotation\MetadataInterface; |
|
12
|
|
|
use samsonframework\container\annotation\MethodAnnotation; |
|
13
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
|
14
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Annotation resolver class. |
|
18
|
|
|
*/ |
|
19
|
|
|
class AnnotationResolver extends Resolver |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var AnnotationReader |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $reader; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* AnnotationResolver constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->reader = new AnnotationReader(); |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function resolve($classData, $identifier = null) |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var \ReflectionClass $classData */ |
|
42
|
|
|
|
|
43
|
|
|
// Create and fill class metadata base fields |
|
44
|
1 |
|
$metadata = new ClassMetadata(); |
|
45
|
1 |
|
$metadata->className = $classData->getName(); |
|
46
|
1 |
|
$metadata->internalId = $identifier ?: uniqid(); |
|
47
|
1 |
|
$metadata->name = $metadata->internalId; |
|
48
|
|
|
|
|
49
|
1 |
|
$this->resolveClassAnnotations($classData, $metadata); |
|
50
|
|
|
|
|
51
|
|
|
/** @var \ReflectionMethod $method */ |
|
52
|
1 |
|
foreach ($classData->getMethods() as $method) { |
|
53
|
|
|
$this->resolveMethodAnnotation($method, $metadata); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $metadata; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Resolve all class annotations. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \ReflectionClass $classData |
|
63
|
|
|
* @param ClassMetadata $metadata |
|
64
|
|
|
*/ |
|
65
|
1 |
|
protected function resolveClassAnnotations(\ReflectionClass $classData, ClassMetadata $metadata) |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var MetadataInterface $annotation Read class annotations */ |
|
68
|
1 |
|
foreach ($this->reader->getClassAnnotations($classData) as $annotation) { |
|
69
|
1 |
|
if (class_implements($annotation, MetadataInterface::class)) { |
|
70
|
1 |
|
$annotation->toMetadata($metadata); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Resolve all method annotations. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \ReflectionMethod $method |
|
79
|
|
|
* @param ClassMetadata $metadata |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function resolveMethodAnnotation(\ReflectionMethod $method, ClassMetadata $metadata) |
|
82
|
|
|
{ |
|
83
|
|
|
$methodAnnotations = $this->reader->getMethodAnnotations($method); |
|
84
|
|
|
$methodMetadata = new MethodMetadata(); |
|
85
|
|
|
$methodMetadata->name = $method->getName(); |
|
|
|
|
|
|
86
|
|
|
$methodMetadata->modifiers = $method->getModifiers(); |
|
87
|
|
|
$methodMetadata->parameters = $method->getParameters(); |
|
88
|
|
|
|
|
89
|
|
|
/** @var MethodAnnotation $methodAnnotation */ |
|
90
|
|
|
foreach ($methodAnnotations as $methodAnnotation) { |
|
91
|
|
|
$methodMetadata->options[$methodAnnotation->getMethodAlias()] = $methodAnnotation->convertToMetadata(); |
|
92
|
|
|
} |
|
93
|
|
|
$metadata->methodsMetadata[$method->getName()] = $methodMetadata; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|