Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

PointcutBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 107
ccs 0
cts 41
cp 0
rs 10
c 3
b 1
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A before() 0 5 1
A after() 0 5 1
A afterThrowing() 0 5 1
A around() 0 5 1
A declareError() 0 5 1
A registerAdviceInContainer() 0 7 1
A getPointcutId() 0 6 1
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Aop\Support;
12
13
use Closure;
14
use Go\Aop\Advice;
15
use Go\Aop\Framework\AfterInterceptor;
16
use Go\Aop\Framework\AfterThrowingInterceptor;
17
use Go\Aop\Framework\AroundInterceptor;
18
use Go\Aop\Framework\BeforeInterceptor;
19
use Go\Aop\Framework\DeclareErrorInterceptor;
20
use Go\Core\AspectContainer;
21
22
/**
23
 * Pointcut builder provides simple DSL for declaring pointcuts in plain PHP code
24
 */
25
class PointcutBuilder
26
{
27
    /**
28
     * @var AspectContainer
29
     */
30
    protected $container;
31
32
    /**
33
     * Default constructor for the builder
34
     *
35
     * @param AspectContainer $container Instance of container
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
     * @param string $pointcutExpression Pointcut, e.g. "within(**)"
46
     * @param Closure $advice Advice to call
47
     */
48
    public function before($pointcutExpression, Closure $advice)
49
    {
50
        $advice = new BeforeInterceptor($advice, 0, $pointcutExpression);
51
        $this->registerAdviceInContainer($pointcutExpression, $advice);
52
    }
53
54
    /**
55
     * Declares the "After" hook for specific pointcut expression
56
     *
57
     * @param string $pointcutExpression Pointcut, e.g. "within(**)"
58
     * @param Closure $advice Advice to call
59
     */
60
    public function after($pointcutExpression, Closure $advice)
61
    {
62
        $advice = new AfterInterceptor($advice, 0, $pointcutExpression);
63
        $this->registerAdviceInContainer($pointcutExpression, $advice);
64
    }
65
66
    /**
67
     * Declares the "AfterThrowing" hook for specific pointcut expression
68
     *
69
     * @param string $pointcutExpression Pointcut, e.g. "within(**)"
70
     * @param Closure $advice Advice to call
71
     */
72
    public function afterThrowing($pointcutExpression, Closure $advice)
73
    {
74
        $advice = new AfterThrowingInterceptor($advice, 0, $pointcutExpression);
75
        $this->registerAdviceInContainer($pointcutExpression, $advice);
76
    }
77
78
    /**
79
     * Declares the "Around" hook for specific pointcut expression
80
     *
81
     * @param string $pointcutExpression Pointcut, e.g. "within(**)"
82
     * @param Closure $advice Advice to call
83
     */
84
    public function around($pointcutExpression, Closure $advice)
85
    {
86
        $advice = new AroundInterceptor($advice, 0, $pointcutExpression);
87
        $this->registerAdviceInContainer($pointcutExpression, $advice);
88
    }
89
90
    /**
91
     * Declares the error message for specific pointcut expression
92
     *
93
     * @param string $pointcutExpression Pointcut, e.g. "within(**)"
94
     * @param string $message Error message to show
95
     * @param integer $level Error level to trigger
96
     */
97
    public function declareError($pointcutExpression, $message, $level = E_USER_ERROR)
98
    {
99
        $advice = new DeclareErrorInterceptor($message, $level, $pointcutExpression);
100
        $this->registerAdviceInContainer($pointcutExpression, $advice);
101
    }
102
103
104
    /**
105
     * General method to register advices
106
     *
107
     * @param string $pointcutExpression Pointcut expression string
108
     * @param Advice $advice Instance of advice
109
     */
110
    private function registerAdviceInContainer($pointcutExpression, Advice $advice)
111
    {
112
        $this->container->registerAdvisor(
113
            new LazyPointcutAdvisor($this->container, $pointcutExpression, $advice),
114
            $this->getPointcutId($pointcutExpression)
115
        );
116
    }
117
118
    /**
119
     * Returns a unique name for pointcut expression
120
     *
121
     * @param string $pointcutExpression
122
     *
123
     * @return string
124
     */
125
    private function getPointcutId($pointcutExpression)
126
    {
127
        static $index = 0;
128
129
        return preg_replace('/\W+/', '_', $pointcutExpression) . '.' . $index++;
130
    }
131
}
132