forked from lanfix/goaop-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicMethodsAspect.php
More file actions
62 lines (57 loc) · 1.87 KB
/
DynamicMethodsAspect.php
File metadata and controls
62 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Demo\Aspect;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Attribute\Before;
/**
* Aspect that intercepts specific magic methods, declared with __call and __callStatic
*/
class DynamicMethodsAspect implements Aspect
{
/**
* This advice intercepts an execution of __call methods
*
* Unlike traditional "execution" pointcut, "dynamic" is checking the name of method in
* the runtime, allowing to write interceptors for __call more transparently.
*/
#[Before('dynamic(public Demo\Example\DynamicMethodsDemo->save*(*))')]
public function beforeMagicMethodExecution(MethodInvocation $invocation): void
{
// we need to unpack args from invocation args
[$methodName, $args] = $invocation->getArguments();
echo 'Calling Magic Interceptor for method: ',
$invocation->getScope(),
'->',
$methodName,
'()',
' with arguments: ',
json_encode($args),
PHP_EOL;
}
/**
* This advice intercepts an execution of methods via __callStatic
*/
#[Before('dynamic(public Demo\Example\DynamicMethodsDemo::find*(*))')]
public function beforeMagicStaticMethodExecution(MethodInvocation $invocation): void
{
// we need to unpack args from invocation args
[$methodName, $args] = $invocation->getArguments();
echo 'Calling Static Magic Interceptor for method: ',
$invocation->getScope(),
'::',
$methodName,
'()',
' with arguments: ',
json_encode($args),
PHP_EOL;
}
}