1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Doppelgaenger\StreamFilters\IntroductionFilter |
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\StreamFilters; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Doppelgaenger\Entities\Lists\IntroductionList; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This filter will add given interfaces to already defined classes |
27
|
|
|
* |
28
|
|
|
* @author Bernhard Wick <[email protected]> |
29
|
|
|
* @copyright 2015 TechDivision GmbH - <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/appserver-io/doppelgaenger |
32
|
|
|
* @link http://www.appserver.io/ |
33
|
|
|
*/ |
34
|
|
|
class IntroductionFilter extends AbstractFilter |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Order number if filters are used as a stack, higher means below others |
39
|
|
|
* |
40
|
|
|
* @const integer FILTER_ORDER |
41
|
|
|
*/ |
42
|
|
|
const FILTER_ORDER = 00; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Filter a chunk of data by adding introductions to it |
46
|
|
|
* |
47
|
|
|
* @param string $chunk The data chunk to be filtered |
48
|
|
|
* @param IntroductionList $introductions List of introductions |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function filterChunk($chunk, IntroductionList $introductions) |
53
|
|
|
{ |
54
|
|
|
// Get our buckets from the stream |
55
|
|
|
$interfaceHook = ''; |
56
|
|
|
$keywordNeeded = true; |
57
|
|
|
// Has to be done only once at the beginning of the definition |
58
|
|
|
if (empty($interfaceHook) && $introductions->count() > 0) { |
59
|
|
|
// Get the tokens |
60
|
|
|
$tokens = token_get_all($chunk); |
61
|
|
|
|
62
|
|
|
// Go through the tokens and check what we found |
63
|
|
|
$tokensCount = count($tokens); |
64
|
|
|
for ($i = 0; $i < $tokensCount; $i++) { |
65
|
|
|
// We need something to hook into, right after class header seems fine |
66
|
|
|
if (is_array($tokens[$i]) && $tokens[$i][0] === T_CLASS && $tokens[$i - 1][0] !== T_PAAMAYIM_NEKUDOTAYIM) { |
67
|
|
|
for ($j = $i; $j < $tokensCount; $j++) { |
68
|
|
|
// If we got the opening bracket we can break |
69
|
|
|
if ($tokens[$j] === '{' || $tokens[$j][0] === T_CURLY_OPEN) { |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (is_array($tokens[$j])) { |
74
|
|
|
// we have to check if there already are interfaces |
75
|
|
|
if ($tokens[$j][0] === T_IMPLEMENTS) { |
76
|
|
|
$keywordNeeded = false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$interfaceHook .= $tokens[$j][1]; |
80
|
|
|
|
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
$interfaceHook .= $tokens[$j]; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// build up the injected code and make the injection |
87
|
|
|
if ($keywordNeeded) { |
88
|
|
|
$implementsCode = ' implements '; |
89
|
|
|
|
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
$implementsCode = ', '; |
92
|
|
|
} |
93
|
|
|
$useCode = ''; |
94
|
|
|
$interfaces = array(); |
95
|
|
|
foreach ($introductions as $introduction) { |
96
|
|
|
$interfaces[] = $introduction->getInterface(); |
97
|
|
|
|
98
|
|
|
// build up code for the trait usage |
99
|
|
|
$useCode .= 'use ' . $introduction->getImplementation() . '; |
100
|
|
|
'; |
101
|
|
|
} |
102
|
|
|
$implementsCode .= implode(', ', $interfaces); |
103
|
|
|
|
104
|
|
|
// add the "use" code |
105
|
|
|
$chunk = str_replace( |
106
|
|
|
$interfaceHook . '{', |
107
|
|
|
$interfaceHook . '{' . $useCode, |
108
|
|
|
$chunk |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
// add the "implements" code |
112
|
|
|
$chunk = str_replace( |
113
|
|
|
$interfaceHook, |
114
|
|
|
$interfaceHook . $implementsCode, |
115
|
|
|
$chunk |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $chunk; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Will filter the given params and create a clean array of interface names from them |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
protected function filterParams() |
131
|
|
|
{ |
132
|
|
|
$interfaces = array(); |
133
|
|
|
|
134
|
|
|
// filter the params |
135
|
|
|
if (is_array($this->params)) { |
136
|
|
|
$interfaces = $this->params; |
137
|
|
|
|
|
|
|
|
138
|
|
|
} else { |
139
|
|
|
$interfaces[] = $this->params; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// filter out everything which might not be right |
143
|
|
|
foreach ($interfaces as $key => $interfaceCandidate) { |
144
|
|
|
if (!is_string($interfaceCandidate)) { |
145
|
|
|
unset($interfaces[$key]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $interfaces; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|