1 | <?php |
||
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 |