1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application; |
4
|
|
|
|
5
|
|
|
use suda\framework\loader\Path; |
6
|
|
|
use suda\framework\config\PathResolver; |
7
|
|
|
use suda\framework\filesystem\FileSystem; |
8
|
|
|
use suda\application\Resource as ApplicationResource; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* 资源管理器 |
12
|
|
|
*/ |
13
|
|
|
class Resource |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* 资源路径 |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $resource; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 资源路径 |
24
|
|
|
* |
25
|
|
|
* @param array|string $resource |
26
|
|
|
*/ |
27
|
|
|
public function __construct($resource = []) |
28
|
|
|
{ |
29
|
|
|
$this->resource = is_array($resource) ? $resource : [$resource]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 获取相对的路径 |
34
|
|
|
* |
35
|
|
|
* @param string $source |
36
|
|
|
* @param string $relative |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function getPathByRelativePath(string $source, string $relative): string |
40
|
|
|
{ |
41
|
|
|
$path = $source; |
42
|
|
|
if (Path::isRelativePath($source)) { |
43
|
|
|
$path = $relative . '/' . $path; |
44
|
|
|
} |
45
|
|
|
return Path::toAbsolutePath($path); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 添加资源目录 |
50
|
|
|
* |
51
|
|
|
* @param string $path |
52
|
|
|
* @param string|null $prefix |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function addResourcePath(string $path, string $prefix = null) |
56
|
|
|
{ |
57
|
|
|
$path = Path::toAbsolutePath($path); |
58
|
|
|
if (!in_array($path, $this->resource)) { |
59
|
|
|
if ($prefix !== null) { |
60
|
|
|
$prefix = trim($prefix, '/'); |
61
|
|
|
$path = [$prefix, $path]; |
62
|
|
|
} |
63
|
|
|
array_unshift($this->resource, $path); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 注册资源路径 |
69
|
|
|
* |
70
|
|
|
* @param string $parent |
71
|
|
|
* @param string $path |
72
|
|
|
*/ |
73
|
|
|
public function registerResourcePath(string $parent, string $path) |
74
|
|
|
{ |
75
|
|
|
if (strpos($path, ':')) { |
76
|
|
|
list($prefix, $path) = explode(':', $path, 2); |
77
|
|
|
$prefix = trim($prefix); |
78
|
|
|
$path = ApplicationResource::getPathByRelativePath($path, $parent); |
79
|
|
|
$this->addResourcePath($path, $prefix); |
80
|
|
|
} else { |
81
|
|
|
$path = ApplicationResource::getPathByRelativePath($path, $parent); |
82
|
|
|
$this->addResourcePath($path); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* 获取资源文件路径 |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* @param string $limitPath 父级溢出 |
92
|
|
|
* @return string|null |
93
|
|
|
*/ |
94
|
|
|
public function getResourcePath(string $path, string $limitPath = null): ?string |
95
|
|
|
{ |
96
|
|
|
foreach ($this->resource as $root) { |
97
|
|
|
if ($pathInfo = $this->getTarget($root, $path)) { |
98
|
|
|
list($root, $target) = $pathInfo; |
99
|
|
|
$templateLimitPath = $limitPath ? $root . '/' . $limitPath : $root; |
100
|
|
|
if (FileSystem::exist($target) |
101
|
|
|
&& FileSystem::isOverflowPath($templateLimitPath, $target) === false) { |
102
|
|
|
return $target; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string|array $root |
112
|
|
|
* @param string $path |
113
|
|
|
* @return array|null |
114
|
|
|
*/ |
115
|
|
|
private function getTarget($root, string $path) |
116
|
|
|
{ |
117
|
|
|
if (is_string($root)) { |
118
|
|
|
return [$root, $root . '/' . $path]; |
119
|
|
|
} |
120
|
|
|
list($prefix, $root) = $root; |
121
|
|
|
if (strpos($path, $prefix) === 0) { |
122
|
|
|
return [$root, $root . '/' . ltrim(substr($path, strlen($prefix)), '/')]; |
123
|
|
|
} |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* 获取配置资源文件路径 |
129
|
|
|
* |
130
|
|
|
* @param string $path |
131
|
|
|
* @return string|null |
132
|
|
|
*/ |
133
|
|
|
public function getConfigResourcePath(string $path): ?string |
134
|
|
|
{ |
135
|
|
|
foreach ($this->resource as $root) { |
136
|
|
|
if ($pathInfo = $this->getTarget($root, $path)) { |
137
|
|
|
list($root, $target) = $pathInfo; |
138
|
|
|
if (is_string($target) && ($target = PathResolver::resolve($target))) { |
139
|
|
|
return $target; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
public function getResource(): array |
150
|
|
|
{ |
151
|
|
|
return $this->resource; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|