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