1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\ParserReflection\Instrument; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Special class for resolving path for different file systems, wrappers, etc |
15
|
|
|
* |
16
|
|
|
* @see http://stackoverflow.com/questions/4049856/replace-phps-realpath/4050444 |
17
|
|
|
* @see http://bugs.php.net/bug.php?id=52769 |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/goaop/framework/blob/master/src/Instrument/PathResolver.php |
20
|
|
|
*/ |
21
|
|
|
class PathResolver |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Custom replacement for realpath() and stream_resolve_include_path() |
26
|
|
|
* |
27
|
|
|
* @param string|array $somePath Path without normalization or array of paths |
28
|
|
|
* @param bool $shouldCheckExistence Flag for checking existence of resolved filename |
29
|
|
|
* |
30
|
|
|
* @return array|bool|string |
31
|
|
|
*/ |
32
|
143 |
|
public static function realpath($somePath, $shouldCheckExistence = false) |
33
|
|
|
{ |
34
|
|
|
// Do not resolve empty string/false/arrays into the current path |
35
|
143 |
|
if (!$somePath) { |
36
|
|
|
return $somePath; |
37
|
|
|
} |
38
|
|
|
|
39
|
143 |
|
if (is_array($somePath)) { |
40
|
|
|
return array_map(array(__CLASS__, __FUNCTION__), $somePath); |
41
|
|
|
} |
42
|
|
|
// Trick to get scheme name and path in one action. If no scheme, then there will be only one part |
43
|
143 |
|
$components = explode('://', $somePath, 2); |
44
|
143 |
|
list ($pathScheme, $path) = isset($components[1]) ? $components : array(null, $components[0]); |
45
|
|
|
|
46
|
|
|
// Optimization to bypass complex logic for simple paths (eg. not in phar archives) |
47
|
143 |
|
if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) { |
48
|
143 |
|
return $fastPath; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$isRelative = !$pathScheme && ($path[0] !== '/') && ($path[1] !== ':'); |
52
|
|
|
if ($isRelative) { |
53
|
|
|
$path = getcwd() . DIRECTORY_SEPARATOR . $path; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// resolve path parts (single dot, double dot and double delimiters) |
57
|
|
|
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); |
58
|
|
|
if (strpos($path, '.') !== false) { |
59
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $path); |
60
|
|
|
$absolutes = []; |
61
|
|
|
foreach ($parts as $part) { |
62
|
|
|
if ('.' == $part) { |
63
|
|
|
continue; |
64
|
|
|
} elseif ('..' == $part) { |
65
|
|
|
array_pop($absolutes); |
66
|
|
|
} else { |
67
|
|
|
$absolutes[] = $part; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$path = implode(DIRECTORY_SEPARATOR, $absolutes); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($pathScheme) { |
74
|
|
|
$path = "{$pathScheme}://{$path}"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($shouldCheckExistence && !file_exists($path)) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $path; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|