|
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 Closure; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Static closure method invocation is responsible to call static methods via closure |
|
18
|
|
|
*/ |
|
19
|
|
|
final class StaticClosureMethodInvocation extends AbstractMethodInvocation |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Closure to use |
|
23
|
|
|
* |
|
24
|
|
|
* @var Closure |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $closureToCall; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Previous scope of invocation |
|
30
|
|
|
* |
|
31
|
|
|
* @var null|object|string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $previousScope; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Invokes original method and return result from it |
|
37
|
|
|
* |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
13 |
|
public function proceed() |
|
41
|
|
|
{ |
|
42
|
13 |
|
if (isset($this->advices[$this->current])) { |
|
43
|
1 |
|
$currentInterceptor = $this->advices[$this->current++]; |
|
44
|
|
|
|
|
45
|
1 |
|
return $currentInterceptor->invoke($this); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// Rebind the closure if scope (class name) was changed since last time |
|
49
|
13 |
|
if ($this->previousScope !== $this->instance) { |
|
50
|
13 |
|
if ($this->closureToCall === null) { |
|
51
|
13 |
|
$this->closureToCall = $this->getStaticInvoker($this->className, $this->reflectionMethod->name); |
|
52
|
|
|
} |
|
53
|
13 |
|
$this->closureToCall = $this->closureToCall->bindTo(null, $this->instance); |
|
54
|
13 |
|
$this->previousScope = $this->instance; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
13 |
|
return ($this->closureToCall)($this->arguments); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns static method invoker |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $className Class name to forward request |
|
65
|
|
|
* @param string $method Method name to call |
|
66
|
|
|
* |
|
67
|
|
|
* @return Closure |
|
68
|
|
|
*/ |
|
69
|
|
|
protected static function getStaticInvoker($className, $method): Closure |
|
70
|
|
|
{ |
|
71
|
13 |
|
return function(array $args) use ($className, $method) { |
|
|
|
|
|
|
72
|
13 |
|
return forward_static_call_array([$className, $method], $args); |
|
73
|
13 |
|
}; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|