1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2014, 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 Demo\Aspect; |
12
|
|
|
|
13
|
|
|
use Go\Aop\Aspect; |
14
|
|
|
use Go\Aop\Intercept\MethodInvocation; |
15
|
|
|
use Go\Lang\Annotation\Before; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Aspect that intercepts specific magic methods, declared with __call and __callStatic |
19
|
|
|
*/ |
20
|
|
|
class DynamicMethodsAspect implements Aspect |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This advice intercepts an execution of __call methods |
25
|
|
|
* |
26
|
|
|
* Unlike traditional "execution" pointcut, "dynamic" is checking the name of method in |
27
|
|
|
* the runtime, allowing to write interceptors for __call more transparently. |
28
|
|
|
* |
29
|
|
|
* @param MethodInvocation $invocation Invocation |
30
|
|
|
* |
31
|
|
|
* @Before("dynamic(public Demo\Example\DynamicMethodsDemo->save*(*))") |
32
|
|
|
*/ |
33
|
|
|
public function beforeMagicMethodExecution(MethodInvocation $invocation) |
34
|
|
|
{ |
35
|
|
|
$obj = $invocation->getThis(); |
36
|
|
|
|
37
|
|
|
// we need to unpack args from invocation args |
38
|
|
|
list($methodName, $args) = $invocation->getArguments(); |
39
|
|
|
echo 'Calling Magic Interceptor for method: ', |
40
|
|
|
is_object($obj) ? get_class($obj) : $obj, |
41
|
|
|
$invocation->getMethod()->isStatic() ? '::' : '->', |
42
|
|
|
$methodName, |
43
|
|
|
'()', |
44
|
|
|
' with arguments: ', |
45
|
|
|
json_encode($args), |
46
|
|
|
PHP_EOL; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* This advice intercepts an execution of methods via __callStatic |
51
|
|
|
* |
52
|
|
|
* @param MethodInvocation $invocation |
53
|
|
|
* @Before("dynamic(public Demo\Example\DynamicMethodsDemo::find*(*))") |
54
|
|
|
*/ |
55
|
|
|
public function beforeMagicStaticMethodExecution(MethodInvocation $invocation) |
56
|
|
|
{ |
57
|
|
|
// we need to unpack args from invocation args |
58
|
|
|
list($methodName) = $invocation->getArguments(); |
59
|
|
|
|
60
|
|
|
echo "Calling Magic Static Interceptor for method: ", $methodName, PHP_EOL; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|