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

[11.x] Simple scoped bindings container registration #52481

Conversation

freerkminnema
Copy link

In #37521, Laravel introduced "scoped" binding. This PR adds support for a $scopedBindings property on service providers to allow for the registration of simple scoped bindings. It also has support for tersely binding classes that don't have a backing contract, as was introduced for simple singleton bindings in #43469.

For "regular" bindings and singletons, there already exist convenient properties ($bindings and $singletons respectively), that can be used to simplify registration of bindings.

When using Laravel Octane, you regularly encounter situations where you might want to use a scoped binding, but currently you need to implement the register() method when you want to do this.

Before:

class AppServiceProvider extends ServiceProvider
{
    public array $bindings = [
        ServerProvider::class => DigitalOceanServerProvider::class,
    ];
 
    public array $singletons = [
        DowntimeNotifier::class => PingdomDowntimeNotifier::class,
    ];

    public function register()
    {
        $this->app->scoped(RequestScopedClassContract::class, RequestScopedClass::class)
    }
}

When skimming the code above, it feels like there is a fundamental difference between the different types of bindings.

When looking at the code below, the connection between the three types of bindings is more obvious.

After:

class AppServiceProvider extends ServiceProvider
{
    public array $bindings = [
        ServerProvider::class => DigitalOceanServerProvider::class,
    ];
 
    public array $scopedBindings = [
        RequestScopedClassContract::class => RequestScopedClass::class,
    ];
 
    public array $singletons = [
        DowntimeNotifier::class => PingdomDowntimeNotifier::class,
    ];
}

… for convenient registration of scoped bindings, in similar fashion to the existing $bindings and $singletons properties.
@freerkminnema freerkminnema changed the title Simple scoped bindings registration Simple scoped bindings container registration Aug 14, 2024
@freerkminnema freerkminnema changed the title Simple scoped bindings container registration [11.x] Simple scoped bindings container registration Aug 14, 2024
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants