-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathBundle.php
144 lines (123 loc) · 3.38 KB
/
Bundle.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
<?php
namespace Roots\Acorn\Assets;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Roots\Acorn\Assets\Concerns\Enqueuable;
use Roots\Acorn\Assets\Contracts\Bundle as BundleContract;
class Bundle implements BundleContract
{
use Enqueuable;
protected $id;
protected $path;
protected $uri;
protected $runtime;
protected $bundle;
protected static $runtimes = [];
/**
* Create a new bundle.
*
* @param string $id
* @param array $bundle
* @param string $path
* @param string $uri
*/
public function __construct(string $id, array $bundle, string $path, string $uri = '/')
{
$this->id = $id;
$this->path = $path;
$this->uri = $uri;
$this->bundle = $bundle;
$this->setRuntime();
}
/**
* Get CSS files in bundle.
*
* Optionally pass a function to execute on each CSS file.
*
* @param callable $callable
* @return Collection|$this
*/
public function css(?callable $callable = null)
{
if (! $callable) {
return collect($this->bundle['css']);
}
collect($this->bundle['css'] ?? [])
->each(function ($src, $handle) use ($callable) {
$callable("{$this->id}/{$handle}", $this->getUrl($src));
});
return $this;
}
/**
* Get JS files in bundle.
*
* Optionally pass a function to execute on each JS file.
*
* @param callable $callable
* @return Collection|$this
*/
public function js(?callable $callable = null)
{
if (! $callable) {
return collect($this->bundle['js']);
}
collect($this->bundle['js'])
->reject('runtime')
->each(function ($src, $handle) use ($callable) {
$callable("{$this->id}/{$handle}", $this->getUrl($src), $this->dependencies());
});
return $this;
}
/**
* Get depdencies.
*
* @return array
*/
public function dependencies()
{
return $this->bundle['dependencies'];
}
/**
* Get bundle runtime.
*
* @return string|null
*/
public function runtime()
{
return $this->runtime;
}
/**
* Get bundle runtime contents.
*
* @return string|null
*/
public function runtimeSource()
{
if (($runtime = $this->runtime()) === null) {
return null;
}
if ($sauce = self::$runtimes[$runtime] ?? null) {
return $sauce;
}
return self::$runtimes[$runtime] = file_get_contents("{$this->path}/{$runtime}");
}
protected function getUrl($path)
{
if (parse_url($path, PHP_URL_HOST)) {
return $path;
}
$path = ltrim($path, '/');
$uri = rtrim($this->uri, '/');
return "{$uri}/{$path}";
}
protected function setRuntime()
{
if (Arr::isAssoc($this->bundle['js'])) {
$this->runtime = $this->bundle['js']['runtime'] ?? $this->bundle['js']["runtime~{$this->id}"] ?? null;
unset($this->bundle['js']['runtime'], $this->bundle['js']["runtime~{$this->id}"]);
} elseif (isset($this->bundle['js'][0]) && strpos($this->bundle['js'][0], 'runtime') === 0) {
$this->runtime = $this->bundle['js'][0];
unset($this->bundle['js'][0]);
}
}
}