|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Minime\Annotations; |
|
4
|
|
|
|
|
5
|
|
|
use Minime\Annotations\Cache\ArrayCache; |
|
6
|
|
|
use Minime\Annotations\Interfaces\CacheInterface; |
|
7
|
|
|
use Minime\Annotations\Interfaces\ParserInterface; |
|
8
|
|
|
use Minime\Annotations\Interfaces\ReaderInterface; |
|
9
|
|
|
use Minime\Annotations\Reflector\ReflectionConst; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class is the primary entry point to read annotations |
|
13
|
|
|
* |
|
14
|
|
|
* @package Minime\Annotations |
|
15
|
|
|
*/ |
|
16
|
|
|
class Reader implements ReaderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Minime\Annotations\Interfaces\ParserInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $parser; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Minime\Annotations\Interfaces\CacheInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cache; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param \Minime\Annotations\Interfaces\ParserInterface $parser |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ParserInterface $parser, CacheInterface $cache = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->setParser($parser); |
|
34
|
|
|
$this->setCache($cache); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \Minime\Annotations\Interfaces\CacheInterface $cache Cache handler |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setCache(CacheInterface $cache = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->cache = $cache; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return \Minime\Annotations\Interfaces\CacheInterface Cache handler |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getCache() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->cache; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \Minime\Annotations\Interfaces\ParserInterface $parser |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setParser(ParserInterface $parser) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->parser = $parser; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return \Minime\Annotations\Interfaces\ParserInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getParser() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->parser; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Retrieve all annotations from a given function or closure |
|
71
|
|
|
* |
|
72
|
|
|
* @param mixed $fn Full qualified function name or closure |
|
73
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
74
|
|
|
* @throws \ReflectionException If function is not found |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getFunctionAnnotations($fn) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->getAnnotations(new \ReflectionFunction($fn)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Retrieve all annotations from a given class |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed $class Full qualified class name or object |
|
85
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
86
|
|
|
* @throws \ReflectionException If class is not found |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getClassAnnotations($class) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->getAnnotations(new \ReflectionClass($class)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrieve all annotations from a given property of a class |
|
95
|
|
|
* |
|
96
|
|
|
* @param mixed $class Full qualified class name or object |
|
97
|
|
|
* @param string $property Property name |
|
98
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
99
|
|
|
* @throws \ReflectionException If property is undefined |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getPropertyAnnotations($class, $property) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getAnnotations(new \ReflectionProperty($class, $property)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Retrieve all annotations from a given method of a class |
|
108
|
|
|
* |
|
109
|
|
|
* @param mixed $class Full qualified class name or object |
|
110
|
|
|
* @param string $method Method name |
|
111
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
112
|
|
|
* @throws \ReflectionException If method is undefined |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getMethodAnnotations($class, $method) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getAnnotations(new \ReflectionMethod($class, $method)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Retrieve all annotations from a given constant of a class |
|
121
|
|
|
* |
|
122
|
|
|
* @param string|object $class fully qualified name or instance of the class |
|
123
|
|
|
* @param string $const name of the constant |
|
124
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getConstantAnnotations($class, $const) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getAnnotations(new ReflectionConst($class, $const)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Retrieve annotations from docblock of a given reflector |
|
133
|
|
|
* |
|
134
|
|
|
* @param \Reflector $Reflection Reflector object |
|
135
|
|
|
* @return \Minime\Annotations\Interfaces\AnnotationsBagInterface Annotations collection |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getAnnotations(\Reflector $Reflection) |
|
138
|
|
|
{ |
|
139
|
|
|
$doc = $Reflection->getDocComment(); |
|
|
|
|
|
|
140
|
|
|
if ($this->cache) { |
|
141
|
|
|
$key = $this->cache->getKey($doc); |
|
142
|
|
|
$ast = $this->cache->get($key); |
|
143
|
|
|
if (! $ast) { |
|
144
|
|
|
$ast = $this->parser->parse($doc); |
|
145
|
|
|
$this->cache->set($key, $ast); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
$ast = $this->parser->parse($doc); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return new AnnotationsBag($ast); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Shortcut to create an instance of the default annotations Reader |
|
156
|
|
|
* bundled with the default Parser implementation |
|
157
|
|
|
* |
|
158
|
|
|
* @return \Minime\Annotations\Interfaces\ReaderInterface |
|
159
|
|
|
*/ |
|
160
|
|
|
public static function createFromDefaults() |
|
161
|
|
|
{ |
|
162
|
|
|
return new self(new Parser, new ArrayCache); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: