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