Ubiquity  2.0.3
php rapid development framework
ClassUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\cache;
4 
6 
7 class ClassUtils {
8 
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 
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 
54  public static function getClassObjectFromFile($filePathName) {
55  $classString=self::getClassFullNameFromFile($filePathName);
56  $object=new $classString();
57  return $object;
58  }
59 
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 
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 }
static getClassNameFromFile($filePathName)
get the class name form file path using token
Definition: ClassUtils.php:108
static getClassNamespaceFromFile($filePathName)
get the class namespace form file path using token
Definition: ClassUtils.php:67
static getNamespaceFromParts($parts)
Returns a cleanly namespace.
Definition: ClassUtils.php:35
static getClassNameWithNS($defaultNS, $name)
Definition: ClassUtils.php:128
static getClassNamespaceFromPhpCode($phpCode)
Definition: ClassUtils.php:72
static cleanClassname($classname)
Definition: ClassUtils.php:26
static getClassObjectFromFile($filePathName)
build and return an object of a class from its file path
Definition: ClassUtils.php:54
static getClassNameFromPhpCode($phpCode)
Definition: ClassUtils.php:113
static getClassSimpleName($classnameWithNamespace)
Definition: ClassUtils.php:135
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Definition: ClassUtils.php:17