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