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\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
|
|
|
class PathResolver |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Custom replacement for realpath() and stream_resolve_include_path() |
24
|
|
|
* |
25
|
|
|
* @param string|array $somePath Path without normalization or array of paths |
26
|
|
|
* @param bool $shouldCheckExistence Flag for checking existence of resolved filename |
27
|
|
|
* |
28
|
|
|
* @return array|bool|string |
29
|
|
|
*/ |
30
|
28 |
|
public static function realpath($somePath, $shouldCheckExistence = false) |
31
|
|
|
{ |
32
|
|
|
// Do not resolve empty string/false/arrays into the current path |
33
|
28 |
|
if (!$somePath) { |
34
|
|
|
return $somePath; |
35
|
|
|
} |
36
|
|
|
|
37
|
28 |
|
if (is_array($somePath)) { |
38
|
1 |
|
return array_map(array(__CLASS__, __FUNCTION__), $somePath); |
39
|
|
|
} |
40
|
|
|
// Trick to get scheme name and path in one action. If no scheme, then there will be only one part |
41
|
28 |
|
$components = explode('://', $somePath, 2); |
42
|
28 |
|
list ($pathScheme, $path) = isset($components[1]) ? $components : array(null, $components[0]); |
43
|
|
|
|
44
|
|
|
// Optimization to bypass complex logic for simple paths (eg. not in phar archives) |
45
|
28 |
|
if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) { |
46
|
17 |
|
return $fastPath; |
47
|
|
|
} |
48
|
|
|
|
49
|
13 |
|
$isRelative = !$pathScheme && ($path[0] !== '/') && ($path[1] !== ':'); |
50
|
13 |
|
if ($isRelative) { |
51
|
4 |
|
$path = getcwd() . DIRECTORY_SEPARATOR . $path; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// resolve path parts (single dot, double dot and double delimiters) |
55
|
13 |
|
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); |
56
|
13 |
|
if (strpos($path, '.') !== false) { |
57
|
10 |
|
$parts = explode(DIRECTORY_SEPARATOR, $path); |
58
|
10 |
|
$absolutes = []; |
59
|
10 |
|
foreach ($parts as $part) { |
60
|
10 |
|
if ('.' == $part) { |
61
|
2 |
|
continue; |
62
|
10 |
|
} elseif ('..' == $part) { |
63
|
5 |
|
array_pop($absolutes); |
64
|
|
|
} else { |
65
|
10 |
|
$absolutes[] = $part; |
66
|
|
|
} |
67
|
|
|
} |
68
|
10 |
|
$path = implode(DIRECTORY_SEPARATOR, $absolutes); |
69
|
|
|
} |
70
|
|
|
|
71
|
13 |
|
if ($pathScheme) { |
72
|
4 |
|
$path = "{$pathScheme}://{$path}"; |
73
|
|
|
} |
74
|
|
|
|
75
|
13 |
|
if ($shouldCheckExistence && !file_exists($path)) { |
76
|
2 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
11 |
|
return $path; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|