-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlasherSweetAlertServiceProvider.php
65 lines (57 loc) · 2.06 KB
/
FlasherSweetAlertServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Flasher\SweetAlert\Laravel;
use Flasher\Laravel\Support\PluginServiceProvider;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
use Flasher\SweetAlert\Prime\SweetAlertPlugin;
/**
* FlasherSweetAlertServiceProvider - Laravel service provider for SweetAlert2 integration.
*
* This service provider registers the SweetAlert2 plugin with Laravel's service container
* and sets up the Livewire integration for interactive dialogs. It extends the base plugin
* service provider to inherit common registration logic while providing SweetAlert-specific
* plugin implementation and event listeners.
*
* Design patterns:
* - Service Provider: Implements Laravel's service provider pattern
* - Factory Method: Creates the plugin instance
* - Observer: Registers event listeners for framework integration
*/
final class FlasherSweetAlertServiceProvider extends PluginServiceProvider
{
/**
* Creates the SweetAlert plugin instance.
*
* @return SweetAlertPlugin The SweetAlert plugin instance
*/
public function createPlugin(): SweetAlertPlugin
{
return new SweetAlertPlugin();
}
/**
* Performs additional setup after the service provider is booted.
*
* This method is called after all service providers have been registered.
* It's used here to set up the Livewire integration for interactive dialogs.
*/
protected function afterBoot(): void
{
$this->registerLivewireListener();
}
/**
* Registers the Livewire event listener for SweetAlert dialogs.
*
* This listener enables SweetAlert's interactive dialogs to work with
* Livewire's AJAX-based component updates.
*/
private function registerLivewireListener(): void
{
if (!$this->app->bound('livewire')) {
return;
}
$this->app->extend('flasher.event_dispatcher', static function (EventDispatcherInterface $dispatcher) {
$dispatcher->addListener(new LivewireListener());
return $dispatcher;
});
}
}