|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Annotations; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
14
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
15
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class ClassMethodAnnotationReader implements ClassMethodAnnotationReaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Reader |
|
24
|
|
|
*/ |
|
25
|
|
|
private $delegate; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Reader|null $reader |
|
29
|
|
|
* |
|
30
|
|
|
* @throws AnnotationException |
|
31
|
|
|
*/ |
|
32
|
12 |
|
public function __construct(Reader $reader = null) |
|
33
|
|
|
{ |
|
34
|
12 |
|
$this->delegate = $reader ?: new AnnotationReader(); |
|
35
|
12 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Note: Method annotation(s) doesn't replaces class annotation(s), but they are merged. |
|
39
|
|
|
* |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
12 |
|
public function read($class, $method, array $annotationClasses) |
|
43
|
|
|
{ |
|
44
|
12 |
|
$object = new \ReflectionClass($class); |
|
45
|
12 |
|
$method = $object->getMethod($method); |
|
46
|
|
|
|
|
47
|
12 |
|
$classAnnotations = $this->filter($this->delegate->getClassAnnotations($object), $annotationClasses); |
|
48
|
12 |
|
$methodAnnotations = $this->filter($this->delegate->getMethodAnnotations($method), $annotationClasses); |
|
49
|
|
|
|
|
50
|
12 |
|
return array_merge($classAnnotations, $methodAnnotations); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param array $annotations |
|
55
|
|
|
* @param array $classes |
|
56
|
|
|
* |
|
57
|
|
|
* @return array Filtered annotations |
|
58
|
|
|
*/ |
|
59
|
12 |
|
private function filter(array $annotations, array $classes) |
|
60
|
|
|
{ |
|
61
|
12 |
|
$filtered = []; |
|
62
|
|
|
|
|
63
|
12 |
|
foreach ($annotations as $annotation) { |
|
64
|
12 |
|
if (!in_array(get_class($annotation), $classes, true)) { |
|
65
|
11 |
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
12 |
|
$filtered[] = $annotation; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
12 |
|
return $filtered; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|