fix notifying failed plugin

This commit is contained in:
Pig Fang 2020-03-10 15:12:03 +08:00
parent b66a48181f
commit dcd48a086c
4 changed files with 15 additions and 12 deletions

View File

@ -3,6 +3,7 @@
namespace App\Listeners;
use Event;
use App\Models\User;
class NotifyFailedPlugin
{
@ -10,15 +11,11 @@ class NotifyFailedPlugin
{
$plugin = $event->plugin;
Event::listen(\App\Events\RenderingFooter::class, function ($event) use ($plugin) {
/** @var User */
$user = auth()->user();
if ($user && $user->isAdmin()) {
$options = json_encode([
'type' => 'error',
'message' => trans('errors.plugins.boot', ['plugin' => trans($plugin->title)]),
'duration' => 0,
'showClose' => true,
]);
$event->addContent('<script>blessing.ui.message('.$options.')</script>');
$message = trans('errors.plugins.boot', ['plugin' => trans($plugin->title)]);
$event->addContent("<script>blessing.notify.toast.error('$message')</script>");
}
});
}

View File

@ -34,7 +34,7 @@ class User extends Authenticatable implements JWTSubject
protected $hidden = ['password', 'remember_token'];
public function isAdmin()
public function isAdmin(): bool
{
return $this->permission >= static::ADMIN;
}

View File

@ -243,13 +243,18 @@ class PluginManager
$this->app->call($this->filesystem->getRequire($path), ['plugin' => $plugin]);
} catch (\Throwable $th) {
report($th);
$this->dispatcher->dispatch(new Events\PluginBootFailed($plugin));
// @codeCoverageIgnoreStart
if (config('app.debug')) {
throw $th;
}
// @codeCoverageIgnoreEnd
if (is_a($th, \Exception::class)) {
$handler = $this->app->make(\App\Exceptions\Handler::class);
if (!$handler->shouldReport($th)) {
throw $th;
}
}
$this->dispatcher->dispatch(new Events\PluginBootFailed($plugin));
}
}
}

View File

@ -3,6 +3,7 @@
namespace Tests;
use App\Events;
use App\Models\User;
use App\Services\Plugin;
class NotifyFailedPluginTest extends TestCase
@ -16,14 +17,14 @@ class NotifyFailedPluginTest extends TestCase
event(new Events\RenderingFooter($content));
$this->assertCount(0, $content);
$this->actAs('normal');
$this->actingAs(factory(User::class)->make());
event(new Events\PluginBootFailed($plugin));
event(new Events\RenderingFooter($content));
$this->assertCount(0, $content);
$this->actAs('admin');
$this->actingAs(factory(User::class)->states('admin')->make());
event(new Events\PluginBootFailed($plugin));
event(new Events\RenderingFooter($content));
$this->assertStringContainsString('blessing.ui.message', $content[0]);
$this->assertStringContainsString('blessing.notify.toast', $content[0]);
}
}