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