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