| Total Complexity | 10 |
| Total Lines | 70 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class Resource |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * 资源路径 |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected $resource = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * 获取相对的路径 |
||
| 22 | * |
||
| 23 | * @param string $source |
||
| 24 | * @param string $relative |
||
| 25 | * @return string |
||
| 26 | */ |
||
| 27 | public static function getPathByRelativedPath(string $source, string $relative):string |
||
| 28 | { |
||
| 29 | $path = $source; |
||
| 30 | if (Path::isRelativePath($source)) { |
||
| 31 | $path = $relative.'/'.$path; |
||
| 32 | } |
||
| 33 | return Path::toAbsolutePath($path); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * 添加资源目录 |
||
| 38 | * |
||
| 39 | * @param string $path |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function addResourcePath(string $path) |
||
| 43 | { |
||
| 44 | $path = Path::toAbsolutePath($path); |
||
| 45 | if (!\in_array($path, $this->resource)) { |
||
| 46 | array_unshift($this->resource, $path); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * 获取资源文件路径 |
||
| 52 | * |
||
| 53 | * @param string $path |
||
| 54 | * @return string|null |
||
| 55 | */ |
||
| 56 | public function getResourcePath(string $path):?string |
||
| 57 | { |
||
| 58 | foreach ($this->resource as $root) { |
||
| 59 | $target = $root.'/'.$path; |
||
| 60 | if (FileSystem::exist($target)) { |
||
| 61 | return $target; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | return null; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * 获取配置资源文件路径 |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * @return string|null |
||
| 72 | */ |
||
| 73 | public function getConfigResourcePath(string $path):?string |
||
| 81 | } |
||
| 82 | } |
||
| 83 |