Completed
Pull Request — 1.x (#301)
by
unknown
04:37
created

AbstractJoinpoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2011, 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\Framework;
12
13
use Go\Aop\Advice;
14
use Go\Aop\AdviceAfter;
15
use Go\Aop\AdviceBefore;
16
use Go\Aop\AdviceAround;
17
use Go\Aop\Intercept\Joinpoint;
18
19
/**
20
 *  Abstract joinpoint for framework
21
 *
22
 * Join points are points in the execution of the system, such as method calls,
23
 * where behavior supplied by aspects is combined. A join point is a point in
24
 * the execution of the program, which is used to define the dynamic structure
25
 * of a crosscutting concern.
26
 *
27
 * @link http://en.wikipedia.org/wiki/Aspect-oriented_software_development#Join_point_model
28
 */
29
abstract class AbstractJoinpoint implements Joinpoint
30
{
31
    /**
32
     * List of advices
33
     *
34
     * @var array|Advice[]
35
     */
36
    protected $advices = [];
37
38
    /**
39
     * Current advice index
40
     *
41
     * @var int
42
     */
43
    protected $current = 0;
44
45
    /**
46
     * Stack frames to work with recursive calls or with cross-calls inside object
47
     *
48
     * @var array
49
     */
50
    protected $stackFrames = [];
51
52
    /**
53
     * Recursion level for invocation
54
     *
55
     * @var int
56
     */
57
    protected $level = 0;
58
59
    /**
60
     * Initializes list of advices for current joinpoint
61
     *
62
     * @param array $advices List of advices
63
     */
64
    public function __construct(array $advices)
65
    {
66
        $this->advices = $advices;
67
    }
68
69
    /**
70
     * Sorts advices by priority
71
     *
72
     * @param array|Advice[] $advices
73
     * @return array|Advice[] Sorted list of advices
74
     */
75
    public static function sortAdvices(array $advices)
76
    {
77
        $sortedAdvices = $advices;
78
        uasort($sortedAdvices, function(Advice $first, Advice $second) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
79
            switch (true) {
80
                case $first instanceof AdviceBefore && !($second instanceof AdviceBefore):
81
                    return -1;
82
83
                case $first instanceof AdviceAround && !($second instanceof AdviceAround):
84
                    return 1;
85
86
                case $first instanceof AdviceAfter && !($second instanceof AdviceAfter):
87
                    return $second instanceof AdviceBefore ? 1 : -1;
88
89
                case ($first instanceof OrderedAdvice && $second instanceof OrderedAdvice):
90
                    return $first->getAdviceOrder() - $second->getAdviceOrder();
91
92
                default:
93
                    return 0;
94
            }
95
        });
96
97
        return $sortedAdvices;
98
    }
99
}
100