From 88ced3088e43a751601a9d5497df3e0276ce8787 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Mon, 16 Dec 2024 10:53:02 +0000 Subject: [PATCH] feat: Add service override events (#78) * chore: Add service override events * chore: Dispatch service override events --- src/Concerns/HandlesServiceOverrides.php | 12 ++++++++ src/Events/ServiceOverrideBooted.php | 38 ++++++++++++++++++++++++ src/Events/ServiceOverrideEvent.php | 18 +++++++++++ src/Events/ServiceOverrideProcessed.php | 36 ++++++++++++++++++++++ src/Events/ServiceOverrideProcessing.php | 31 +++++++++++++++++++ src/Events/ServiceOverrideRegistered.php | 32 ++++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 src/Events/ServiceOverrideBooted.php create mode 100644 src/Events/ServiceOverrideEvent.php create mode 100644 src/Events/ServiceOverrideProcessed.php create mode 100644 src/Events/ServiceOverrideProcessing.php create mode 100644 src/Events/ServiceOverrideRegistered.php diff --git a/src/Concerns/HandlesServiceOverrides.php b/src/Concerns/HandlesServiceOverrides.php index fbcdc95..3453467 100644 --- a/src/Concerns/HandlesServiceOverrides.php +++ b/src/Concerns/HandlesServiceOverrides.php @@ -9,6 +9,10 @@ use Sprout\Contracts\ServiceOverride; use Sprout\Contracts\Tenancy; use Sprout\Contracts\Tenant; +use Sprout\Events\ServiceOverrideBooted; +use Sprout\Events\ServiceOverrideProcessed; +use Sprout\Events\ServiceOverrideProcessing; +use Sprout\Events\ServiceOverrideRegistered; trait HandlesServiceOverrides { @@ -65,6 +69,8 @@ public function registerOverride(string $overrideClass): static // Flag the service override as being registered $this->registeredOverrides[] = $overrideClass; + ServiceOverrideRegistered::dispatch($overrideClass); + if (is_subclass_of($overrideClass, DeferrableServiceOverride::class)) { $this->registerDeferrableOverride($overrideClass); } else { @@ -88,6 +94,8 @@ public function registerOverride(string $overrideClass): static */ protected function processOverride(string $overrideClass): static { + ServiceOverrideProcessing::dispatch($overrideClass); + // Create a new instance of the override $override = $this->app->make($overrideClass); @@ -107,6 +115,8 @@ protected function processOverride(string $overrideClass): static } } + ServiceOverrideProcessed::dispatch($override); + return $this; } @@ -218,6 +228,8 @@ protected function bootOverride(string $overrideClass): void $override->boot($this->app, $this); $this->bootedOverrides[$overrideClass] = true; + + ServiceOverrideBooted::dispatch($override); } /** diff --git a/src/Events/ServiceOverrideBooted.php b/src/Events/ServiceOverrideBooted.php new file mode 100644 index 0000000..4449a83 --- /dev/null +++ b/src/Events/ServiceOverrideBooted.php @@ -0,0 +1,38 @@ + + * @phpstan-var ServiceOverrideClass + */ + public readonly object $override; + + /** + * @param object<\Sprout\Contracts\ServiceOverride> $override + * + * @phpstan-param ServiceOverrideClass $override + */ + public function __construct(object $override) + { + $this->override = $override; + } +} diff --git a/src/Events/ServiceOverrideEvent.php b/src/Events/ServiceOverrideEvent.php new file mode 100644 index 0000000..52753f0 --- /dev/null +++ b/src/Events/ServiceOverrideEvent.php @@ -0,0 +1,18 @@ + + * @phpstan-var ServiceOverrideClass + */ + public readonly object $override; + + /** + * @param object<\Sprout\Contracts\ServiceOverride> $override + * + * @phpstan-param ServiceOverrideClass $override + */ + public function __construct(object $override) + { + $this->override = $override; + } +} diff --git a/src/Events/ServiceOverrideProcessing.php b/src/Events/ServiceOverrideProcessing.php new file mode 100644 index 0000000..e6c9034 --- /dev/null +++ b/src/Events/ServiceOverrideProcessing.php @@ -0,0 +1,31 @@ + + */ + public readonly string $override; + + /** + * @param class-string<\Sprout\Contracts\ServiceOverride> $override + */ + public function __construct(string $override) + { + $this->override = $override; + } +} diff --git a/src/Events/ServiceOverrideRegistered.php b/src/Events/ServiceOverrideRegistered.php new file mode 100644 index 0000000..d6bea8d --- /dev/null +++ b/src/Events/ServiceOverrideRegistered.php @@ -0,0 +1,32 @@ + + */ + public readonly string $override; + + /** + * @param class-string<\Sprout\Contracts\ServiceOverride> $override + */ + public function __construct(string $override) + { + $this->override = $override; + } +}