Store plugin initialization status in the database

This commit is contained in:
Fedor A. Fetisov 2016-12-28 17:30:33 +03:00
parent 57afba72e8
commit cf76dd744d
3 changed files with 29 additions and 3 deletions

View File

@ -81,6 +81,7 @@ class PluginInfo
$state->version = false; $state->version = false;
$state->installed = false; $state->installed = false;
$state->enabled = false; $state->enabled = false;
$state->initialized = false;
} }
$this->pluginState = $state; $this->pluginState = $state;
} }
@ -209,6 +210,16 @@ class PluginInfo
return new $plugin_class($configs); return new $plugin_class($configs);
} }
/**
* Checks if the plugin is initialized.
*
* @return bool
*/
public function isInitialized()
{
return $this->getState()->initialized;
}
/** /**
* Checks if the plugin is enabled. * Checks if the plugin is enabled.
* *

View File

@ -181,12 +181,22 @@ class PluginManager
$this->loadedPlugins[$name] = $instance; $this->loadedPlugins[$name] = $instance;
$running_queue[$instance->getWeight() . "_" . $offset] = $instance; $running_queue[$instance->getWeight() . "_" . $offset] = $instance;
$offset++; $offset++;
if (!$plugin_info->isInitialized()) {
$plugin_info->getState()->initialized = true;
$plugin_info->getState()->save();
# TODO: clear cache
}
} else { } else {
// The plugin cannot be loaded. Just skip it. // The plugin cannot be loaded. Just skip it.
trigger_error( trigger_error(
"Plugin '{$name}' was not initialized correctly!", "Plugin '{$name}' was not initialized correctly!",
E_USER_WARNING E_USER_WARNING
); );
if ($plugin_info->isInitialized()) {
$plugin_info->getState()->initialized = false;
$plugin_info->getState()->save();
# TODO: clear cache
}
} }
} }
@ -256,6 +266,7 @@ class PluginManager
} }
$plugin->getState()->enabled = false; $plugin->getState()->enabled = false;
$plugin->getState()->initialized = false;
$plugin->getState()->save(); $plugin->getState()->save();
return true; return true;

View File

@ -193,6 +193,7 @@ class State
$this->version = null; $this->version = null;
$this->installed = false; $this->installed = false;
$this->enabled = false; $this->enabled = false;
$this->initialized = false;
} }
/** /**
@ -205,13 +206,14 @@ class State
if (!$this->id) { if (!$this->id) {
// This state is new. // This state is new.
$db->query( $db->query(
("INSERT INTO {plugin} (name, version, installed, enabled) " ("INSERT INTO {plugin} (name, version, installed, enabled, initialized) "
. "VALUES (:name, :version, :installed, :enabled)"), . "VALUES (:name, :version, :installed, :enabled, :initialized)"),
array( array(
':name' => $this->pluginName, ':name' => $this->pluginName,
':version' => $this->version, ':version' => $this->version,
':installed' => (int)$this->installed, ':installed' => (int)$this->installed,
':enabled' => (int)$this->enabled, ':enabled' => (int)$this->enabled,
':initialized' => (int)$this->initialized,
) )
); );
$this->id = $db->insertedId(); $this->id = $db->insertedId();
@ -219,13 +221,14 @@ class State
// Update existing state // Update existing state
$db->query( $db->query(
("UPDATE {plugin} SET name = :name, version = :version, " ("UPDATE {plugin} SET name = :name, version = :version, "
. "installed = :installed, enabled = :enabled WHERE id = :id"), . "installed = :installed, enabled = :enabled, initialized = :initialized WHERE id = :id"),
array( array(
':id' => $this->id, ':id' => $this->id,
':name' => $this->pluginName, ':name' => $this->pluginName,
':version' => $this->version, ':version' => $this->version,
':installed' => (int)$this->installed, ':installed' => (int)$this->installed,
':enabled' => (int)$this->enabled, ':enabled' => (int)$this->enabled,
':initialized' => (int)$this->initialized,
) )
); );
} }
@ -261,5 +264,6 @@ class State
$this->version = $db_fields['version']; $this->version = $db_fields['version'];
$this->enabled = (bool)$db_fields['enabled']; $this->enabled = (bool)$db_fields['enabled'];
$this->installed = (bool)$db_fields['installed']; $this->installed = (bool)$db_fields['installed'];
$this->initialized = (bool)$db_fields['initialized'];
} }
} }