1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\loader; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* 自动加载路径处理 |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
trait PathTrait |
9
|
|
|
{ |
10
|
|
|
public static function formatSeparator(string $path):string |
11
|
|
|
{ |
12
|
|
|
return str_replace(['\\','/'], DIRECTORY_SEPARATOR, $path); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public static function toAbsolutePath(string $path, string $separator = DIRECTORY_SEPARATOR):string{ |
16
|
|
|
list($scheme, $path) = static::parsePathSchemeSubpath($path); |
17
|
|
|
list($root, $path) = static::parsePathRootSubpath($path); |
18
|
|
|
$path = static::parsePathRelativePath($path, $separator); |
19
|
|
|
return $scheme.$root.$path; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected static function parsePathRootSubpath(string $path):array { |
23
|
|
|
$subpath = str_replace(['/', '\\'], '/', $path); |
24
|
|
|
$root = null; |
25
|
|
|
if (DIRECTORY_SEPARATOR === '/') { |
26
|
|
|
if (strpos($path, '/') !== 0) { |
27
|
|
|
$subpath = getcwd().DIRECTORY_SEPARATOR.$subpath; |
28
|
|
|
} |
29
|
|
|
$root = '/'; |
30
|
|
|
$subpath = substr($subpath, 1); |
31
|
|
|
} else { |
32
|
|
|
if (strpos($subpath, ':/') === false) { |
33
|
|
|
$subpath=str_replace(['/', '\\'], '/', getcwd()).'/'.$subpath; |
34
|
|
|
} |
35
|
|
|
list($root, $subpath) = explode(':/', $subpath, 2); |
36
|
|
|
$root .= ':'.DIRECTORY_SEPARATOR; |
37
|
|
|
} |
38
|
|
|
return [$root, $subpath]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected static function parsePathRelativePath(string $path, string $separator = DIRECTORY_SEPARATOR):string { |
42
|
|
|
$subpathArr = explode('/', $path); |
43
|
|
|
$absulotePaths = []; |
44
|
|
|
foreach ($subpathArr as $name) { |
45
|
|
|
$name = trim($name); |
46
|
|
|
if ($name === '..') { |
47
|
|
|
array_pop($absulotePaths); |
48
|
|
|
} elseif ($name === '.') { |
49
|
|
|
continue; |
50
|
|
|
} elseif (strlen($name)) { |
51
|
|
|
$absulotePaths[]=$name; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return implode($separator, $absulotePaths); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected static function parsePathSchemeSubpath(string $path):array |
58
|
|
|
{ |
59
|
|
|
if (static::getHomePath() !== null && strpos($path, '~') === 0) { |
60
|
|
|
$scheme =''; |
61
|
|
|
$subpath = static::getHomePath() .DIRECTORY_SEPARATOR.substr($path, 1); |
62
|
|
|
} elseif (strpos($path, '://') !== false) { |
63
|
|
|
list($scheme, $subpath) = explode('://', $path, 2); |
64
|
|
|
$scheme.='://'; |
65
|
|
|
} else { |
66
|
|
|
$scheme =''; |
67
|
|
|
$subpath = $path; |
68
|
|
|
} |
69
|
|
|
return [$scheme, $subpath]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public static function getHomePath():?string { |
74
|
|
|
if (defined('USER_HOME_PATH')) { |
75
|
|
|
return constant('USER_HOME_PATH'); |
76
|
|
|
} |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|