1 | <?php |
||
13 | class DynamicClosureMethodInvocation extends AbstractMethodInvocation |
||
14 | { |
||
15 | /** |
||
16 | * Closure to use |
||
17 | * |
||
18 | * @var \Closure |
||
19 | */ |
||
20 | protected $closureToCall = null; |
||
21 | |||
22 | /** |
||
23 | * Previous instance/scope of invocation |
||
24 | * |
||
25 | * @var null|object|string |
||
26 | */ |
||
27 | protected $previousInstance = null; |
||
28 | |||
29 | /** |
||
30 | * Invokes original method and return result from it |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | 6 | public function proceed() |
|
35 | { |
||
36 | 6 | if (isset($this->advices[$this->current])) { |
|
37 | /** @var $currentInterceptor \Go\Aop\Intercept\Interceptor */ |
||
38 | 1 | $currentInterceptor = $this->advices[$this->current++]; |
|
39 | |||
40 | 1 | return $currentInterceptor->invoke($this); |
|
41 | } |
||
42 | |||
43 | // Fill the closure only once if it's empty |
||
44 | 6 | if (!$this->closureToCall) { |
|
45 | 6 | $this->closureToCall = $this->reflectionMethod->getClosure($this->instance); |
|
46 | } |
||
47 | |||
48 | // Rebind the closure if instance was changed since last time |
||
49 | 6 | if ($this->previousInstance !== $this->instance) { |
|
50 | 6 | $this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class); |
|
51 | 6 | $this->previousInstance = $this->instance; |
|
52 | } |
||
53 | |||
54 | 6 | $closureToCall = $this->closureToCall; |
|
55 | 6 | $args = $this->arguments; |
|
56 | |||
57 | 6 | switch (count($args)) { |
|
58 | 6 | case 0: |
|
59 | 3 | return $closureToCall(); |
|
60 | 3 | case 1: |
|
61 | 2 | return $closureToCall($args[0]); |
|
62 | 2 | case 2: |
|
63 | 2 | return $closureToCall($args[0], $args[1]); |
|
64 | 1 | case 3: |
|
65 | 1 | return $closureToCall($args[0], $args[1], $args[2]); |
|
66 | 1 | case 4: |
|
67 | 1 | return $closureToCall($args[0], $args[1], $args[2], $args[3]); |
|
68 | 1 | case 5: |
|
69 | 1 | return $closureToCall($args[0], $args[1], $args[2], $args[3], $args[4]); |
|
70 | default: |
||
71 | 1 | return forward_static_call_array($closureToCall, $args); |
|
72 | } |
||
73 | |||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Invokes current method invocation with all interceptors |
||
78 | * |
||
79 | * @param null|object|string $instance Invocation instance (class name for static methods) |
||
80 | * @param array $arguments List of arguments for method invocation |
||
81 | * |
||
82 | * @return mixed Result of invocation |
||
83 | */ |
||
84 | 24 | final public function __invoke($instance = null, array $arguments = []) |
|
108 | } |
||
109 |