add support for callback of enabling/disabling/deleting plugins

This commit is contained in:
printempw 2017-01-14 21:30:05 +08:00
parent 10a9397fd0
commit 9486f19f5d
4 changed files with 74 additions and 5 deletions

View File

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

View File

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

View File

@ -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.
*

View File

@ -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));
}
/**