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