laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

how to use lumen defer service provider #764

Closed BAN1ce closed 6 years ago

BAN1ce commented 6 years ago

Description:

I want create a defer service provider , according to laravel doc create a provider which not defer.I found lumen function registerDeferredProvider is weird.this function content only one line code return $this->register($provider);.So,No defer ?

Steps To Reproduce:

protected $defer = true;

/**
 * Bootstrap services.
 *
 * @return void
 */
public function boot()
{
    //
}

/**
 * Register services.
 *
 * @return void
 */
public function register()
{
    //
    $this->app->singleton('Hello',function ($app){

        return new \Redis();
    });

}

public function provides()
{
    return ['Hello'];

}
hakuno commented 6 years ago

+1

ifeltsweet commented 6 years ago

No support for deferred service providers in Lumen.

What you can do though, is something similar to what Lumen does with its register...Bindings() methods. You can have your own registering function and a corresponding $availableBindings record. You of course need to extend original Laravel\Lumen\Application class to make this happen. I do this personally and it works great.

What Lumen really needs, is a way to register these lazy bindings without extending original Application. Something like $app->lazyBinding[$abstract] = $closure, granted that it still performs better than $app->bind($abstract, $closure), otherwise it would be a futile exercise.

jtrachez commented 6 years ago

No support for deferred service providers in Lumen.

What you can do though, is something similar to what Lumen does with its register...Bindings() methods. You can have your own registering function and a corresponding $availableBindings record. You of course need to extend original Laravel\Lumen\Application class to make this happen. I do this personally and it works great.

What Lumen really needs, is a way to register these lazy bindings without extending original Application. Something like $app->lazyBinding[$abstract] = $closure, granted that it still performs better than $app->bind($abstract, $closure), otherwise it would be a futile exercise.

Hi,

You have an example of your code for this implemention ? thanks

ifeltsweet commented 6 years ago

Extend Lumen application:

<?php

namespace Acme\Support;

use Illuminate\Support\Arr;
use Laravel\Lumen\Application as LumenApplication;

class Application extends LumenApplication
{
    /**
     * Register bindings for MyService.
     */
    protected function registerMyServiceBindings()
    {
        $this->configure('myservice');
        $this->register('Acme\Providers\MyServiceProvider');
    }

    /**
     * { inherit_doc }.
     */
    protected function registerCacheBindings()
    {
        // parent::registerCacheBindings();
        // Modify original Lumen cache bindings here to suit my application.
    }
}

And then in your "bootstrap/app.php":

$app = new Acme\Support\Application(
    realpath(__DIR__.'/../')
);

$app->availableBindings[\Acme\Contracts\MyService::class] = 'registerMyServiceBindings';

So when doing:

$app->make(\Acme\Contracts\MyService::class)

it will run Acme\Support\Application::registerMyServiceBindings() first and then return whatever you have configured in your service provider.

Notice how you can override original Lumen bindings too.

driesvints commented 6 years ago

Heya, Lumen indeed doesn't allow deferring of service providers at this time.