-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Dispatcher.php
189 lines (163 loc) · 4.22 KB
/
Dispatcher.php
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
<?php
namespace Orchestra\Asset;
use Illuminate\Support\Str;
use Collective\Html\HtmlBuilder;
use Illuminate\Filesystem\Filesystem;
class Dispatcher
{
/**
* Filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* Html builder instance.
*
* @var \Collective\Html\HtmlBuilder
*/
protected $html;
/**
* Dependency resolver instance.
*
* @var \Orchestra\Asset\DependencyResolver
*/
protected $resolver;
/**
* Public path location.
*
* @var string
*/
protected $path;
/**
* Use asset versioning.
*
* @var bool
*/
public $useVersioning = false;
/**
* Create a new asset dispatcher instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Collective\Html\HtmlBuilder $html
* @param \Orchestra\Asset\DependencyResolver $resolver
* @param string $path
*/
public function __construct(
Filesystem $files,
HtmlBuilder $html,
DependencyResolver $resolver,
string $path
) {
$this->files = $files;
$this->html = $html;
$this->resolver = $resolver;
$this->path = $path;
}
/**
* Enable asset versioning.
*
* @return void
*/
public function addVersioning(): void
{
$this->useVersioning = true;
}
/**
* Disable asset versioning.
*
* @return void
*/
public function removeVersioning(): void
{
$this->useVersioning = false;
}
/**
* Dispatch assets by group.
*
* @param string $group
* @param array $assets
* @param string|null $prefix
*
* @return string
*/
public function run(string $group, array $assets = [], ?string $prefix = null): string
{
$html = '';
if (! isset($assets[$group]) || \count($assets[$group]) == 0) {
return $html;
}
\is_null($prefix) || $this->path = \rtrim($prefix, '/');
foreach ($this->resolver->arrange($assets[$group]) as $data) {
$html .= $this->asset($group, $data);
}
return $html;
}
/**
* Get the HTML link to a registered asset.
*
* @param string $group
* @param array|null $asset
*
* @return string
*/
public function asset(string $group, ?array $asset = null): string
{
if (! isset($asset)) {
return '';
}
$asset['source'] = $this->getAssetSourceUrl($asset['source']);
$html = $this->html->{$group}($asset['source'], $asset['attributes']);
return $html->toHtml();
}
/**
* Determine if path is local.
*
* @param string $path
*
* @return bool
*/
protected function isLocalPath(string $path): bool
{
if (Str::startsWith($path, ['https://', 'http://', '//'])) {
return false;
}
return \filter_var($path, FILTER_VALIDATE_URL) === false;
}
/**
* Get asset source URL.
*
* @param string $source
*
* @return string
*/
protected function getAssetSourceUrl(string $source): string
{
// If the source is not a complete URL, we will go ahead and prepend
// the asset's path to the source provided with the asset. This will
// ensure that we attach the correct path to the asset.
if (! $this->isLocalPath($file = $this->path.'/'.\ltrim($source, '/'))) {
return $file;
}
return $this->getAssetSourceUrlWithModifiedTime($source, $file);
}
/**
* Get asset source URL with Modified time.
*
* @param string $source
* @param string $file
*
* @return string
*/
protected function getAssetSourceUrlWithModifiedTime(string $source, string $file): string
{
if ($this->isLocalPath($source) && $this->useVersioning) {
// We can only append mtime to locally defined path since we need
// to extract the file.
if (! empty($modified = $this->files->lastModified($file))) {
$source = $source."?{$modified}";
}
}
return $source;
}
}