|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Go! AOP framework |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Go\Aop\Framework; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Dynamic closure method invocation is responsible to call dynamic methods via closure |
|
15
|
|
|
*/ |
|
16
|
|
|
final class DynamicClosureMethodInvocation extends AbstractMethodInvocation |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Closure to use |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Closure |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $closureToCall = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Previous instance of invocation |
|
27
|
|
|
* |
|
28
|
|
|
* @var null|object|string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $previousInstance = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Invokes original method and return result from it |
|
34
|
6 |
|
* |
|
35
|
|
|
* @return mixed |
|
36
|
6 |
|
*/ |
|
37
|
|
|
public function proceed() |
|
38
|
1 |
|
{ |
|
39
|
|
|
if (isset($this->advices[$this->current])) { |
|
40
|
1 |
|
/** @var $currentInterceptor \Go\Aop\Intercept\Interceptor */ |
|
41
|
|
|
$currentInterceptor = $this->advices[$this->current++]; |
|
42
|
|
|
|
|
43
|
|
|
return $currentInterceptor->invoke($this); |
|
44
|
6 |
|
} |
|
45
|
6 |
|
|
|
46
|
|
|
// Fill the closure only once if it's empty |
|
47
|
|
|
if ($this->closureToCall === null) { |
|
48
|
|
|
$this->closureToCall = $this->reflectionMethod->getClosure($this->instance); |
|
49
|
6 |
|
$this->previousInstance = $this->instance; |
|
50
|
6 |
|
} |
|
51
|
6 |
|
|
|
52
|
|
|
// Rebind the closure if instance was changed since last time |
|
53
|
|
|
if ($this->previousInstance !== $this->instance) { |
|
54
|
6 |
|
$this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class); |
|
55
|
6 |
|
$this->previousInstance = $this->instance; |
|
56
|
|
|
} |
|
57
|
6 |
|
|
|
58
|
|
|
$closureToCall = $this->closureToCall; |
|
59
|
3 |
|
|
|
60
|
|
|
return $closureToCall(...$this->arguments); |
|
61
|
2 |
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|