1 | <?php |
||
26 | */ |
||
27 | class PointcutBuilder |
||
28 | { |
||
29 | /** |
||
30 | * Instance of aspect container |
||
31 | */ |
||
32 | protected AspectContainer $container; |
||
|
|||
33 | |||
34 | /** |
||
35 | * Default constructor for the builder |
||
36 | */ |
||
37 | public function __construct(AspectContainer $container) |
||
38 | { |
||
39 | $this->container = $container; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Declares the "Before" hook for specific pointcut expression |
||
44 | */ |
||
45 | public function before(string $pointcutExpression, Closure $adviceToInvoke): void |
||
46 | { |
||
47 | $interceptor = new BeforeInterceptor($adviceToInvoke, 0, $pointcutExpression); |
||
48 | $this->registerAdviceInContainer($pointcutExpression, $interceptor); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Declares the "After" hook for specific pointcut expression |
||
53 | */ |
||
54 | public function after(string $pointcutExpression, Closure $adviceToInvoke): void |
||
55 | { |
||
56 | $interceptor = new AfterInterceptor($adviceToInvoke, 0, $pointcutExpression); |
||
57 | $this->registerAdviceInContainer($pointcutExpression, $interceptor); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Declares the "AfterThrowing" hook for specific pointcut expression |
||
62 | */ |
||
63 | public function afterThrowing(string $pointcutExpression, Closure $adviceToInvoke): void |
||
64 | { |
||
65 | $interceptor = new AfterThrowingInterceptor($adviceToInvoke, 0, $pointcutExpression); |
||
66 | $this->registerAdviceInContainer($pointcutExpression, $interceptor); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Declares the "Around" hook for specific pointcut expression |
||
71 | */ |
||
72 | public function around(string $pointcutExpression, Closure $adviceToInvoke): void |
||
73 | { |
||
74 | $interceptor = new AroundInterceptor($adviceToInvoke, 0, $pointcutExpression); |
||
75 | $this->registerAdviceInContainer($pointcutExpression, $interceptor); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Declares the error message for specific pointcut expression with concrete error level |
||
80 | */ |
||
81 | public function declareError(string $pointcutExpression, string $message, int $errorLevel = E_USER_ERROR): void |
||
82 | { |
||
83 | $interceptor = new DeclareErrorInterceptor($message, $errorLevel, $pointcutExpression); |
||
84 | $this->registerAdviceInContainer($pointcutExpression, $interceptor); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * General method to register advices |
||
89 | */ |
||
90 | private function registerAdviceInContainer(string $pointcutExpression, Advice $adviceToInvoke): void |
||
91 | { |
||
92 | $this->container->registerAdvisor( |
||
93 | new LazyPointcutAdvisor($this->container, $pointcutExpression, $adviceToInvoke), |
||
94 | $this->getPointcutId($pointcutExpression) |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns an unique name for given pointcut expression |
||
100 | */ |
||
101 | private function getPointcutId(string $pointcutExpression): string |
||
102 | { |
||
103 | static $index = 0; |
||
104 | |||
105 | return preg_replace('/\W+/', '_', $pointcutExpression) . '.' . $index++; |
||
106 | } |
||
107 | } |
||
108 |