1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Usernotnull\Toast; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Facade; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @see \Usernotnull\Toast\Toast |
12
|
|
|
*/ |
13
|
|
|
class ToastManager extends Facade |
14
|
|
|
{ |
15
|
|
|
public bool $componentRendered = false; |
16
|
|
|
|
17
|
|
|
public static function componentRendered(): bool |
18
|
|
|
{ |
19
|
|
|
return app('toast.manager')->componentRendered; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected static function getFacadeAccessor(): string |
23
|
|
|
{ |
24
|
|
|
return 'toast'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function hasPendingToasts(): bool |
28
|
|
|
{ |
29
|
|
|
return session()->has(config('tall-toasts.session_keys.toasts')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected static function javaScriptAssets(array $options): string |
33
|
|
|
{ |
34
|
|
|
$appUrl = config('toast.asset_url') ?: rtrim($options['asset_url'] ?? '', '/'); |
35
|
|
|
|
36
|
|
|
$manifestContent = File::get(__DIR__ . '/../dist/js/manifest.json'); |
37
|
|
|
|
38
|
|
|
$manifest = json_decode($manifestContent, true); |
39
|
|
|
$versionedFileName = $manifest['/tall-toasts.js']; |
40
|
|
|
|
41
|
|
|
// Default to dynamic `tall-toasts.js` (served by a Laravel route). |
42
|
|
|
$fullAssetPath = "{$appUrl}/toast{$versionedFileName}"; |
43
|
|
|
|
44
|
|
|
$nonce = isset($options['nonce']) ? "nonce=\"{$options['nonce']}\"" : ''; |
45
|
|
|
|
46
|
|
|
// Adding semicolons for this JavaScript is important, |
47
|
|
|
// because it will be minified in production. |
48
|
|
|
return <<<HTML |
49
|
|
|
<script src="{$fullAssetPath}" data-turbo-eval="false" data-turbolinks-eval="false" {$nonce}></script> |
50
|
|
|
<script data-turbo-eval="false" data-turbolinks-eval="false" {$nonce}> |
51
|
|
|
document.addEventListener('alpine:init', () => { |
52
|
|
|
window.Alpine.directive('ToastComponent', window.ToastComponent); |
53
|
|
|
}); |
54
|
|
|
</script> |
55
|
|
|
HTML; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected static function minify(string $subject): ?string |
59
|
|
|
{ |
60
|
|
|
return preg_replace('~(\v|\t|\s{2,})~m', '', $subject); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function pull(): array |
64
|
|
|
{ |
65
|
|
|
return session()->pull(config('tall-toasts.session_keys.toasts'), []); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function pullNextPage(): array |
69
|
|
|
{ |
70
|
|
|
return session()->pull(config('tall-toasts.session_keys.toasts_next_page'), []); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function scripts(array $options = []): string |
74
|
|
|
{ |
75
|
|
|
$debug = config('app.debug'); |
76
|
|
|
|
77
|
|
|
$scripts = self::javaScriptAssets($options); |
78
|
|
|
|
79
|
|
|
$html = $debug ? ['<!-- Toast Scripts -->'] : []; |
80
|
|
|
|
81
|
|
|
$html[] = $debug ? $scripts : self::minify($scripts); |
82
|
|
|
|
83
|
|
|
return implode("\n", $html); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function setComponentRendered(bool $rendered): void |
87
|
|
|
{ |
88
|
|
|
app('toast.manager')->componentRendered = $rendered; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|