-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManifest.php
87 lines (73 loc) · 1.93 KB
/
Manifest.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
<?php
/**
* MooToons\Manifest
*
* @package MooToons\Manifest
* @author MooToon <support@mootoons.com>
* @license GPL v2 or later
* @link https://mootoons.com/
*/
namespace MooToons\Manifest;
if (!defined('ABSPATH')) {
exit;
}
use Illuminate\Support\Arr;
use MooToons\Manifest\Concerns\ReadJson;
use MooToons\Manifest\Contracts\JsonFileNotFoundException;
class Manifest
{
use ReadJson {
load as traitLoad;
}
/** @var string */
protected $url;
/** @var string */
protected $path;
public function __construct(string $path, string $url, string $directory = 'dist')
{
$this->path = rtrim($path, '\/') . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR;
$this->url = rtrim($url, '\/') . "/{$directory}/";
}
/**
* {@inheritDoc}
*/
protected function getJsonPath(): string
{
return $this->path . DIRECTORY_SEPARATOR . 'asset-manifest.json';
}
/**
* {@inheritDoc}
*/
protected function load(string $file): array
{
try {
return $this->traitLoad($file);
} catch (JsonFileNotFoundException $e) {
// We used to throw an exception here but it just causes confusion for new users.
error_log($e->getMessage());
}
return [];
}
/**
* Get a path dist asset-manifest.json.
*
* @return EntryPoints
*/
public function entrypoints(string $id): EntryPoints
{
return new EntryPoints($id, $this->path, $this->url, $this->get('entrypoints'));
}
/**
* Get a path dist asset-manifest.json key files.
*
* @return string|array|null
*/
public function files(?string $file = null)
{
$files = array_map(
fn ($f) => $this->url . trim(substr($f, strpos($f, '/dist/') + 6)),
$this->get('files')
);
return $file ? Arr::get($files, $file, null) : $files;
}
}