Fix event of plugin.versionChanged

This commit is contained in:
Pig Fang 2019-09-09 23:08:03 +08:00
parent a75e116510
commit 65111d3609
6 changed files with 21 additions and 28 deletions

View File

@ -1,21 +0,0 @@
<?php
namespace App\Events;
use App\Services\Plugin;
class PluginVersionChanged extends Event
{
public $plugin;
/**
* Create a new event instance.
*
* @param Plugin $plugin
* @return void
*/
public function __construct(Plugin $plugin)
{
$this->plugin = $plugin;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Listeners;
use App\Services\Plugin;
use Illuminate\Filesystem\Filesystem;
class CopyPluginAssets
@ -18,7 +19,7 @@ class CopyPluginAssets
public function handle($event)
{
$plugin = $event->plugin;
$plugin = $event instanceof Plugin ? $event : $event->plugin;
$dir = public_path('plugins/'.$plugin->name);
$this->filesystem->deleteDirectory($dir);

View File

@ -25,7 +25,7 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\CopyPluginAssets',
'App\Listeners\GeneratePluginTranslations',
],
'App\Events\PluginVersionChanged' => [
'plugin.versionChanged' => [
'App\Listeners\CopyPluginAssets',
'App\Listeners\GeneratePluginTranslations',
],

View File

@ -82,6 +82,7 @@ class PluginManager
return [$item['name'] => ['version' => $item['version']]];
});
$plugins = collect();
$versionChanged = [];
$this->getPluginsDirs()
->flatMap(function ($directory) {
@ -91,7 +92,7 @@ class PluginManager
->filter(function ($directory) {
return $this->filesystem->exists($directory.DIRECTORY_SEPARATOR.'package.json');
})
->each(function ($directory) use (&$plugins) {
->each(function ($directory) use (&$plugins, &$versionChanged) {
$manifest = json_decode(
$this->filesystem->get($directory.DIRECTORY_SEPARATOR.'package.json'),
true
@ -117,12 +118,18 @@ class PluginManager
$this->enabled->get($name)['version']
)) {
$this->enabled->put($name, ['version' => $manifest['version']]);
$this->dispatcher->dispatch(new Events\PluginVersionChanged($plugin));
$versionChanged[] = $plugin;
}
}
});
$this->plugins = $plugins;
if (count($versionChanged) > 0) {
$this->saveEnabled();
}
array_walk($versionChanged, function ($plugin) {
$this->dispatcher->dispatch('plugin.versionChanged', [$plugin]);
});
return $plugins;
}

View File

@ -23,6 +23,6 @@ class CopyPluginAssetsTest extends TestCase
->once();
});
event(new \App\Events\PluginVersionChanged($plugin));
event('plugin.versionChanged', [$plugin]);
}
}

View File

@ -117,6 +117,12 @@ class PluginManagerTest extends TestCase
$mock->shouldReceive('get')
->with('plugins_enabled', '[]')
->andReturn(json_encode([['name' => 'mayaka', 'version' => '0.0.0']]));
$mock->shouldReceive('set')
->with(
'plugins_enabled',
json_encode([['name' => 'mayaka', 'version' => '0.1.0']])
)
->once();
});
$this->mock(Filesystem::class, function ($mock) {
$mock->shouldReceive('directories')
@ -149,8 +155,8 @@ class PluginManagerTest extends TestCase
app()->forgetInstance(PluginManager::class);
resolve(PluginManager::class)->boot();
Event::assertDispatched(\App\Events\PluginVersionChanged::class, function ($event) {
$this->assertEquals('0.1.0', $event->plugin->version);
Event::assertDispatched('plugin.versionChanged', function ($eventName, $payload) {
$this->assertEquals('0.1.0', $payload[0]->version);
return true;
});