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