|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/* |
|
5
|
|
|
* Go! AOP framework |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This source file is subject to the license that is bundled |
|
10
|
|
|
* with this source code in the file LICENSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Go\Aop\Pointcut; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
16
|
|
|
use Go\Aop\Pointcut; |
|
17
|
|
|
use InvalidArgumentException; |
|
18
|
|
|
use ReflectionClass; |
|
19
|
|
|
use ReflectionMethod; |
|
20
|
|
|
use ReflectionProperty; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Annotation property pointcut checks property annotation |
|
24
|
|
|
*/ |
|
25
|
|
|
class AnnotationPointcut implements Pointcut |
|
26
|
|
|
{ |
|
27
|
|
|
use PointcutClassFilterTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Annotation class to match |
|
31
|
|
|
*/ |
|
32
|
|
|
protected string $annotationName; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Annotation reader |
|
36
|
|
|
*/ |
|
37
|
|
|
protected Reader $annotationReader; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Kind of current filter, can be KIND_CLASS, KIND_METHOD, KIND_PROPERTY, KIND_TRAIT |
|
41
|
|
|
*/ |
|
42
|
|
|
protected int $filterKind = 0; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Specifies name of the expected class to receive |
|
46
|
|
|
*/ |
|
47
|
|
|
protected string $expectedClass = ''; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Method to call for annotation reader |
|
51
|
|
|
*/ |
|
52
|
|
|
protected string $annotationMethod = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Static mappings of kind to expected class and method name |
|
56
|
|
|
*/ |
|
57
|
|
|
protected static array $mappings = [ |
|
58
|
|
|
self::KIND_CLASS => [ReflectionClass::class, 'getClassAnnotation'], |
|
59
|
|
|
self::KIND_TRAIT => [ReflectionClass::class, 'getClassAnnotation'], |
|
60
|
|
|
self::KIND_METHOD => [ReflectionMethod::class, 'getMethodAnnotation'], |
|
61
|
|
|
self::KIND_PROPERTY => [ReflectionProperty::class, 'getPropertyAnnotation'] |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Annotation matcher constructor |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $filterKind Kind of filter, e.g. KIND_CLASS |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(int $filterKind, Reader $annotationReader, string $annotationName) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!isset(self::$mappings[$filterKind])) { |
|
72
|
|
|
throw new InvalidArgumentException("Unsupported filter kind {$filterKind}"); |
|
73
|
|
|
} |
|
74
|
|
|
$this->filterKind = $filterKind; |
|
75
|
|
|
$this->annotationName = $annotationName; |
|
76
|
4 |
|
$this->annotationReader = $annotationReader; |
|
77
|
|
|
|
|
78
|
4 |
|
[$this->expectedClass, $this->annotationMethod] = self::$mappings[$filterKind]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
/** |
|
82
|
4 |
|
* {@inheritdoc} |
|
83
|
4 |
|
*/ |
|
84
|
|
|
public function matches($point, $context = null, $instance = null, array $arguments = null): bool |
|
85
|
4 |
|
{ |
|
86
|
4 |
|
$expectedClass = $this->expectedClass; |
|
87
|
|
|
if (!$point instanceof $expectedClass) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
$annotation = $this->annotationReader->{$this->annotationMethod}($point, $this->annotationName); |
|
92
|
|
|
|
|
93
|
1 |
|
return (bool)$annotation; |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the kind of point filter |
|
98
|
1 |
|
*/ |
|
99
|
|
|
public function getKind(): int |
|
100
|
1 |
|
{ |
|
101
|
|
|
return $this->filterKind; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|