AbstractSignaturePointcut::straightenExpression()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Entities\Pointcuts\AbstractSignaturePointcut
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Entities\Pointcuts;
22
23
use AppserverIo\Doppelgaenger\Dictionaries\PointcutPatterns;
24
use AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition;
25
use AppserverIo\Doppelgaenger\Entities\Definitions\AttributeDefinition;
26
27
/**
28
 * Abstract parent class for pointcuts which accept expressions which express a signature like pattern
29
 *
30
 * @author    Bernhard Wick <[email protected]>
31
 * @copyright 2015 TechDivision GmbH - <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/appserver-io/doppelgaenger
34
 * @link      http://www.appserver.io/
35
 */
36
abstract class AbstractSignaturePointcut extends AbstractPointcut
37
{
38
39
    /**
40
     * Call type for a call from an object
41
     *
42
     * @var string CALL_TYPE_OBJECT
43
     */
44
    const CALL_TYPE_OBJECT = '->';
45
46
    /**
47
     * Call type for a static call
48
     *
49
     * @var string CALL_TYPE_STATIC
50
     */
51
    const CALL_TYPE_STATIC = '::';
52
53
    /**
54
     * The pattern used by this pointcut to match candidates
55
     *
56
     * @var string MATCH_PATTERN
57
     */
58
    const MATCH_PATTERN = PointcutPatterns::SIGNATURE;
59
60
    /**
61
     * The type of the call made to $function
62
     *
63
     * @var string|null $callType
64
     *
65
     * @Enum({"->", "::", null})
66
     */
67
    protected $callType;
68
69
    /**
70
     * Function/method which will get called within the signature expression
71
     *
72
     * @var string $function
73
     */
74
    protected $function;
75
76
    /**
77
     * Structure name (if any) of the structure the called method belongs to
78
     *
79
     * @var string|null $structure
80
     */
81
    protected $structure;
82
83
    /**
84
     * Default constructor
85
     *
86
     * @param string  $expression String representing the expression defining this pointcut
87
     * @param boolean $isNegated  If any match made against this pointcut's expression has to be negated in its result
88
     */
89
    public function __construct($expression, $isNegated)
90
    {
91
        parent::__construct($expression, $isNegated);
92
93
        // filter what we need
94
        if (strpos($expression, self::CALL_TYPE_OBJECT) !== false || strpos($expression, self::CALL_TYPE_STATIC) !== false) {
95
            // assume an object call but correct the call type in the unlikely case we did get a static call
96
            $this->callType = self::CALL_TYPE_OBJECT;
97
            if (strpos($expression, self::CALL_TYPE_STATIC) !== false) {
98
                $this->callType = self::CALL_TYPE_STATIC;
99
            }
100
101
            // we have to isolate the parts of the expression
102
            $this->structure = strstr($expression, $this->callType, true);
103
            $this->function = str_replace($this->structure . $this->callType, '', $expression);
104
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
105
        } else {
106
            $this->callType = null;
107
            $this->structure = null;
108
            $this->function = null;
109
        }
110
    }
111
112
    /**
113
     * Will return a chain of callbacks which can be used to call woven code in an onion like manner
114
     *
115
     * @param \AppserverIo\Doppelgaenger\Entities\Definitions\FunctionDefinition $functionDefinition Definition of the function to inject invocation code into
116
     *
117
     * @return array
118
     */
119
    public function getCallbackChain(FunctionDefinition $functionDefinition)
120
    {
121
122
        if ($this->callType === self::CALL_TYPE_STATIC) {
123
            // we can work with the structure name alone if we have a static call
124
125
            return array(array($this->structure, $this->function));
126
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
127
        } elseif (ltrim($this->structure, '\\') === $functionDefinition->getStructureName()) {
128
            // if the callback chain is used within the actual class we can use the current context
129
130
            return array(array('$this', $this->function));
131
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
132
        } else {
133
            // for everything else (mostly advice chain callbacks) we will create a new instance
134
135
            return array(array('new ' . $this->structure . '()', $this->function));
136
        }
137
    }
138
139
    /**
140
     * Used to "straighten out" an expression as some expressions allow for shell regex which makes them hard to
141
     * generate code from.
142
     * So with this method a matching pointcut can be altered into having a directly readable expression
143
     *
144
     * @param FunctionDefinition|AttributeDefinition $definition Definition to straighten the expression against
145
     *
146
     * @return null
147
     */
148
    public function straightenExpression($definition)
149
    {
150
        // structure name has to be absolute
151
        $structureName = '\\' . ltrim($definition->getStructureName(), '\\');
152
153
        // fix the expression
154
        $this->expression = str_replace(
155
            array($this->callType . $this->function, $this->structure),
156
            array($this->callType . $definition->getName(), $structureName),
157
            $this->getExpression()
158
        );
159
160
        // set the obvious properties
161
        $this->function = $definition->getName();
162
        $this->structure = $structureName;
163
    }
164
}
165