Update the structure of plugins_enabled field

This commit is contained in:
Pig Fang 2018-11-21 22:53:27 +08:00
parent 091abbdf69
commit 889e461bd6

View File

@ -39,6 +39,11 @@ class PluginManager
*/ */
protected $plugins; protected $plugins;
/**
* @var Collection
*/
protected $enabled;
public function __construct( public function __construct(
Application $app, Application $app,
OptionRepository $option, OptionRepository $option,
@ -68,7 +73,7 @@ class PluginManager
} }
// traverse plugins dir // traverse plugins dir
while($filename = @readdir($resource)) { while ($filename = @readdir($resource)) {
if ($filename == '.' || $filename == '..') if ($filename == '.' || $filename == '..')
continue; continue;
@ -134,14 +139,18 @@ class PluginManager
*/ */
public function enable($name) public function enable($name)
{ {
if (is_null($this->enabled)) {
$this->convertPluginRecord();
}
if (! $this->isEnabled($name)) { if (! $this->isEnabled($name)) {
$plugin = $this->getPlugin($name); $plugin = $this->getPlugin($name);
$enabled = $this->getEnabled(); $this->enabled->push([
'name' => $name,
$enabled[] = $name; 'version' => $plugin->getVersion(),
]);
$this->setEnabled($enabled); $this->saveEnabled();
$plugin->setEnabled(true); $plugin->setEnabled(true);
@ -156,17 +165,21 @@ class PluginManager
*/ */
public function disable($name) public function disable($name)
{ {
$enabled = $this->getEnabled(); if (is_null($this->enabled)) {
$this->convertPluginRecord();
}
if (($k = array_search($name, $enabled)) !== false) { $rejected = $this->enabled->reject(function ($item) use ($name) {
unset($enabled[$k]); return $item['name'] == $name;
});
if ($rejected->count() !== $this->enabled->count()) {
$plugin = $this->getPlugin($name); $plugin = $this->getPlugin($name);
$this->setEnabled($enabled);
$plugin->setEnabled(false); $plugin->setEnabled(false);
$this->enabled = $rejected;
$this->saveEnabled();
$this->dispatcher->fire(new Events\PluginWasDisabled($plugin)); $this->dispatcher->fire(new Events\PluginWasDisabled($plugin));
} }
} }
@ -198,6 +211,10 @@ class PluginManager
*/ */
public function getEnabledPlugins() public function getEnabledPlugins()
{ {
if (is_null($this->enabled)) {
$this->convertPluginRecord();
}
return $this->getPlugins()->only($this->getEnabled()); return $this->getPlugins()->only($this->getEnabled());
} }
@ -244,19 +261,23 @@ class PluginManager
*/ */
public function getEnabled() public function getEnabled()
{ {
return (array) json_decode($this->option->get('plugins_enabled'), true); $enabled = collect(json_decode($this->option->get('plugins_enabled'), true));
return $enabled->map(function ($item) {
if (is_string($item)) {
return $item;
} else {
return $item['name'];
}
})->values()->toArray();
} }
/** /**
* Persist the currently enabled plugins. * Persist the currently enabled plugins.
*
* @param array $enabled
*/ */
protected function setEnabled(array $enabled) protected function saveEnabled()
{ {
$enabled = array_values(array_unique($enabled)); $this->option->set('plugins_enabled', $this->enabled->values()->toJson());
$this->option->set('plugins_enabled', json_encode($enabled));
// ensure to save options // ensure to save options
$this->option->save(); $this->option->save();
@ -370,4 +391,29 @@ class PluginManager
); );
} }
/**
* Convert `plugins_enabled` field for backward compatibility.
*
* @return $this
*/
protected function convertPluginRecord()
{
$list = collect(json_decode($this->option->get('plugins_enabled'), true));
$this->enabled = $list->map(function ($item) {
if (is_string($item)) {
$plugin = $this->getPlugin($item);
return [
'name' => $item,
'version' => $plugin->getVersion(),
];
} else {
return $item;
}
});
$this->saveEnabled();
return $this;
}
} }