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 $manifest[$file]; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return '/' . config('twill.public_directory', 'twill') . '/' . $file; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getManifestFilename() |
26
|
|
|
{ |
27
|
|
|
$fileName = |
28
|
|
|
public_path(config('twill.public_directory', 'twill')) . |
29
|
|
|
'/' . |
30
|
|
|
config('twill.manifest_file', 'twill-manifest.json'); |
31
|
|
|
|
32
|
|
|
if (file_exists($fileName)) { |
33
|
|
|
return $fileName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return base_path( |
37
|
|
|
'vendor/area17/twill/dist/assets/admin/twill-manifest.json' |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function devAsset($file) |
42
|
|
|
{ |
43
|
|
|
if (!$this->devMode()) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$devServerUrl = config('twill.dev_mode_url', 'http://localhost:8080'); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$manifest = $this->readJson( |
51
|
|
|
$devServerUrl . |
52
|
|
|
'/' . |
53
|
|
|
config('twill.manifest_file', 'twill-manifest.json') |
54
|
|
|
); |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
throw new \Exception( |
57
|
|
|
'Twill dev assets manifest is missing. Make sure you are running the npm run serve command inside Twill.' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $devServerUrl . ($manifest[$file] ?? '/' . $file); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
private function readManifest() |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
return Cache::rememberForever('twill-manifest', function () { |
71
|
|
|
return $this->readJson($this->getManifestFilename()); |
72
|
|
|
}); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
throw new \Exception( |
75
|
|
|
'Twill assets manifest is missing. Make sure you published/updated Twill assets using the "php artisan twill:update" command.' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function readJson($fileName) |
81
|
|
|
{ |
82
|
|
|
return json_decode(file_get_contents($fileName), true); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function devMode() |
86
|
|
|
{ |
87
|
|
|
return app()->environment('local', 'development') && |
|
|
|
|
88
|
|
|
config('twill.dev_mode', false); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.