-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathAndPointcut.php
86 lines (74 loc) · 2.25 KB
/
AndPointcut.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
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\Aop\Pointcut;
use Go\Aop\Pointcut;
use Go\ParserReflection\ReflectionFileNamespace;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionProperty;
/**
* Logical "and" pointcut filter.
*/
final readonly class AndPointcut implements Pointcut
{
/**
* Kind of pointcut
*/
private int $pointcutKind;
/**
* List of Pointcut to combine with "AND"
*
* @var array<Pointcut>
*/
private array $pointcuts;
/**
* And constructor
*/
public function __construct(int $pointcutKind = null)
{
$args = func_get_args();
if (is_array($args[1])) {
$pointcuts = $args[1];
} else {
$pointcuts = array_slice($args, 1);
}
if (count(array_filter($pointcuts, static fn ($pointcut) => $pointcut instanceof Pointcut)) !== count($pointcuts)) {
throw new \Exception('only pointcats allowed to be passed');
}
// If we don't have specified kind, it will be calculated as intersection then
if (!isset($pointcutKind)) {
$pointcutKind = -1;
foreach ($pointcuts as $singlePointcut) {
$pointcutKind &= $singlePointcut->getKind();
}
}
$this->pointcutKind = $pointcutKind;
$this->pointcuts = $pointcuts;
}
public function matches(
ReflectionClass|ReflectionFileNamespace $context,
ReflectionMethod|ReflectionProperty|ReflectionFunction $reflector = null,
object|string $instanceOrScope = null,
array $arguments = null
): bool {
foreach ($this->pointcuts as $singlePointcut) {
if (!$singlePointcut->matches($context, $reflector, $instanceOrScope, $arguments)) {
return false;
}
}
return true;
}
public function getKind(): int
{
return $this->pointcutKind;
}
}