Register service providers of plugins

This commit is contained in:
Pig Fang 2019-08-16 14:56:47 +08:00
parent 43ede5b274
commit 72aa2c39ac
5 changed files with 52 additions and 9 deletions

View File

@ -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.
*

View File

@ -1,5 +1,10 @@
{
"name": "fake",
"version": "0.0.0",
"namespace": "Fake"
"namespace": "Fake",
"enchants": {
"providers": [
"Fake"
]
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Fake;
use Illuminate\Support\ServiceProvider;
class FakeServiceProvider extends ServiceProvider
{
public function boot()
{
event('provider.loaded');
}
}

View File

@ -1,7 +0,0 @@
<?php
namespace Fake;
class Faker
{
}

View File

@ -341,7 +341,22 @@ class PluginManagerTest extends TestCase
$this->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' => '[]']);