diff --git a/app/Events/PluginWasDisabled.php b/app/Events/PluginWasDisabled.php new file mode 100644 index 00000000..09273da0 --- /dev/null +++ b/app/Events/PluginWasDisabled.php @@ -0,0 +1,21 @@ +plugin = $plugin; + } + +} diff --git a/app/Events/PluginWasEnabled.php b/app/Events/PluginWasEnabled.php new file mode 100644 index 00000000..ab263779 --- /dev/null +++ b/app/Events/PluginWasEnabled.php @@ -0,0 +1,21 @@ +plugin = $plugin; + } + +} diff --git a/app/Providers/PluginServiceProvider.php b/app/Providers/PluginServiceProvider.php index 67a1c81a..ef908489 100644 --- a/app/Providers/PluginServiceProvider.php +++ b/app/Providers/PluginServiceProvider.php @@ -2,12 +2,25 @@ namespace App\Providers; +use Event; +use App\Events; use Illuminate\Support\Str; use App\Services\PluginManager; 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. * @@ -33,6 +46,7 @@ class PluginServiceProvider extends ServiceProvider $loader->addNamespace($plugin->getNameSpace(), $plugin->getPath()."/lang"); } + $this->registerPluginCallbackListener(); $this->registerClassAutoloader($src_paths); $bootstrappers = $plugins->getEnabledBootstrappers(); @@ -44,6 +58,20 @@ class PluginServiceProvider extends ServiceProvider } } + protected function registerPluginCallbackListener() + { + Event::listen(array_keys($this->eventCallbackMap), 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)]); + + return $callback ? call_user_func($callback, $event->plugin) : null; + } + }); + } + /** * Register any application services. * diff --git a/app/Services/PluginManager.php b/app/Services/PluginManager.php index da220ed3..75cb18b1 100644 --- a/app/Services/PluginManager.php +++ b/app/Services/PluginManager.php @@ -2,9 +2,8 @@ namespace App\Services; +use App\Events; use Illuminate\Support\Arr; -use App\Events\PluginWasEnabled; -use App\Events\PluginWasDisabled; use Illuminate\Support\Collection; use App\Events\PluginWasUninstalled; use Illuminate\Filesystem\Filesystem; @@ -131,7 +130,7 @@ class PluginManager $plugin->setEnabled(true); - // $this->dispatcher->fire(new PluginWasEnabled($plugin)); + $this->dispatcher->fire(new Events\PluginWasEnabled($plugin)); } } @@ -153,7 +152,7 @@ class PluginManager $plugin->setEnabled(false); - // $this->dispatcher->fire(new PluginWasDisabled($plugin)); + $this->dispatcher->fire(new Events\PluginWasDisabled($plugin)); } } @@ -173,7 +172,7 @@ class PluginManager // refresh plugin list $this->plugins = null; - // $this->dispatcher->fire(new PluginWasUninstalled($plugin)); + $this->dispatcher->fire(new Events\PluginWasDeleted($plugin)); } /**