Passed
Push — dev ( 8c8a72...55d73e )
by 世昌
02:16
created

Resource::getTarget()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 10
rs 10
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
            $target = $this->getTarget($root, $path);
98
            $templateLimitPath = $limitPath ? $root . '/' . $limitPath : $root;
99
            if (FileSystem::exist($target) && FileSystem::isOverflowPath($templateLimitPath, $target) === false) {
100
                return $target;
101
            }
102
        }
103
        return null;
104
    }
105
106
107
    /**
108
     * @param string|array $root
109
     * @param string $path
110
     * @return string
111
     */
112
    private function getTarget($root, string $path)
113
    {
114
        if (is_string($root)) {
115
            return $root . '/' . $path;
116
        }
117
        list($prefix, $root) = $root;
118
        if (strpos($path, $prefix) === 0) {
119
            $path = ltrim(substr($path, strlen($prefix)), '/');
120
        }
121
        return $root . '/' . $path;
122
    }
123
124
    /**
125
     * 获取配置资源文件路径
126
     *
127
     * @param string $path
128
     * @return string|null
129
     */
130
    public function getConfigResourcePath(string $path): ?string
131
    {
132
        foreach ($this->resource as $root) {
133
            $target = $this->getTarget($root, $path);
134
            if ($target = PathResolver::resolve($target)) {
135
                return $target;
136
            }
137
        }
138
        return null;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getResource(): array
145
    {
146
        return $this->resource;
147
    }
148
}
149