Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ClassUtils.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\cache;
4
6
17
18 private static function getClassNamespaceFromPhpCode($phpCode) {
19 $tokens = token_get_all ( $phpCode );
20 $count = \count ( $tokens );
21 $i = 0;
22 $namespace = '';
23 $namespace_ok = false;
24 while ( $i < $count ) {
25 $token = $tokens [$i];
26 if (\is_array ( $token ) && $token [0] === T_NAMESPACE) {
27 // Found namespace declaration
28 while ( ++ $i < $count ) {
29 if ($tokens [$i] === ';') {
30 $namespace_ok = true;
31 $namespace = \trim ( $namespace );
32 break;
33 }
34 $namespace .= \is_array ( $tokens [$i] ) ? $tokens [$i] [1] : $tokens [$i];
35 }
36 break;
37 }
38 $i ++;
39 }
40 if (! $namespace_ok) {
41 return null;
42 } else {
43 return $namespace;
44 }
45 }
46
47 private static function getClassNameFromPhpCode($phpCode) {
48 $classes = array ();
49 $tokens = \token_get_all ( $phpCode );
50 $count = count ( $tokens );
51 for($i = 2; $i < $count; $i ++) {
52 if (($tokens [$i - 2] [0] == T_TRAIT || $tokens [$i - 2] [0] == T_CLASS )&& $tokens [$i - 1] [0] == T_WHITESPACE && $tokens [$i] [0] == T_STRING) {
53 $class_name = $tokens [$i] [1];
54 $classes [] = $class_name;
55 }
56 }
57 if (isset ( $classes [0] ))
58 return $classes [0];
59 return null;
60 }
61
70 public static function getClassFullNameFromFile($filePathName, $backSlash = false) {
71 $phpCode = \file_get_contents ( $filePathName );
72 $ns = self::getClassNamespaceFromPhpCode ( $phpCode );
73 if ($backSlash && UString::isNotNull ( $ns )) {
74 $ns = "\\" . $ns;
75 }
76 $class=self::getClassNameFromPhpCode ( $phpCode );
77 if($class!=null) {
78 return $ns . '\\' . self::getClassNameFromPhpCode($phpCode);
79 }
80 return null;
81 }
82
83 public static function cleanClassname($classname) {
84 return \str_replace ( "\\", "\\\\", $classname );
85 }
86
93 public static function getNamespaceFromParts($parts) {
94 $resultArray = [ ];
95 if (! \is_array ( $parts )) {
96 $parts = [ $parts ];
97 }
98 foreach ( $parts as $part ) {
99 $resultArray = \array_merge ( $resultArray, \explode ( "\\", $part ) );
100 }
101 $resultArray = \array_diff ( $resultArray, [ "" ] );
102 return \implode ( "\\", $resultArray );
103 }
104
111 public static function getNamespaceFromCompleteClassname($completeClassname) {
112 $position = \strrpos ( $completeClassname, '\\' );
113 return \substr ( $completeClassname, 0, $position );
114 }
115
123 public static function getClassObjectFromFile($filePathName) {
124 $classString = self::getClassFullNameFromFile ( $filePathName );
125 $object = new $classString ();
126 return $object;
127 }
128
136 public static function getClassNamespaceFromFile($filePathName) {
137 $phpCode = \file_get_contents ( $filePathName );
138 return self::getClassNamespaceFromPhpCode ( $phpCode );
139 }
140
148 public static function getClassNameFromFile($filePathName) {
149 $phpCode = \file_get_contents ( $filePathName );
150 return self::getClassNameFromPhpCode ( $phpCode );
151 }
152
160 public static function getClassNameWithNS($defaultNS, $name) {
161 if (\strpos ( $name, "\\" ) === false) {
162 $name = $defaultNS . "\\" . $name;
163 }
164 return $name;
165 }
166
173 public static function getClassSimpleName($classnameWithNamespace) {
174 if (($pos = \strrpos ( $classnameWithNamespace, '\\' )) !== false) {
175 return \substr ( $classnameWithNamespace, $pos + 1 );
176 }
177 return $classnameWithNamespace;
178 }
179}
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
static getClassObjectFromFile($filePathName)
build and return an object of a class from its file path
static getClassFullNameFromFile($filePathName, $backSlash=false)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
static getClassNameFromPhpCode($phpCode)
static getNamespaceFromCompleteClassname($completeClassname)
Returns the namespace from a complete classname.
static getClassNamespaceFromFile($filePathName)
get the class namespace form file path using token
static cleanClassname($classname)
static getNamespaceFromParts($parts)
Returns a cleanly namespace.
static getClassNameFromFile($filePathName)
get the class name from file path using token
static getClassNamespaceFromPhpCode($phpCode)
static getClassNameWithNS($defaultNS, $name)
Returns the complete name of a class.
static getClassSimpleName($classnameWithNamespace)
Returns the simple class name of a class, without namespace.
String utilities.
Definition UString.php:15
Cache managment.
Definition CacheFile.php:3