From d92e1738dc0cc74ff1ec993134dbbee2767cf2d6 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Mon, 12 Aug 2019 10:52:40 +0800 Subject: [PATCH] Refactor plugin system (part 2) --- app/Services/PluginManager.php | 16 ++++++++++++---- tests/ServicesTest/PluginManagerTest.php | 13 +++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index bc83066d..8a7f886c 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -94,14 +94,22 @@ class PluginManager ]), 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 - $this->registerAutoload($plugins->mapWithKeys(function ($plugin) { - return [$plugin->namespace => $plugin->getPath().'/src']; - })); + $this->registerAutoload( + $plugins->filter(function ($plugin) { + return $plugin->isEnabled(); + })->mapWithKeys(function ($plugin) { + return [$plugin->namespace => $plugin->getPath().'/src']; + }) + ); $this->booted = true; } diff --git a/tests/ServicesTest/PluginManagerTest.php b/tests/ServicesTest/PluginManagerTest.php index a95ef7bd..3d725986 100644 --- a/tests/ServicesTest/PluginManagerTest.php +++ b/tests/ServicesTest/PluginManagerTest.php @@ -29,6 +29,17 @@ class PluginManagerTest extends TestCase 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() { $this->mock(Filesystem::class, function ($mock) { @@ -75,11 +86,13 @@ class PluginManagerTest extends TestCase { $dir = config('plugins.directory'); config(['plugins.directory' => storage_path('mocks')]); + option(['plugins_enabled' => json_encode([['name' => 'fake', 'version' => '0.0.0']])]); $this->assertFalse(class_exists('Fake\Faker')); $manager = $this->rebootPluginManager(app('plugins')); $this->assertTrue(class_exists('Fake\Faker')); config(['plugins.directory' => $dir]); + option(['plugins_enabled' => '[]']); } }