|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/* |
|
4
|
|
|
* Go! AOP framework |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Go\Aop\Framework; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Go\Aop\Intercept\Joinpoint; |
|
16
|
|
|
use function get_class, is_string; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Interceptor to dynamically trigger an user notice/warning/error on method call |
|
20
|
|
|
* |
|
21
|
|
|
* This interceptor can be used as active replacement for the "deprecated" tag or to notify about |
|
22
|
|
|
* probable issues with specific method. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class DeclareErrorInterceptor extends AbstractInterceptor |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Error message to show for this interceptor |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $message; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Default level of error |
|
36
|
|
|
* |
|
37
|
|
|
* @var int |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $level; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Default constructor for interceptor |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(string $message, int $errorLevel, string $pointcutExpression) |
|
45
|
|
|
{ |
|
46
|
|
|
$adviceMethod = self::getDeclareErrorAdvice(); |
|
47
|
|
|
$this->message = $message; |
|
48
|
|
|
$this->level = $errorLevel; |
|
49
|
|
|
parent::__construct($adviceMethod, -256, $pointcutExpression); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function unserializeAdvice(array $adviceData): Closure |
|
56
|
|
|
{ |
|
57
|
|
|
return self::getDeclareErrorAdvice(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function invoke(Joinpoint $joinpoint) |
|
64
|
|
|
{ |
|
65
|
|
|
$reflection = $joinpoint->getStaticPart(); |
|
66
|
|
|
$reflectorName = 'unknown'; |
|
67
|
|
|
if ($reflection && method_exists($reflection, 'getName')) { |
|
68
|
|
|
$reflectorName = $reflection->getName(); |
|
69
|
|
|
} |
|
70
|
|
|
($this->adviceMethod)($joinpoint->getThis(), $reflectorName, $this->message, $this->level); |
|
71
|
|
|
|
|
72
|
|
|
return $joinpoint->proceed(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns an advice |
|
77
|
|
|
*/ |
|
78
|
|
|
private static function getDeclareErrorAdvice(): Closure |
|
79
|
|
|
{ |
|
80
|
|
|
static $adviceMethod; |
|
81
|
|
|
if (!$adviceMethod) { |
|
82
|
|
|
$adviceMethod = function ($object, $reflectorName, $message, $level = E_USER_NOTICE) { |
|
83
|
|
|
$class = is_string($object) ? $object : get_class($object); |
|
84
|
|
|
$message = vsprintf('[AOP Declare Error]: %s has an error: "%s"', [ |
|
85
|
|
|
$class . '->' . $reflectorName, |
|
86
|
|
|
$message |
|
87
|
|
|
]); |
|
88
|
|
|
trigger_error($message, $level); |
|
89
|
|
|
}; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $adviceMethod; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|