|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Aop; |
|
6
|
|
|
|
|
7
|
|
|
final class Bind implements BindInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array<string, array<MethodInterceptor>> |
|
11
|
|
|
*/ |
|
12
|
|
|
private $bindings = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var MethodMatch |
|
16
|
|
|
*/ |
|
17
|
|
|
private $methodMatch; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->methodMatch = new MethodMatch($this); |
|
25
|
40 |
|
} |
|
26
|
|
|
|
|
27
|
40 |
|
public function __sleep() |
|
28
|
40 |
|
{ |
|
29
|
|
|
return ['bindings']; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
31 |
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
31 |
|
public function bind(string $class, array $pointcuts) : BindInterface |
|
36
|
31 |
|
{ |
|
37
|
|
|
$pointcuts = $this->getAnnotationPointcuts($pointcuts); |
|
38
|
31 |
|
$class = new \ReflectionClass($class); |
|
39
|
|
|
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC); |
|
40
|
|
|
foreach ($methods as $method) { |
|
41
|
|
|
($this->methodMatch)($class, $method, $pointcuts); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
37 |
|
return $this; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
37 |
|
|
|
47
|
3 |
|
/** |
|
48
|
3 |
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function bindInterceptors(string $method, array $interceptors) : BindInterface |
|
51
|
37 |
|
{ |
|
52
|
|
|
$this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge( |
|
53
|
|
|
$this->bindings[$method], |
|
54
|
|
|
$interceptors |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
34 |
|
return $this; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
34 |
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getBindings() : array |
|
64
|
|
|
{ |
|
65
|
18 |
|
return $this->bindings; |
|
66
|
|
|
} |
|
67
|
18 |
|
|
|
68
|
|
|
/** |
|
69
|
18 |
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function toString($salt) : string |
|
72
|
|
|
{ |
|
73
|
|
|
unset($salt); |
|
74
|
|
|
|
|
75
|
|
|
return strtr(rtrim(base64_encode(pack('H*', sprintf('%u', crc32(serialize($this->bindings))))), '='), '+/', '-_'); |
|
76
|
|
|
} |
|
77
|
31 |
|
|
|
78
|
|
|
/** |
|
79
|
31 |
|
* @param Pointcut[] $pointcuts |
|
80
|
31 |
|
* |
|
81
|
31 |
|
* @return Pointcut[] |
|
82
|
2 |
|
*/ |
|
83
|
|
|
public function getAnnotationPointcuts(array &$pointcuts) : array |
|
84
|
31 |
|
{ |
|
85
|
|
|
$keyPointcuts = []; |
|
86
|
|
|
foreach ($pointcuts as $key => $pointcut) { |
|
87
|
31 |
|
if ($pointcut->methodMatcher instanceof AnnotatedMatcher) { |
|
88
|
|
|
$key = $pointcut->methodMatcher->annotation; |
|
89
|
|
|
} |
|
90
|
31 |
|
$keyPointcuts[$key] = $pointcut; |
|
91
|
|
|
} |
|
92
|
31 |
|
|
|
93
|
31 |
|
return $keyPointcuts; |
|
94
|
31 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.