-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetManifestTrait.php
executable file
·85 lines (73 loc) · 2.04 KB
/
AssetManifestTrait.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
<?php
namespace Statamic\Addons\AssetManifest;
use Statamic\API\Config;
use Statamic\API\File;
use Statamic\API\Str;
use Statamic\API\URL;
use Statamic\Extend\HasParameters;
trait AssetManifestTrait
{
/**
* Provides access to methods for retrieving parameters
*/
use HasParameters;
/**
* The name of asset revision manifest file.
*/
static $manifest = 'asset-manifest.json';
/**
* Returns the path of given asset type.
* @param string $type
* @return string
*/
public function getAssetPath($type)
{
$src = $this->get('src', Config::get('theming.theme'));
$path = '/' . $type . '/' . Str::ensureRight($src, '.' . $type);
$manifest = $this->getManifest()->get($path);
return $this->themeUrl($manifest ? $manifest : $path);
}
/**
* Transforms the asset directory into a relative or absolute URL for use in the front-end.
*
* @param string $path
* @return string
*/
private function themeUrl($path)
{
if (!$this->getParam('path', false)) {
$url = URL::assemble(
Config::get('system.filesystems.themes.url'),
Config::get('theming.theme'),
$path
);
} else {
$url = URL::assemble($this->getParam('path'), $path);
}
$url = URL::prependSiteUrl(
$url,
$this->get('locale', default_locale()),
false
);
if (!$this->getBool('absolute')) {
$url = URL::makeRelative($url);
}
return $url;
}
/**
* Returns the revision manifest contained in a Collection.
*
* @return \Illuminate\Support\Collection
*/
private function getManifest()
{
$path = root_path(
URL::assemble(
Config::get('system.filesystems.themes.root'),
Config::get('theming.theme'),
static::$manifest
)
);
return collect(json_decode(File::get($path), true));
}
}