proper unregistering of deactivated plugins

This commit is contained in:
Steffen Schröder 2014-04-28 17:53:54 +02:00
parent 2befc24d17
commit cf78a2a141

View File

@ -5,6 +5,8 @@ namespace ManiaControl\Plugins;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
/**
* Class managing Plugins
@ -23,8 +25,8 @@ class PluginManager {
* Private Properties
*/
private $maniaControl = null;
private $pluginMenu = null;
private $pluginInstallMenu = null;
private $pluginMenu = null;
private $pluginInstallMenu = null;
private $activePlugins = array();
private $pluginClasses = array();
@ -40,8 +42,8 @@ class PluginManager {
$this->pluginMenu = new PluginMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginMenu);
$this->pluginInstallMenu = new PluginInstallMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginInstallMenu);
$this->pluginInstallMenu = new PluginInstallMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginInstallMenu);
}
/**
@ -50,7 +52,7 @@ class PluginManager {
* @return bool
*/
private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$mysqli = $this->maniaControl->database->mysqli;
$pluginsTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLUGINS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT,
`className` varchar(100) NOT NULL,
@ -59,7 +61,7 @@ class PluginManager {
PRIMARY KEY (`index`),
UNIQUE KEY `className` (`className`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
$tableStatement = $mysqli->prepare($pluginsTableQuery);
$tableStatement = $mysqli->prepare($pluginsTableQuery);
if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
@ -80,7 +82,7 @@ class PluginManager {
* @return bool
*/
public function isPluginActive($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
$pluginClass = $this->getPluginClass($pluginClass);
return isset($this->activePlugins[$pluginClass]);
}
@ -91,7 +93,7 @@ class PluginManager {
* @return bool
*/
public function isPluginClass($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
$pluginClass = $this->getPluginClass($pluginClass);
if (!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) {
return false;
}
@ -105,7 +107,7 @@ class PluginManager {
* @return bool
*/
public function addPluginClass($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
$pluginClass = $this->getPluginClass($pluginClass);
if (in_array($pluginClass, $this->pluginClasses)) {
return false;
}
@ -135,12 +137,15 @@ class PluginManager {
return false;
}
$plugin = new $pluginClass();
/** @var Plugin $plugin */
/**
* @var Plugin $plugin
*/
$this->activePlugins[$pluginClass] = $plugin;
$this->savePluginStatus($pluginClass, true);
try {
$plugin->load($this->maniaControl);
} catch(\Exception $e) {
}
catch (\Exception $e) {
$this->maniaControl->chat->sendError('Error while plugin activating ' . $pluginClass . ': ' . $e->getMessage(), $adminLogin);
$this->maniaControl->log('Error while plugin activation: ' . $pluginClass . ': ' . $e->getMessage());
unset($this->activePlugins[$pluginClass]);
@ -159,21 +164,29 @@ class PluginManager {
* @return bool
*/
public function deactivatePlugin($pluginClass) {
$pluginClass = $this->getPluginClass($pluginClass);
$pluginClass = $this->getPluginClass($pluginClass);
if (!$this->isPluginActive($pluginClass)) {
return false;
}
$plugin = $this->activePlugins[$pluginClass];
/** @var Plugin $plugin */
unset($this->activePlugins[$pluginClass]);
/**
* @var Plugin $plugin
*/
$plugin->unload();
unset($this->activePlugins[$pluginClass]);
if ($plugin instanceof CallbackListener) {
$this->maniaControl->callbackManager->unregisterCallbackListener($plugin);
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin);
}
if ($plugin instanceof CommandListener) {
$this->maniaControl->commandManager->unregisterCommandListener($plugin);
}
if ($plugin instanceof ManialinkPageAnswerListener) {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
}
if ($plugin instanceof TimerListener) {
$this->maniaControl->timerManager->unregisterTimerListenings($plugin);
}
$this->savePluginStatus($pluginClass, false);
return true;
}
@ -189,7 +202,7 @@ class PluginManager {
$classesAfter = get_declared_classes();
$newClasses = array_diff($classesAfter, $classesBefore);
foreach($newClasses as $className) {
foreach ($newClasses as $className) {
if (!$this->isPluginClass($className)) {
continue;
}
@ -215,7 +228,7 @@ class PluginManager {
*/
public function loadPluginFiles($directory = '') {
$pluginFiles = scandir($directory);
foreach($pluginFiles as $pluginFile) {
foreach ($pluginFiles as $pluginFile) {
if (stripos($pluginFile, '.') === 0) {
continue;
}
@ -272,11 +285,11 @@ class PluginManager {
* Save plugin status in database
*
* @param string $className
* @param bool $active
* @param bool $active
* @return bool
*/
private function savePluginStatus($className, $active) {
$mysqli = $this->maniaControl->database->mysqli;
$mysqli = $this->maniaControl->database->mysqli;
$pluginStatusQuery = "INSERT INTO `" . self::TABLE_PLUGINS . "` (
`className`,
`active`
@ -284,7 +297,7 @@ class PluginManager {
?, ?
) ON DUPLICATE KEY UPDATE
`active` = VALUES(`active`);";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
@ -308,10 +321,10 @@ class PluginManager {
* @return bool
*/
public function getSavedPluginStatus($className) {
$mysqli = $this->maniaControl->database->mysqli;
$mysqli = $this->maniaControl->database->mysqli;
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
WHERE `className` = ?;";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
@ -341,28 +354,28 @@ class PluginManager {
/**
* Fetch the plugin list of the ManiaControl Website
*
* @param $function
* @param $function
* @param bool $ignoreVersion
*/
public function fetchPluginList($function) {
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
$this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use (&$function) {
$this->maniaControl->fileReader->loadFile($url, function ($dataJson, $error) use(&$function) {
$data = json_decode($dataJson);
call_user_func($function, $data, $error);
});
}
/**
* Get the Class of the Plugin
*
* @param mixed $pluginClass
* @return string
*/
private function getPluginClass($pluginClass) {
if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass);
}
return (string) $pluginClass;
}
/**
* Get the Class of the Plugin
*
* @param mixed $pluginClass
* @return string
*/
private function getPluginClass($pluginClass) {
if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass);
}
return (string) $pluginClass;
}
}