Skip to content

Commit

Permalink
fix(ServiceProvider): Do not use EventServiceProvider as base and rep…
Browse files Browse the repository at this point in the history
…licate $listen

BREAKING CHANGE: add array to $listen to your service provider
  • Loading branch information
pionl committed Jun 23, 2024
1 parent c2bba62 commit 232164b
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Providers/AbstractBaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@

namespace LaraStrict\Providers;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use LaraStrict\Contracts\AppServiceProviderPipeContract;
use LaraStrict\Contracts\CreateAppServiceProviderActionContract;
use LaraStrict\Contracts\RunAppServiceProviderPipesActionContract;
use LaraStrict\Providers\Actions\CreateAppServiceProviderAction;
use LaraStrict\Providers\Entities\AppServiceProviderEntity;
use LogicException;

abstract class AbstractBaseServiceProvider extends EventServiceProvider
/**
* From Laravel 10+ the EventServiceProvider cant be used anymore, it was designed to be used only once
* within app/Providers/EventServiceProvider.php. We are using $listen shortcut so let's use similar implementation.
*/
abstract class AbstractBaseServiceProvider extends ServiceProvider
{
protected AppServiceProviderEntity|null $appServiceProvider = null;

/**
* The event handler mappings for the application.
*
* @var array<string, array<int, string>>
*/
protected array $listen = [];

public function register(): void
{
parent::register();
Expand All @@ -24,12 +38,22 @@ public function register(): void
assert($runPipes instanceof RunAppServiceProviderPipesActionContract);

$runPipes->execute($this->getAppServiceProvider(), $this->registerPipes());

$this->booting(function () {
$events = $this->app->make(Dispatcher::class);
assert($events instanceof Dispatcher);

// Taken from vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php
foreach ($this->listen as $event => $listeners) {
foreach (array_unique($listeners, SORT_REGULAR) as $listener) {
$events->listen($event, $listener);
}
}
});
}

public function boot(): void
{
parent::boot();

$runPipes = $this->app->make(RunAppServiceProviderPipesActionContract::class);
assert($runPipes instanceof RunAppServiceProviderPipesActionContract);

Expand Down

0 comments on commit 232164b

Please sign in to comment.