1
|
|
|
<?php namespace Anomaly\Streams\Platform\Asset; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Application\Application; |
4
|
|
|
use Illuminate\Contracts\Config\Repository; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AssetPaths |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class AssetPaths |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Predefined paths. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $paths = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The config repository. |
26
|
|
|
* |
27
|
|
|
* @var Repository |
28
|
|
|
*/ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The request object. |
33
|
|
|
* |
34
|
|
|
* @var Request |
35
|
|
|
*/ |
36
|
|
|
protected $request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The application object. |
40
|
|
|
* |
41
|
|
|
* @var Application |
42
|
|
|
*/ |
43
|
|
|
protected $application; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new AssetPaths instance. |
47
|
|
|
* |
48
|
|
|
* @param Repository $config |
49
|
|
|
* @param Request $request |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function __construct(Repository $config, Request $request, Application $application) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->config = $config; |
54
|
|
|
$this->request = $request; |
55
|
|
|
$this->application = $application; |
56
|
|
|
|
57
|
|
|
$this->paths = $config->get('streams::assets.paths', []); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the paths. |
62
|
|
|
* |
63
|
|
|
* @return array|mixed |
64
|
|
|
*/ |
65
|
|
|
public function getPaths() |
66
|
|
|
{ |
67
|
|
|
return $this->paths; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the paths. |
72
|
|
|
* |
73
|
|
|
* @param array $paths |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setPaths(array $paths) |
77
|
|
|
{ |
78
|
|
|
$this->paths = $paths; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Add an image path hint. |
85
|
|
|
* |
86
|
|
|
* @param $namespace |
87
|
|
|
* @param $path |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function addPath($namespace, $path) |
91
|
|
|
{ |
92
|
|
|
$this->paths[$namespace] = rtrim($path, '/\\'); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get a single path. |
99
|
|
|
* |
100
|
|
|
* @param $namespace |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
public function getPath($namespace) |
104
|
|
|
{ |
105
|
|
|
return array_get($this->paths, $namespace); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return the hinted extension. |
110
|
|
|
* |
111
|
|
|
* @param $path |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function hint($path) |
115
|
|
|
{ |
116
|
|
|
$hint = $this->extension($path); |
117
|
|
|
|
118
|
|
|
foreach ($this->config->get('streams::assets.hints', []) as $extension => $hints) { |
119
|
|
|
if (in_array($hint, $hints)) { |
120
|
|
|
return $extension; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $hint; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return the extension of the path. |
129
|
|
|
* |
130
|
|
|
* @param $path |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function extension($path) |
134
|
|
|
{ |
135
|
|
|
return pathinfo($path, PATHINFO_EXTENSION); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return the real path for a given path. |
140
|
|
|
* |
141
|
|
|
* @param $path |
142
|
|
|
* @return string |
143
|
|
|
* @throws \Exception |
144
|
|
|
*/ |
145
|
|
|
public function realPath($path) |
146
|
|
|
{ |
147
|
|
|
if (str_contains($path, '::')) { |
148
|
|
|
|
149
|
|
|
list($namespace, $path) = explode('::', $path); |
150
|
|
|
|
151
|
|
|
if (!isset($this->paths[$namespace])) { |
152
|
|
|
throw new \Exception("Path hint [{$namespace}::{$path}] does not exist!"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$path = rtrim($this->paths[$namespace], '/') . '/' . $path; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (strpos($path, '?v=')) { |
159
|
|
|
$path = substr($path, 0, strpos($path, '?v=')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $path; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return the download path for a remote asset. |
167
|
|
|
* |
168
|
|
|
* @param $url |
169
|
|
|
* @param null $path |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function downloadPath($url, $path = null) |
173
|
|
|
{ |
174
|
|
|
if (!$path && $parsed = parse_url($url)) { |
175
|
|
|
$path = array_get($parsed, 'host') . '/' . basename(array_get($parsed, 'path')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $path = $this->outputPath('downloads/' . $path); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return the output path. |
183
|
|
|
* |
184
|
|
|
* @param $collection |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function outputPath($collection) |
188
|
|
|
{ |
189
|
|
|
/* |
190
|
|
|
* If the path is already public |
191
|
|
|
* then just use it as it is. |
192
|
|
|
*/ |
193
|
|
|
if (str_contains($collection, public_path())) { |
194
|
|
|
return str_replace(public_path(), '', $collection); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/* |
198
|
|
|
* Get the real path relative to our installation. |
199
|
|
|
*/ |
200
|
|
|
$path = str_replace(base_path(), '', $this->realPath($collection)); |
201
|
|
|
|
202
|
|
|
/* |
203
|
|
|
* Build out path parts. |
204
|
|
|
*/ |
205
|
|
|
$directory = ltrim(dirname($path), '/\\') . '/'; |
206
|
|
|
$application = $this->application->getReference(); |
207
|
|
|
$filename = basename($path, $this->extension($path)) . $this->hint($path); |
208
|
|
|
|
209
|
|
|
if (starts_with($directory, 'vendor/')) { |
210
|
|
|
$directory = substr($directory, 7); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (starts_with($directory, './')) { |
214
|
|
|
$directory = in_array($this->request->segment(1), ['admin', 'installer']) ? 'admin/' : 'public/'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return "/app/{$application}/assets/{$directory}{$filename}"; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|