use IoC container to call plugin callbacks

This commit is contained in:
printempw 2017-01-20 21:36:29 +08:00
parent c16ae91de0
commit b13552e5d1

View File

@ -10,17 +10,6 @@ use Illuminate\Support\ServiceProvider;
class PluginServiceProvider extends ServiceProvider
{
/**
* Map of event class names to callback names.
*
* @var array
*/
protected $eventCallbackMap = [
Events\PluginWasEnabled::class => 'enabled',
Events\PluginWasDeleted::class => 'deleted',
Events\PluginWasDisabled::class => 'disabled'
];
/**
* Bootstrap any application services.
*
@ -60,14 +49,18 @@ class PluginServiceProvider extends ServiceProvider
protected function registerPluginCallbackListener()
{
Event::listen(array_keys($this->eventCallbackMap), function ($event) {
Event::listen([
Events\PluginWasEnabled::class,
Events\PluginWasDeleted::class,
Events\PluginWasDisabled::class,
], function ($event) {
// call callback functions of plugin
if (file_exists($filename = $event->plugin->getPath()."/callbacks.php")) {
$callbacks = require $filename;
$callback = array_get($callbacks, $this->eventCallbackMap[get_class($event)]);
$callback = array_get($callbacks, get_class($event));
return $callback ? call_user_func($callback, $event->plugin) : null;
return $callback ? app()->call($callback, [$event->plugin]) : null;
}
});
}