|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace gossi\codegen\utils; |
|
19
|
|
|
|
|
20
|
|
|
use phootwork\tokenizer\PhpTokenizer; |
|
21
|
|
|
|
|
22
|
|
|
class ReflectionUtils { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @param bool $publicOnly |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public static function getOverrideableMethods(\ReflectionClass $class, $publicOnly = false) { |
|
29
|
1 |
|
$filter = \ReflectionMethod::IS_PUBLIC; |
|
30
|
|
|
|
|
31
|
1 |
|
if (!$publicOnly) { |
|
32
|
1 |
|
$filter |= \ReflectionMethod::IS_PROTECTED; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
return array_filter($class->getMethods($filter), function ($method) { |
|
36
|
1 |
|
return !$method->isFinal() && !$method->isStatic(); |
|
37
|
1 |
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $docComment |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public static function getUnindentedDocComment($docComment) { |
|
45
|
1 |
|
$lines = explode("\n", $docComment); |
|
46
|
1 |
|
for ($i = 0, $c = count($lines); $i < $c; $i++) { |
|
47
|
1 |
|
if (0 === $i) { |
|
48
|
1 |
|
$docBlock = $lines[0] . "\n"; |
|
49
|
1 |
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$docBlock .= ' ' . ltrim($lines[$i]); |
|
53
|
|
|
|
|
54
|
1 |
|
if ($i + 1 < $c) { |
|
55
|
1 |
|
$docBlock .= "\n"; |
|
56
|
1 |
|
} |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $docBlock; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* @param \ReflectionFunctionAbstract $function |
|
65
|
|
|
*/ |
|
66
|
7 |
|
public static function getFunctionBody(\ReflectionFunctionAbstract $function) { |
|
67
|
7 |
|
$source = file($function->getFileName()); |
|
68
|
7 |
|
$start = $function->getStartLine() - 1; |
|
69
|
7 |
|
$end = $function->getEndLine(); |
|
70
|
7 |
|
$body = implode('', array_slice($source, $start, $end - $start)); |
|
71
|
7 |
|
$open = strpos($body, '{'); |
|
72
|
7 |
|
$close = strrpos($body, '}'); |
|
73
|
7 |
|
return trim(substr($body, $open + 1, (strlen($body) - $close) * -1)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
public static function getUseStatements(\ReflectionClass $class) { |
|
77
|
6 |
|
$content = ''; |
|
78
|
6 |
|
$file = file($class->getFileName()); |
|
79
|
6 |
|
for ($i = 0; $i < $class->getStartLine(); $i++) { |
|
80
|
6 |
|
$content .= $file[$i]; |
|
81
|
6 |
|
} |
|
82
|
6 |
|
$tokenizer = new PhpTokenizer(); |
|
83
|
6 |
|
$tokens = $tokenizer->tokenize($content); |
|
84
|
6 |
|
$tokens = $tokens->filter(function ($token) { |
|
85
|
6 |
|
return $token->type !== T_WHITESPACE && $token->type !== T_COMMENT && $token->type !== T_DOC_COMMENT; |
|
86
|
6 |
|
}); |
|
87
|
6 |
|
$statements = []; |
|
88
|
|
|
|
|
89
|
6 |
|
while (($token = $tokens->next())) { |
|
90
|
6 |
|
if ($token->type === T_USE) { |
|
91
|
3 |
|
$explicitAlias = false; |
|
92
|
3 |
|
$alias = ''; |
|
93
|
3 |
|
$class = ''; |
|
94
|
|
|
|
|
95
|
3 |
|
while (($token = $tokens->next())) { |
|
96
|
3 |
|
$isNameToken = $token->type === T_STRING || $token->type === T_NS_SEPARATOR; |
|
97
|
3 |
|
if (!$explicitAlias && $isNameToken) { |
|
98
|
3 |
|
$class .= $token->contents; |
|
99
|
3 |
|
} else if ($explicitAlias && $isNameToken) { |
|
100
|
2 |
|
$alias .= $token->contents; |
|
101
|
3 |
|
} else if ($token->type === T_AS) { |
|
102
|
2 |
|
$explicitAlias = true; |
|
103
|
2 |
|
$alias = ''; |
|
104
|
3 |
|
} else if ($token->contents === ',') { |
|
105
|
|
|
if ($explicitAlias) { |
|
106
|
|
|
$statements[$alias] = $class; |
|
107
|
|
|
} else { |
|
108
|
|
|
$statements[] = $class; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$class = ''; |
|
112
|
|
|
$alias = ''; |
|
113
|
|
|
$explicitAlias = false; |
|
114
|
3 |
|
} else if ($token->contents === ';') { |
|
115
|
3 |
|
if ($explicitAlias) { |
|
116
|
2 |
|
$statements[$alias] = $class; |
|
117
|
2 |
|
} else { |
|
118
|
3 |
|
$statements[] = $class; |
|
119
|
|
|
} |
|
120
|
3 |
|
break; |
|
121
|
|
|
} else { |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
3 |
|
} |
|
125
|
3 |
|
} |
|
126
|
6 |
|
} |
|
127
|
|
|
|
|
128
|
6 |
|
return $statements; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|