|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\Assets; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
|
6
|
|
|
|
|
7
|
|
|
class Twill |
|
8
|
|
|
{ |
|
9
|
|
|
function asset($file) |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
return $this->devAsset($file) ?? $this->twillAsset($file); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function twillAsset($file) |
|
15
|
|
|
{ |
|
16
|
|
|
$manifest = $this->readManifest(); |
|
17
|
|
|
|
|
18
|
|
|
if (isset($manifest[$file])) { |
|
19
|
|
|
return asset($manifest[$file]); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return asset( |
|
23
|
|
|
'/' . config('twill.public_directory', 'twill') . '/' . $file, |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getManifestFilename() |
|
28
|
|
|
{ |
|
29
|
|
|
$fileName = |
|
30
|
|
|
public_path(config('twill.public_directory', 'twill')) . |
|
31
|
|
|
'/' . |
|
32
|
|
|
config('twill.manifest_file', 'twill-manifest.json'); |
|
33
|
|
|
|
|
34
|
|
|
if (file_exists($fileName)) { |
|
35
|
|
|
return $fileName; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return base_path( |
|
39
|
|
|
'vendor/area17/twill/dist/assets/admin/twill-manifest.json', |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function devAsset($file) |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$this->devMode()) { |
|
46
|
|
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080'); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
$manifest = $this->readJson( |
|
53
|
|
|
$devServerUrl . |
|
54
|
|
|
'/' . |
|
55
|
|
|
config('twill.manifest_file', 'twill-manifest.json'), |
|
56
|
|
|
); |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
throw new \Exception( |
|
59
|
|
|
'Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.', |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $devServerUrl . ($manifest[$file] ?? '/' . $file); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
private function readManifest() |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
|
|
return Cache::rememberForever('twill-manifest', function () { |
|
73
|
|
|
return $this->readJson($this->getManifestFilename()); |
|
74
|
|
|
}); |
|
75
|
|
|
} catch (\Exception $e) { |
|
76
|
|
|
throw new \Exception( |
|
77
|
|
|
'Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.', |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function readJson($fileName) |
|
83
|
|
|
{ |
|
84
|
|
|
return json_decode(file_get_contents($fileName), true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function devMode() |
|
88
|
|
|
{ |
|
89
|
|
|
return app()->environment('local', 'development') && |
|
|
|
|
|
|
90
|
|
|
config('twill.dev_mode', false); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.