-
Notifications
You must be signed in to change notification settings - Fork 554
/
Twill.php
105 lines (86 loc) · 2.79 KB
/
Twill.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
<?php
namespace A17\Twill\Services\Assets;
use Illuminate\Support\Facades\Cache;
class Twill
{
public static $cache = [];
public function asset($file)
{
return $this->devAsset($file) ?? $this->twillAsset($file);
}
public function twillAsset($file)
{
$manifest = $this->readManifest();
if (isset($manifest[$file])) {
return $manifest[$file];
}
return '/' . config('twill.public_directory', 'twill') . '/' . $file;
}
public function getManifestFilename()
{
$fileName =
public_path(config('twill.public_directory', 'twill')) .
'/' .
config('twill.manifest_file', 'twill-manifest.json');
if (file_exists($fileName)) {
return $fileName;
}
return base_path(
'vendor/area17/twill/dist/assets/twill/twill-manifest.json'
);
}
public function devAsset($file)
{
if (!$this->devMode()) {
return null;
}
$devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080');
try {
$manifest = $this->readJson(
$devServerUrl .
'/' .
// During dev mode and webpack 5 this is the valid path.
'assets/twill/twill-manifest.json'
);
} catch (\Exception $exception) {
throw new \Exception('Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.', $exception->getCode(), $exception);
}
return $devServerUrl . ($manifest[$file] ?? '/' . $file);
}
/**
* @return mixed
*/
private function readManifest()
{
try {
return Cache::rememberForever('twill-manifest', function () {
return $this->readJson($this->getManifestFilename());
});
} catch (\Exception $exception) {
throw new \Exception('Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.', $exception->getCode(), $exception);
}
}
private function readJson($fileName)
{
$requestOptionsIgnoreSsl = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
if (self::$cache === []) {
self::$cache = json_decode(
file_get_contents($fileName, false, stream_context_create($requestOptionsIgnoreSsl)),
true,
512,
JSON_THROW_ON_ERROR
);
}
return self::$cache;
}
private function devMode()
{
return app()->environment('local', 'development') &&
config('twill.dev_mode', false);
}
}