Skip to content

Commit

Permalink
Utilize Laravel's Container for Dependency Resolution in LivewireServ…
Browse files Browse the repository at this point in the history
…iceProvider

Livewire Reference: livewire#7076
Jetstream Reference: laravel/jetstream#1390
The required update in `laravel/jetstream` has been merged into core, and tagged in [v4.0.4](https://github.com/laravel/jetstream/releases/tag/v4.0.4)

-----

This pull request updates the LivewireServiceProvider class to utilize Laravel's container for resolving dependencies and instantiating objects, improving modularity and flexibility for app developers. With this change, developers can easily override core Livewire functionalities in their service providers as needed, such as Mechanisms and Features.

These changes have been made with the aim of maintaining backward compatibility and ensuring that there are no side effects or incompatibilities.

-----

Code snippet of the improvement and reasons why it's useful:

```php
namespace App;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(\Livewire\Mechanisms\HandleRequests\HandleRequests::class, \App\HandleRequests::class);
    }
}
```

Example of a `HandleRequests` Mechanism override, changing the mechanism boot logic. This is just an example, but there's many other use cases.
```php
namespace App;

use Livewire\Mechanisms\HandleRequests\HandleRequests as BaseHandleRequests;

class HandleRequests extends BaseHandleRequests
{
    function boot()
    {
        // ...
    }
}

```

Thanks for your consideration! 🙌
  • Loading branch information
Omranic authored Oct 29, 2023
1 parent b008a9e commit 2fe128f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/LivewireServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function registerConfig()

protected function bootEventBus()
{
(new \Livewire\EventBus)->boot();
app(\Livewire\EventBus::class)->boot();
}

protected function getMechanisms()
Expand All @@ -61,7 +61,7 @@ protected function getMechanisms()
protected function registerMechanisms()
{
foreach ($this->getMechanisms() as $mechanism) {
(new $mechanism)->register($this);
app($mechanism)->register($this);
}
}

Expand All @@ -74,7 +74,7 @@ protected function bootMechanisms()
}

foreach ($this->getMechanisms() as $mechanism) {
(new $mechanism)->boot($this);
app($mechanism)->boot($this);
}
}

Expand Down

0 comments on commit 2fe128f

Please sign in to comment.