|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Aop; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionClass; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
final class Bind implements BindInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array<string, array<MethodInterceptor>> |
|
13
|
|
|
*/ |
|
14
|
|
|
private $bindings = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var MethodMatch |
|
18
|
|
|
*/ |
|
19
|
|
|
private $methodMatch; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
40 |
|
{ |
|
26
|
|
|
$this->methodMatch = new MethodMatch($this); |
|
27
|
40 |
|
} |
|
28
|
40 |
|
|
|
29
|
|
|
public function __sleep() |
|
30
|
|
|
{ |
|
31
|
|
|
return ['bindings']; |
|
32
|
|
|
} |
|
33
|
31 |
|
|
|
34
|
|
|
/** |
|
35
|
31 |
|
* {@inheritdoc} |
|
36
|
31 |
|
*/ |
|
37
|
|
|
public function bind(string $class, array $pointcuts) : BindInterface |
|
38
|
31 |
|
{ |
|
39
|
|
|
$pointcuts = $this->getAnnotationPointcuts($pointcuts); |
|
40
|
|
|
$class = new ReflectionClass($class); |
|
41
|
|
|
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC); |
|
42
|
|
|
foreach ($methods as $method) { |
|
43
|
|
|
($this->methodMatch)($class, $method, $pointcuts); |
|
44
|
37 |
|
} |
|
45
|
|
|
|
|
46
|
37 |
|
return $this; |
|
|
|
|
|
|
47
|
3 |
|
} |
|
48
|
3 |
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
37 |
|
*/ |
|
52
|
|
|
public function bindInterceptors(string $method, array $interceptors) : BindInterface |
|
53
|
|
|
{ |
|
54
|
|
|
$this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge( |
|
55
|
|
|
$this->bindings[$method], |
|
56
|
|
|
$interceptors |
|
57
|
34 |
|
); |
|
58
|
|
|
|
|
59
|
34 |
|
return $this; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
18 |
|
public function getBindings() : array |
|
66
|
|
|
{ |
|
67
|
18 |
|
return $this->bindings; |
|
68
|
|
|
} |
|
69
|
18 |
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function toString($salt) : string |
|
74
|
|
|
{ |
|
75
|
|
|
unset($salt); |
|
76
|
|
|
|
|
77
|
31 |
|
return serialize($this->bindings); |
|
78
|
|
|
} |
|
79
|
31 |
|
|
|
80
|
31 |
|
/** |
|
81
|
31 |
|
* @param Pointcut[] $pointcuts |
|
82
|
2 |
|
* |
|
83
|
|
|
* @return Pointcut[] |
|
84
|
31 |
|
*/ |
|
85
|
|
|
public function getAnnotationPointcuts(array &$pointcuts) : array |
|
86
|
|
|
{ |
|
87
|
31 |
|
$keyPointcuts = []; |
|
88
|
|
|
foreach ($pointcuts as $key => $pointcut) { |
|
89
|
|
|
if ($pointcut->methodMatcher instanceof AnnotatedMatcher) { |
|
90
|
31 |
|
$key = $pointcut->methodMatcher->annotation; |
|
91
|
|
|
} |
|
92
|
31 |
|
$keyPointcuts[$key] = $pointcut; |
|
93
|
31 |
|
} |
|
94
|
31 |
|
|
|
95
|
|
|
return $keyPointcuts; |
|
96
|
31 |
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: