Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add service override events #78

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Concerns/HandlesServiceOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -107,6 +115,8 @@ protected function processOverride(string $overrideClass): static
}
}

ServiceOverrideProcessed::dispatch($override);

return $this;
}

Expand Down Expand Up @@ -218,6 +228,8 @@ protected function bootOverride(string $overrideClass): void

$override->boot($this->app, $this);
$this->bootedOverrides[$overrideClass] = true;

ServiceOverrideBooted::dispatch($override);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Events/ServiceOverrideBooted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace Sprout\Events;

use Sprout\Contracts\ServiceOverride as ServiceOverrideClass;

/**
* Service Override Booted Event
*
* This event is dispatched after a service override has been booted.
*
* @template ServiceOverrideClass of \Sprout\Contracts\ServiceOverride
*
* @package Overrides
*
* @method static self dispatch(object $override)
* @method static self dispatchIf(bool $boolean, object $override)
* @method static self dispatchUnless(bool $boolean, object $override)
*/
final class ServiceOverrideBooted extends ServiceOverrideEvent
{
/**
* @var object<\Sprout\Contracts\ServiceOverride>
* @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;
}
}
18 changes: 18 additions & 0 deletions src/Events/ServiceOverrideEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Sprout\Events;

use Illuminate\Foundation\Events\Dispatchable;

/**
* Service Override Event
*
* This is a base event class for the service override events.
*
* @package Overrides
*/
abstract class ServiceOverrideEvent
{
use Dispatchable;
}
36 changes: 36 additions & 0 deletions src/Events/ServiceOverrideProcessed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace Sprout\Events;

/**
* Service Override Processed Event
*
* This event is dispatched after a service override has been processed.
*
* @template ServiceOverrideClass of \Sprout\Contracts\ServiceOverride
*
* @package Overrides
*
* @method static self dispatch(object $override)
* @method static self dispatchIf(bool $boolean, object $override)
* @method static self dispatchUnless(bool $boolean, object $override)
*/
final class ServiceOverrideProcessed extends ServiceOverrideEvent
{
/**
* @var object<\Sprout\Contracts\ServiceOverride>
* @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;
}
}
31 changes: 31 additions & 0 deletions src/Events/ServiceOverrideProcessing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

namespace Sprout\Events;

/**
* Service Override Processing Event
*
* This event is dispatched before a service override is processed.
*
* @package Overrides
*
* @method static self dispatch(string $override)
* @method static self dispatchIf(bool $boolean, string $override)
* @method static self dispatchUnless(bool $boolean, string $override)
*/
final class ServiceOverrideProcessing extends ServiceOverrideEvent
{
/**
* @var class-string<\Sprout\Contracts\ServiceOverride>
*/
public readonly string $override;

/**
* @param class-string<\Sprout\Contracts\ServiceOverride> $override
*/
public function __construct(string $override)
{
$this->override = $override;
}
}
32 changes: 32 additions & 0 deletions src/Events/ServiceOverrideRegistered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Sprout\Events;

/**
* Service Override Registered Event
*
* This event is dispatched when a service override is registered with
* Sprout.
*
* @package Overrides
*
* @method static self dispatch(string $override)
* @method static self dispatchIf(bool $boolean, string $override)
* @method static self dispatchUnless(bool $boolean, string $override)
*/
final class ServiceOverrideRegistered extends ServiceOverrideEvent
{
/**
* @var class-string<\Sprout\Contracts\ServiceOverride>
*/
public readonly string $override;

/**
* @param class-string<\Sprout\Contracts\ServiceOverride> $override
*/
public function __construct(string $override)
{
$this->override = $override;
}
}
Loading