Ubiquity  2.0.2
php rapid development framework
ClassUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\cache;
4 
5 class ClassUtils {
6 
15  public static function getClassFullNameFromFile($filePathName) {
16  $phpCode=\file_get_contents($filePathName);
17  return self::getClassNamespaceFromPhpCode($phpCode) . '\\' . self::getClassNameFromPhpCode($phpCode);
18  }
19 
20  public static function cleanClassname($classname) {
21  return \str_replace("\\", "\\\\", $classname);
22  }
23 
29  public static function getNamespaceFromParts($parts){
30  $resultArray=[];
31  if(!\is_array($parts)){
32  $parts=[$parts];
33  }
34  foreach ($parts as $part){
35  $resultArray=\array_merge($resultArray,\explode("\\", $part));
36  }
37  $resultArray=\array_diff($resultArray, [""]);
38  return \implode("\\", $resultArray);
39  }
40 
48  public static function getClassObjectFromFile($filePathName) {
49  $classString=self::getClassFullNameFromFile($filePathName);
50  $object=new $classString();
51  return $object;
52  }
53 
61  public static function getClassNamespaceFromFile($filePathName) {
62  $phpCode=\file_get_contents($filePathName);
63  return self::getClassNamespaceFromPhpCode($phpCode);
64  }
65 
66  private static function getClassNamespaceFromPhpCode($phpCode) {
67  $tokens=token_get_all($phpCode);
68  $count=\count($tokens);
69  $i=0;
70  $namespace='';
71  $namespace_ok=false;
72  while ( $i < $count ) {
73  $token=$tokens[$i];
74  if (\is_array($token) && $token[0] === T_NAMESPACE) {
75  // Found namespace declaration
76  while ( ++$i < $count ) {
77  if ($tokens[$i] === ';') {
78  $namespace_ok=true;
79  $namespace=\trim($namespace);
80  break;
81  }
82  $namespace.=\is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
83  }
84  break;
85  }
86  $i++;
87  }
88  if (!$namespace_ok) {
89  return null;
90  } else {
91  return $namespace;
92  }
93  }
94 
102  public static function getClassNameFromFile($filePathName) {
103  $phpCode=\file_get_contents($filePathName);
104  return self::getClassNameFromPhpCode($phpCode);
105  }
106 
107  private static function getClassNameFromPhpCode($phpCode){
108  $classes=array ();
109  $tokens=\token_get_all($phpCode);
110  $count=count($tokens);
111  for($i=2; $i < $count; $i++) {
112  if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
113  $class_name=$tokens[$i][1];
114  $classes[]=$class_name;
115  }
116  }
117  if(isset($classes[0]))
118  return $classes[0];
119  return null;
120  }
121 
122  public static function getClassNameWithNS($defaultNS,$name){
123  if(\strpos($name, "\\")===false){
124  $name=$defaultNS."\\".$name;
125  }
126  return $name;
127  }
128 
129  public static function getClassSimpleName($classnameWithNamespace){
130  return substr($classnameWithNamespace, strrpos($classnameWithNamespace, '\\')+1);
131  }
132 }
static getClassFullNameFromFile($filePathName)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Definition: ClassUtils.php:15
static getClassNameFromFile($filePathName)
get the class name form file path using token
Definition: ClassUtils.php:102
static getClassNamespaceFromFile($filePathName)
get the class namespace form file path using token
Definition: ClassUtils.php:61
static getNamespaceFromParts($parts)
Returns a cleanly namespace.
Definition: ClassUtils.php:29
static getClassNameWithNS($defaultNS, $name)
Definition: ClassUtils.php:122
static getClassNamespaceFromPhpCode($phpCode)
Definition: ClassUtils.php:66
static cleanClassname($classname)
Definition: ClassUtils.php:20
static getClassObjectFromFile($filePathName)
build and return an object of a class from its file path
Definition: ClassUtils.php:48
static getClassNameFromPhpCode($phpCode)
Definition: ClassUtils.php:107
static getClassSimpleName($classnameWithNamespace)
Definition: ClassUtils.php:129