diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index a732337f..3292ae61 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -141,6 +141,7 @@ class PluginManager ); $this->loadVendor($enabled); $this->loadViewsAndTranslations($enabled); + $this->registerServiceProviders($enabled); $this->loadBootstrapper($enabled); $this->registerLifecycleHooks(); @@ -202,6 +203,22 @@ class PluginManager }); } + /** + * @param Collection $enabled + */ + protected function registerServiceProviders($enabled) + { + $enabled->each(function ($plugin) { + $providers = Arr::get($plugin->getManifest(), 'enchants.providers', []); + array_walk($providers, function ($provider) use ($plugin) { + $class = Str::start(Str::finish($provider, 'ServiceProvider'), $plugin->namespace.'\\'); + if (class_exists($class)) { + $this->app->register($class); + } + }); + }); + } + /** * Load plugin's bootstrapper. * diff --git a/storage/mocks/fake-plugin/package.json b/storage/mocks/fake-plugin/package.json index c7540d8c..865571cb 100644 --- a/storage/mocks/fake-plugin/package.json +++ b/storage/mocks/fake-plugin/package.json @@ -1,5 +1,10 @@ { "name": "fake", "version": "0.0.0", - "namespace": "Fake" + "namespace": "Fake", + "enchants": { + "providers": [ + "Fake" + ] + } } diff --git a/storage/mocks/fake-plugin/src/FakeServiceProvider.php b/storage/mocks/fake-plugin/src/FakeServiceProvider.php new file mode 100644 index 00000000..660b63ff --- /dev/null +++ b/storage/mocks/fake-plugin/src/FakeServiceProvider.php @@ -0,0 +1,13 @@ +assertFalse(class_exists('Fake\Faker')); $manager = $this->rebootPluginManager(app('plugins')); - $this->assertTrue(class_exists('Fake\Faker')); + $this->assertTrue(class_exists('Fake\FakeServiceProvider')); + + config(['plugins.directory' => $dir]); + option(['plugins_enabled' => '[]']); + } + + public function testRegisterServiceProviders() + { + Event::fake(); + + $dir = config('plugins.directory'); + config(['plugins.directory' => storage_path('mocks')]); + option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]); + + $manager = $this->rebootPluginManager(app('plugins')); + Event::assertDispatched('provider.loaded'); config(['plugins.directory' => $dir]); option(['plugins_enabled' => '[]']);