Refactor plugin system (part 2)

This commit is contained in:
Pig Fang 2019-08-12 10:52:40 +08:00
parent 93183debda
commit d92e1738dc
2 changed files with 25 additions and 4 deletions

View File

@ -94,14 +94,22 @@ class PluginManager
]), 5); ]), 5);
} }
$plugins->put($name, new Plugin($directory, $manifest)); $plugin = new Plugin($directory, $manifest);
if ($this->enabled->contains('name', $name)) {
$plugin->setEnabled(true);
}
$plugins->put($name, $plugin);
}); });
// disable unsatisfied here // disable unsatisfied here
$this->registerAutoload($plugins->mapWithKeys(function ($plugin) { $this->registerAutoload(
return [$plugin->namespace => $plugin->getPath().'/src']; $plugins->filter(function ($plugin) {
})); return $plugin->isEnabled();
})->mapWithKeys(function ($plugin) {
return [$plugin->namespace => $plugin->getPath().'/src'];
})
);
$this->booted = true; $this->booted = true;
} }

View File

@ -29,6 +29,17 @@ class PluginManagerTest extends TestCase
app('plugins')->boot(); app('plugins')->boot();
} }
public function testDoNotLoadDisabled()
{
$dir = config('plugins.directory');
config(['plugins.directory' => storage_path('mocks')]);
$manager = $this->rebootPluginManager(app('plugins'));
$this->assertFalse(class_exists('Fake\Faker'));
config(['plugins.directory' => $dir]);
}
public function testReportDuplicatedPlugins() public function testReportDuplicatedPlugins()
{ {
$this->mock(Filesystem::class, function ($mock) { $this->mock(Filesystem::class, function ($mock) {
@ -75,11 +86,13 @@ class PluginManagerTest extends TestCase
{ {
$dir = config('plugins.directory'); $dir = config('plugins.directory');
config(['plugins.directory' => storage_path('mocks')]); config(['plugins.directory' => storage_path('mocks')]);
option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]);
$this->assertFalse(class_exists('Fake\Faker')); $this->assertFalse(class_exists('Fake\Faker'));
$manager = $this->rebootPluginManager(app('plugins')); $manager = $this->rebootPluginManager(app('plugins'));
$this->assertTrue(class_exists('Fake\Faker')); $this->assertTrue(class_exists('Fake\Faker'));
config(['plugins.directory' => $dir]); config(['plugins.directory' => $dir]);
option(['plugins_enabled' => '[]']);
} }
} }