1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/* |
5
|
|
|
* Go! AOP framework |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright 2015, 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\Framework; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
|
17
|
|
|
use function get_class; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Dynamic closure method invocation is responsible to call dynamic methods via closure |
21
|
|
|
*/ |
22
|
|
|
final class DynamicClosureMethodInvocation extends AbstractMethodInvocation |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Closure to use |
26
|
|
|
*/ |
27
|
|
|
protected ?Closure $closureToCall = null; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Previous instance of invocation |
31
|
|
|
*/ |
32
|
|
|
protected ?object $previousInstance; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* For dynamic calls we store given argument as 'instance' property |
36
|
|
|
*/ |
37
|
|
|
protected static string $propertyName = 'instance'; |
38
|
7 |
|
|
39
|
|
|
/** |
40
|
7 |
|
* Invokes original method and return result from it |
41
|
1 |
|
*/ |
42
|
|
|
public function proceed() |
43
|
1 |
|
{ |
44
|
|
|
if (isset($this->advices[$this->current])) { |
45
|
|
|
$currentInterceptor = $this->advices[$this->current++]; |
46
|
|
|
|
47
|
7 |
|
return $currentInterceptor->invoke($this); |
48
|
7 |
|
} |
49
|
7 |
|
|
50
|
|
|
// Fill the closure only once if it's empty |
51
|
|
|
if ($this->closureToCall === null) { |
52
|
|
|
$this->closureToCall = $this->reflectionMethod->getClosure($this->instance); |
53
|
7 |
|
$this->previousInstance = $this->instance; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Rebind the closure if instance was changed since last time |
57
|
|
|
if ($this->previousInstance !== $this->instance) { |
58
|
7 |
|
$this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class); |
59
|
|
|
$this->previousInstance = $this->instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return ($this->closureToCall)(...$this->arguments); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns the object for which current joinpoint is invoked |
67
|
|
|
* |
68
|
|
|
* @return object Covariance, always instance of object |
69
|
|
|
*/ |
70
|
|
|
final public function getThis(): object |
71
|
|
|
{ |
72
|
|
|
return $this->instance; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Checks if the current joinpoint is dynamic or static |
77
|
|
|
* |
78
|
|
|
* Dynamic joinpoint contains a reference to an object that can be received via getThis() method call |
79
|
|
|
* |
80
|
|
|
* @see ClassJoinpoint::getThis() |
81
|
|
|
*/ |
82
|
|
|
final public function isDynamic(): bool |
83
|
|
|
{ |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the static scope name (class name) of this joinpoint. |
89
|
|
|
*/ |
90
|
|
|
final public function getScope(): string |
91
|
|
|
{ |
92
|
|
|
// Due to optimization $this->scope won't be filled for each invocation |
93
|
|
|
// However, $this->instance always contains an object, so we can take it's name as a scope name |
94
|
|
|
return get_class($this->instance); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|