-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
PointcutClassAnnotatedWithFilter.php
148 lines (133 loc) · 4.92 KB
/
PointcutClassAnnotatedWithFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Neos\Flow\Aop\Pointcut;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Aop\Builder\ClassNameIndex;
use Neos\Flow\Reflection\ReflectionService;
use Psr\Log\LoggerInterface;
/**
* A class filter which fires on classes annotated with a certain annotation
*
* @Flow\Proxy(false)
*/
class PointcutClassAnnotatedWithFilter implements PointcutFilterInterface
{
/**
* @var ReflectionService
*/
protected $reflectionService;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var string A regular expression to match annotations
*/
protected $annotation;
/**
* @var array
*/
protected $annotationValueConstraints;
/**
* The constructor - initializes the class annotation filter with the expected annotation class
*
* @param string $annotation An annotation class (for example "@Neos\Flow\Annotations\Aspect") which defines which class annotations should match
* @param array $annotationValueConstraints
*/
public function __construct(string $annotation, array $annotationValueConstraints = [])
{
$this->annotation = $annotation;
$this->annotationValueConstraints = $annotationValueConstraints;
}
/**
* Injects the reflection service
*
* @param ReflectionService $reflectionService The reflection service
* @return void
*/
public function injectReflectionService(ReflectionService $reflectionService): void
{
$this->reflectionService = $reflectionService;
}
/**
* Injects the (system) logger based on PSR-3.
*
* @param LoggerInterface $logger
* @return void
*/
public function injectLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* Checks if the specified class matches with the class tag filter pattern
*
* @param string $className Name of the class to check against
* @param string $methodName Name of the method - not used here
* @param string $methodDeclaringClassName Name of the class the method was originally declared in - not used here
* @param mixed $pointcutQueryIdentifier Some identifier for this query - must at least differ from a previous identifier. Used for circular reference detection.
* @return boolean true if the class matches, otherwise false
*/
public function matches($className, $methodName, $methodDeclaringClassName, $pointcutQueryIdentifier): bool
{
$designatedAnnotations = $this->reflectionService->getClassAnnotations($className, $this->annotation);
if ($designatedAnnotations !== [] || $this->annotationValueConstraints === []) {
$matches = ($designatedAnnotations !== []);
} else {
// It makes no sense to check property values for an annotation that is used multiple times, we shortcut and check the value against the first annotation found.
$firstFoundAnnotation = $designatedAnnotations;
$annotationProperties = $this->reflectionService->getClassPropertyNames($this->annotation);
foreach ($this->annotationValueConstraints as $propertyName => $expectedValue) {
if (!array_key_exists($propertyName, $annotationProperties)) {
$this->logger->notice('The property "' . $propertyName . '" declared in pointcut does not exist in annotation ' . $this->annotation);
return false;
}
if ($firstFoundAnnotation->$propertyName === $expectedValue) {
$matches = true;
} else {
return false;
}
}
}
return $matches;
}
/**
* Returns true if this filter holds runtime evaluations for a previously matched pointcut
*
* @return boolean true if this filter has runtime evaluations
*/
public function hasRuntimeEvaluationsDefinition(): bool
{
return false;
}
/**
* Returns runtime evaluations for the pointcut.
*
* @return array Runtime evaluations
*/
public function getRuntimeEvaluationsDefinition(): array
{
return [];
}
/**
* This method is used to optimize the matching process.
*
* @param ClassNameIndex $classNameIndex
* @return ClassNameIndex
*/
public function reduceTargetClassNames(ClassNameIndex $classNameIndex): ClassNameIndex
{
$classNames = $this->reflectionService->getClassNamesByAnnotation($this->annotation);
$annotatedIndex = new ClassNameIndex();
$annotatedIndex->setClassNames($classNames);
return $classNameIndex->intersect($annotatedIndex);
}
}