|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Manipulates class and namespace names |
|
9
|
|
|
* Ubiquity\cache$ClassUtils |
|
10
|
|
|
* This class is part of Ubiquity |
|
11
|
|
|
* |
|
12
|
|
|
* @author jcheron <[email protected]> |
|
13
|
|
|
* @version 1.0.0 |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class ClassUtils { |
|
17
|
|
|
|
|
18
|
22 |
|
private static function getClassNamespaceFromPhpCode($phpCode) { |
|
19
|
22 |
|
$tokens = token_get_all ( $phpCode ); |
|
20
|
22 |
|
$count = \count ( $tokens ); |
|
21
|
22 |
|
$i = 0; |
|
22
|
22 |
|
$namespace = ''; |
|
23
|
22 |
|
$namespace_ok = false; |
|
24
|
22 |
|
while ( $i < $count ) { |
|
25
|
22 |
|
$token = $tokens [$i]; |
|
26
|
22 |
|
if (\is_array ( $token ) && $token [0] === T_NAMESPACE) { |
|
27
|
|
|
// Found namespace declaration |
|
28
|
22 |
|
while ( ++ $i < $count ) { |
|
29
|
22 |
|
if ($tokens [$i] === ';') { |
|
30
|
22 |
|
$namespace_ok = true; |
|
31
|
22 |
|
$namespace = \trim ( $namespace ); |
|
32
|
22 |
|
break; |
|
33
|
|
|
} |
|
34
|
22 |
|
$namespace .= \is_array ( $tokens [$i] ) ? $tokens [$i] [1] : $tokens [$i]; |
|
35
|
|
|
} |
|
36
|
22 |
|
break; |
|
37
|
|
|
} |
|
38
|
22 |
|
$i ++; |
|
39
|
|
|
} |
|
40
|
22 |
|
if (! $namespace_ok) { |
|
41
|
|
|
return null; |
|
42
|
|
|
} else { |
|
43
|
22 |
|
return $namespace; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
22 |
|
private static function getClassNameFromPhpCode($phpCode) { |
|
48
|
22 |
|
$classes = array (); |
|
49
|
22 |
|
$tokens = \token_get_all ( $phpCode ); |
|
50
|
22 |
|
$count = count ( $tokens ); |
|
51
|
22 |
|
for($i = 2; $i < $count; $i ++) { |
|
52
|
22 |
|
if ($tokens [$i - 2] [0] == T_CLASS && $tokens [$i - 1] [0] == T_WHITESPACE && $tokens [$i] [0] == T_STRING) { |
|
53
|
22 |
|
$class_name = $tokens [$i] [1]; |
|
54
|
22 |
|
$classes [] = $class_name; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
22 |
|
if (isset ( $classes [0] )) |
|
58
|
22 |
|
return $classes [0]; |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* get the full name (name \ namespace) of a class from its file path |
|
64
|
|
|
* result example: (string) "I\Am\The\Namespace\Of\This\Class" |
|
65
|
|
|
* |
|
66
|
|
|
* @param $filePathName |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
22 |
|
public static function getClassFullNameFromFile($filePathName, $backSlash = false) { |
|
71
|
22 |
|
$phpCode = \file_get_contents ( $filePathName ); |
|
72
|
22 |
|
$ns = self::getClassNamespaceFromPhpCode ( $phpCode ); |
|
73
|
22 |
|
if ($backSlash && UString::isNotNull ( $ns )) { |
|
74
|
1 |
|
$ns = "\\" . $ns; |
|
75
|
|
|
} |
|
76
|
22 |
|
return $ns . '\\' . self::getClassNameFromPhpCode ( $phpCode ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public static function cleanClassname($classname) { |
|
80
|
1 |
|
return \str_replace ( "\\", "\\\\", $classname ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns a cleanly namespace |
|
85
|
|
|
* |
|
86
|
|
|
* @param array|string $parts |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public static function getNamespaceFromParts($parts) { |
|
90
|
1 |
|
$resultArray = [ ]; |
|
91
|
1 |
|
if (! \is_array ( $parts )) { |
|
92
|
|
|
$parts = [ $parts ]; |
|
93
|
|
|
} |
|
94
|
1 |
|
foreach ( $parts as $part ) { |
|
95
|
1 |
|
$resultArray = \array_merge ( $resultArray, \explode ( "\\", $part ) ); |
|
96
|
|
|
} |
|
97
|
1 |
|
$resultArray = \array_diff ( $resultArray, [ "" ] ); |
|
98
|
1 |
|
return \implode ( "\\", $resultArray ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the namespace from a complete classname |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $completeClassname |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function getNamespaceFromCompleteClassname($completeClassname) { |
|
108
|
|
|
$position = \strrpos ( $completeClassname, '\\' ); |
|
109
|
|
|
return \substr ( $completeClassname, 0, $position ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* build and return an object of a class from its file path |
|
114
|
|
|
* |
|
115
|
|
|
* @param $filePathName |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function getClassObjectFromFile($filePathName) { |
|
120
|
|
|
$classString = self::getClassFullNameFromFile ( $filePathName ); |
|
121
|
|
|
$object = new $classString (); |
|
122
|
|
|
return $object; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* get the class namespace form file path using token |
|
127
|
|
|
* |
|
128
|
|
|
* @param $filePathName |
|
129
|
|
|
* |
|
130
|
|
|
* @return null|string |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function getClassNamespaceFromFile($filePathName) { |
|
133
|
|
|
$phpCode = \file_get_contents ( $filePathName ); |
|
134
|
|
|
return self::getClassNamespaceFromPhpCode ( $phpCode ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* get the class name from file path using token |
|
139
|
|
|
* |
|
140
|
|
|
* @param $filePathName |
|
141
|
|
|
* |
|
142
|
|
|
* @return mixed |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function getClassNameFromFile($filePathName) { |
|
145
|
6 |
|
$phpCode = \file_get_contents ( $filePathName ); |
|
146
|
6 |
|
return self::getClassNameFromPhpCode ( $phpCode ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
6 |
|
/** |
|
150
|
|
|
* Returns the complete name of a class |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $defaultNS |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function getClassNameWithNS($defaultNS, $name) { |
|
157
|
|
|
if (\strpos ( $name, "\\" ) === false) { |
|
158
|
27 |
|
$name = $defaultNS . "\\" . $name; |
|
159
|
27 |
|
} |
|
160
|
|
|
return $name; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the simple class name of a class, without namespace |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $classnameWithNamespace |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function getClassSimpleName($classnameWithNamespace) { |
|
170
|
|
|
if (($pos = \strrpos ( $classnameWithNamespace, '\\' )) !== false) { |
|
171
|
|
|
return \substr ( $classnameWithNamespace, $pos + 1 ); |
|
172
|
|
|
} |
|
173
|
|
|
return $classnameWithNamespace; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|