Passed
Push — master ( f03095...9cdc70 )
by 世昌
02:15
created

Resource::getResourcePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application;
3
4
use nebula\component\loader\Path;
5
use nebula\application\config\Config;
6
use nebula\application\filesystem\FileSystem;
7
8
/**
9
 * 资源管理器
10
 */
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
74
    {
75
        foreach ($this->resource as $root) {
76
            if ( $target = Config::resolve($root.'/'.$path)) {
77
                return $target;
78
            }
79
        }
80
        return null; 
81
    }
82
}
83