Passed
Push — master ( 39c189...908039 )
by Jean-Christophe
11:37
created

PreloaderInternalTrait::getClassNameFromPhpCode()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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