Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PreloaderInternalTrait.php
Go to the documentation of this file.
1<?php
2
4
14 private $vendorDir;
15 private static $libraries = [
16 'application' => './../app/',
17 'ubiquity' => 'phpmv/ubiquity/src/Ubiquity/',
18 'ubiquity-dev' => 'phpmv/ubiquity-dev/src/Ubiquity/',
19 'ubiquity-webtools' => 'phpmv/ubiquity-webtools/src/Ubiquity/',
20 'ubiquity-mailer' => 'phpmv/ubiquity-mailer/src/Ubiquity/',
21 'ubiquity-swoole' => 'phpmv/ubiquity-swoole/src/Ubiquity/',
22 'ubiquity-annotations'=>'phpmv/ubiquity-annotations/src/Ubiquity/',
23 'ubiquity-attributes'=>'phpmv/ubiquity-attributes/src/Ubiquity/',
24 'ubiquity-workerman' => 'phpmv/ubiquity-workerman/src/Ubiquity/',
25 'ubiquity-tarantool' => 'phpmv/ubiquity-tarantool/src/Ubiquity/',
26 'ubiquity-mysqli' => 'phpmv/ubiquity-mysqli/src/Ubiquity/',
27 'phpmv-ui' => 'phpmv/php-mv-ui/Ajax/' ];
28 private $excludeds = [ ];
29 private static $count = 0;
30 private $classes = [ ];
31 private $loader;
32
33 private function addClassFile($class, $file) {
34 if (! isset ( $this->classes [$class] )) {
35 $this->classes [$class] = $file;
36 }
37 }
38
39 private function loadClass($class, $file = null) {
40 if (! \class_exists ( $class, false )) {
41 $file ??= $this->getPathFromClass ( $class );
42 if (isset ( $file )) {
43 $this->loadFile ( $file );
44 }
45 }
46 if (\class_exists ( $class, false )) {
47 echo "$class loaded !\n";
48 }
49 }
50
51 private function getPathFromClass(string $class): ?string {
52 $classPath = $this->loader->findFile ( $class );
53 if (false !== $classPath) {
54 return \realpath ( $classPath );
55 }
56 return null;
57 }
58
59 private function loadFile(string $file): void {
60 if (\file_exists ( $file )) {
61 require_once ($file);
62 self::$count ++;
63 }
64 }
65
66 private function isExcluded(string $name): bool {
67 foreach ( $this->excludeds as $excluded ) {
68 if (\strpos ( $name, $excluded ) === 0) {
69 return true;
70 }
71 }
72 return false;
73 }
74
75 private function glob_recursive($pattern, $flags = 0) {
76 $files = \glob ( $pattern, $flags );
77 foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) {
78 $files = \array_merge ( $files, $this->glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) );
79 }
80 return $files;
81 }
82
83 private function getClassFullNameFromFile($filePathName, $backSlash = false) {
84 $phpCode = \file_get_contents ( $filePathName );
85 $class = $this->getClassNameFromPhpCode ( $phpCode );
86 if (isset ( $class )) {
87 $ns = $this->getClassNamespaceFromPhpCode ( $phpCode );
88 if ($backSlash && $ns != null) {
89 $ns = "\\" . $ns;
90 }
91 return $ns . '\\' . $class;
92 }
93 return null;
94 }
95
96 private function getClassNamespaceFromPhpCode($phpCode) {
97 $tokens = \token_get_all ( $phpCode );
98 $count = \count ( $tokens );
99 $i = 0;
100 $namespace = '';
101 $namespace_ok = false;
102 while ( $i < $count ) {
103 $token = $tokens [$i];
104 if (\is_array ( $token ) && $token [0] === T_NAMESPACE) {
105 // Found namespace declaration
106 while ( ++ $i < $count ) {
107 if ($tokens [$i] === ';') {
108 $namespace_ok = true;
109 $namespace = \trim ( $namespace );
110 break;
111 }
112 $namespace .= \is_array ( $tokens [$i] ) ? $tokens [$i] [1] : $tokens [$i];
113 }
114 break;
115 }
116 $i ++;
117 }
118 if (! $namespace_ok) {
119 return null;
120 }
121 return $namespace;
122 }
123
124 private function getClassNameFromPhpCode($phpCode) {
125 $classes = array ();
126 $tokens = \token_get_all ( $phpCode );
127 $count = count ( $tokens );
128 for($i = 2; $i < $count; $i ++) {
129 $elm = $tokens [$i - 2] [0];
130 if ($elm == T_CLASS && $tokens [$i - 1] [0] == T_WHITESPACE && $tokens [$i] [0] == T_STRING) {
131 $class_name = $tokens [$i] [1];
132 $classes [] = $class_name;
133 }
134 }
135 if (isset ( $classes [0] ))
136 return $classes [0];
137 return null;
138 }
139
140 private function asPhpArray($array, $prefix = "", $depth = 1, $format = false) {
141 $exts = array ();
142 $extsStr = "";
143 $tab = "";
144 $nl = "";
145 if ($format) {
146 $tab = \str_repeat ( "\t", $depth );
147 $nl = PHP_EOL;
148 }
149 foreach ( $array as $k => $v ) {
150 if (\is_string ( $k )) {
151 $exts [] = "\"" . $this->doubleBackSlashes ( $k ) . "\"=>" . $this->parseValue ( $v, 'array', $depth + 1, $format );
152 } else {
153 $exts [] = $this->parseValue ( $v, $prefix, $depth + 1, $format );
154 }
155 }
156 if (\count ( $exts ) > 0 || $prefix !== "") {
157 $extsStr = "(" . \implode ( "," . $nl . $tab, $exts ) . ")";
158 if (\count ( $exts ) > 0) {
159 $extsStr = "(" . $nl . $tab . \implode ( "," . $nl . $tab, $exts ) . $nl . $tab . ")";
160 }
161 }
162 return $prefix . $extsStr;
163 }
164
165 private function parseValue($v, $prefix = "", $depth = 1, $format = false) {
166 if (\is_array ( $v )) {
167 $result = $this->asPhpArray ( $v, $prefix, $depth + 1, $format );
168 } elseif ($v instanceof \Closure) {
169 $result = $this->closure_dump ( $v );
170 } else {
171 $result = $this->doubleBackSlashes ( $v );
172 $result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
173 }
174 return $result;
175 }
176
177 private function closure_dump(\Closure $c) {
178 $str = 'function (';
179 $r = new \ReflectionFunction ( $c );
180 $params = array ();
181 foreach ( $r->getParameters () as $p ) {
182 $s = '';
183 if ($p->isArray ()) {
184 $s .= 'array ';
185 } else if ($p->getClass ()) {
186 $s .= $p->getClass ()->name . ' ';
187 }
188 if ($p->isPassedByReference ()) {
189 $s .= '&';
190 }
191 $s .= '$' . $p->name;
192 if ($p->isOptional ()) {
193 $s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE );
194 }
195 $params [] = $s;
196 }
197 $str .= \implode ( ', ', $params );
198 $str .= ')';
199 $lines = file ( $r->getFileName () );
200 $sLine = $r->getStartLine ();
201 $eLine = $r->getEndLine ();
202 if ($eLine === $sLine) {
203 $match = \strstr ( $lines [$sLine - 1], "function" );
204 $str .= \strstr ( \strstr ( $match, "{" ), "}", true ) . "}";
205 } else {
206 $str .= \strrchr ( $lines [$sLine - 1], "{" );
207 for($l = $sLine; $l < $eLine - 1; $l ++) {
208 $str .= $lines [$l];
209 }
210 $str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}";
211 }
212 $vars = $r->getStaticVariables ();
213 foreach ( $vars as $k => $v ) {
214 $str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str );
215 }
216 return $str;
217 }
218
219 private function doubleBackSlashes($value) {
220 if (\is_string ( $value ))
221 return \str_replace ( "\\", "\\\\", $value );
222 return $value;
223 }
224}
225
Ubiquity\cache\preloading$PreloaderInternalTrait This class is part of Ubiquity.
getClassFullNameFromFile($filePathName, $backSlash=false)
parseValue($v, $prefix="", $depth=1, $format=false)
asPhpArray($array, $prefix="", $depth=1, $format=false)