Skip to content

Instantly share code, notes, and snippets.

@hskrasek
Created February 2, 2017 17:17
Show Gist options
  • Save hskrasek/1df02cc40149d73b298d3ab9048f37d7 to your computer and use it in GitHub Desktop.
Save hskrasek/1df02cc40149d73b298d3ab9048f37d7 to your computer and use it in GitHub Desktop.
Loading deferred service providers with a non-deferred service provider
<?php
return [
'providers' => [
'App\Modules\Foo\FooModuleServiceProvider'
],
];
<?php namespace App\Modules\Foo\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider {
protected $defer = true;
public function register() {
$this->app->bind(FooThingInterface::class, FooThing::class);
}
}
<?php namespace App\Modules\Foo;
use Illuminate\Support\ServiceProvider;
class FooModuleServiceProvider extends ServiceProvider {
protected $defer = false;
public function register() {
$this->app->register('App\Modules\Foo\Providers\RouteServiceProvider'); // Not deferred since routes are always needed
$this->app->register('App\Modules\Foo\Providers\AppServiceProvider'); // Deferred since you don't need all these bindings until you need them
}
}
@hskrasek
Copy link
Author

hskrasek commented Feb 2, 2017

The problem with this setup is that Illuminate\Support\ServiceProvider::isDeferred is only checked when building the manifest file in Illuminate\Foundation\ProviderRepository, so registering a deferred service provider anywhere else but the app.providers array just loads it normally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment