-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetResolver.php
More file actions
228 lines (200 loc) · 5.24 KB
/
AssetResolver.php
File metadata and controls
228 lines (200 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace Mods\Theme;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
class AssetResolver
{
/**
* Hint path delimiter value.
*
* @var string
*/
const HINT_PATH_DELIMITER = '::';
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The config instance.
*
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* The default paths for the loader.
*
* @var array
*/
protected $paths = [];
/**
* The array of asset that have been located.
*
* @var array
*/
protected $assets = [];
/**
* All of the namespace hints.
*
* @var array
*/
protected $hints = [];
/**
* Create a new Asset reslover instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @return void
*/
public function __construct(
Filesystem $files
) {
$this->files = $files;
}
/**
* Get the fully qualified location of the asset.
*
* @param string $name
* @return string
*/
public function find($name, $area = 'frontend')
{
if (isset($this->assets[$area][$name])) {
return $this->assets[$area][$name];
}
if ($this->hasHintInformation($name = trim($name))) {
return $this->assets[$area][$name] = $this->findNamedPathAsset($name, $area);
}
return $this->assets[$area][$name] = $this->findInPaths($name, $this->paths[$area]);
}
/**
* Get the path to a asset with a named path.
*
* @param string $name
* @return string
*/
protected function findNamedPathAsset($name, $area)
{
list($namespace, $view) = $this->getNamespaceSegments($name);
return $this->findInPaths($view, $this->hints[$area][$namespace]);
}
/**
* Find the given asset in the list of paths.
*
* @param string $name
* @param array $paths
* @return string
*
* @throws \InvalidArgumentException
*/
protected function findInPaths($name, $paths)
{
foreach ((array) $paths as $path) {
if ($this->files->exists($assetPath = $path.'/'.$name)) {
return $assetPath;
}
}
throw new InvalidArgumentException("Asset [$name] not found.");
}
/**
* Get the segments of a asset with a named path.
*
* @param string $name
* @return array
*
* @throws \InvalidArgumentException
*/
protected function getNamespaceSegments($name)
{
$segments = explode(static::HINT_PATH_DELIMITER, $name);
if (count($segments) != 2) {
throw new InvalidArgumentException("Asset [$name] has an invalid name.");
}
if (! isset($this->hints[$segments[0]])) {
throw new InvalidArgumentException("No hint path defined for [{$segments[0]}].");
}
return $segments;
}
/**
* Add a namespace hint to the finder.
*
* @param string $namespace
* @param string $hints
* @return void
*/
public function addNamespace($namespace, $hints, $area)
{
/*$hints = (array) $hints;
if (isset($this->hints[$area][$namespace])) {
$hints = array_merge($this->hints[$area][$namespace], $hints);
}*/
if($area === null) {
$area = 'frontend';
}
if(is_array($area)) {
foreach ($area as $value) {
$this->hints[$value][$namespace] = $hints;
}
} else {
$this->hints[$area][$namespace] = $hints;
}
}
/**
* The paths for the loader.
*
* @param array paths
*/
public function addLocation($paths)
{
$this->paths = array_merge($this->paths, (array) $paths);
}
/**
* Returns whether or not the assesr specify a hint information.
*
* @param string $name
* @return bool
*/
public function hasHintInformation($name)
{
return strpos($name, static::HINT_PATH_DELIMITER) > 0;
}
/**
* Get the active view paths.
*
* @return array
*/
public function getPaths($area = null, $theme = null)
{
if ($area && isset($this->paths[$area])) {
if ($theme) {
if (isset($this->paths[$area][$theme])) {
return [$theme => $this->paths[$area][$theme]];
} else {
return [];
}
}
return $this->paths[$area];
}
return [];
}
/**
* Get the namespace to file path hints.
*
* @return array
*/
public function getHints($area = null, $namespace = null)
{
if ($area && isset($this->hints[$area])) {
if ($namespace) {
if (isset($this->hints[$area][$namespace])) {
return [$namespace => $this->hints[$area][$namespace]];
} else {
return [];
}
}
return $this->hints[$area];
}
return [];
}
}