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,10 +5,12 @@ 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
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
@ -18,39 +20,39 @@ class PluginManager {
* Constants * Constants
*/ */
const TABLE_PLUGINS = 'mc_plugins'; const TABLE_PLUGINS = 'mc_plugins';
/* /*
* 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();
/** /**
* Construct plugin manager * Construct plugin manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$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);
} }
/** /**
* Initialize necessary database tables * Initialize necessary database tables
* *
* @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;
@ -75,23 +77,23 @@ class PluginManager {
/** /**
* Check if the plugin is running * Check if the plugin is running
* *
* @param string $pluginClass * @param string $pluginClass
* @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]);
} }
/** /**
* Check if the given class implements the plugin interface * Check if the given class implements the plugin interface
* *
* @param string $pluginClass * @param string $pluginClass
* @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;
} }
@ -100,12 +102,12 @@ class PluginManager {
/** /**
* Add the class to array of loaded plugin classes * Add the class to array of loaded plugin classes
* *
* @param string $pluginClass * @param string $pluginClass
* @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;
} }
@ -119,7 +121,7 @@ class PluginManager {
/** /**
* Activate and start the plugin with the given name * Activate and start the plugin with the given name
* *
* @param string $pluginClass * @param string $pluginClass
* @param string $adminLogin * @param string $adminLogin
* @return bool * @return bool
@ -135,45 +137,56 @@ 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]);
$this->savePluginStatus($pluginClass, false); $this->savePluginStatus($pluginClass, false);
return false; return false;
} }
$this->savePluginStatus($pluginClass, true); $this->savePluginStatus($pluginClass, true);
return true; return true;
} }
/** /**
* Deactivate the plugin with the given class * Deactivate the plugin with the given class
* *
* @param string $pluginClass * @param string $pluginClass
* @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;
} }
@ -183,43 +196,43 @@ class PluginManager {
*/ */
public function loadPlugins() { public function loadPlugins() {
$pluginsDirectory = ManiaControlDir . '/plugins/'; $pluginsDirectory = ManiaControlDir . '/plugins/';
$classesBefore = get_declared_classes(); $classesBefore = get_declared_classes();
$this->loadPluginFiles($pluginsDirectory); $this->loadPluginFiles($pluginsDirectory);
$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;
} }
$this->addPluginClass($className); $this->addPluginClass($className);
$className::prepare($this->maniaControl); $className::prepare($this->maniaControl);
if ($this->isPluginActive($className)) { if ($this->isPluginActive($className)) {
continue; continue;
} }
if (!$this->getSavedPluginStatus($className)) { if (!$this->getSavedPluginStatus($className)) {
continue; continue;
} }
$this->activatePlugin($className); $this->activatePlugin($className);
} }
} }
/** /**
* Load all Plugin Files from the Directory * Load all Plugin Files from the Directory
* *
* @param string $directory * @param string $directory
*/ */
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;
} }
$filePath = $directory . $pluginFile; $filePath = $directory . $pluginFile;
if (is_file($filePath)) { if (is_file($filePath)) {
$success = include_once $filePath; $success = include_once $filePath;
@ -228,7 +241,7 @@ class PluginManager {
} }
continue; continue;
} }
$dirPath = $directory . $pluginFile; $dirPath = $directory . $pluginFile;
if (is_dir($dirPath)) { if (is_dir($dirPath)) {
$this->loadPluginFiles($dirPath . '/'); $this->loadPluginFiles($dirPath . '/');
@ -239,7 +252,7 @@ class PluginManager {
/** /**
* Returns a Plugin if it is activated * Returns a Plugin if it is activated
* *
* @param string $pluginClass * @param string $pluginClass
* @return Plugin * @return Plugin
*/ */
@ -252,7 +265,7 @@ class PluginManager {
/** /**
* Get all declared plugin class names * Get all declared plugin class names
* *
* @return array * @return array
*/ */
public function getPluginClasses() { public function getPluginClasses() {
@ -261,7 +274,7 @@ class PluginManager {
/** /**
* Get all active plugins * Get all active plugins
* *
* @return array * @return array
*/ */
public function getActivePlugins() { public function getActivePlugins() {
@ -270,13 +283,13 @@ 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;
@ -303,15 +316,15 @@ class PluginManager {
/** /**
* Get plugin status from database * Get plugin status from database
* *
* @param string $className * @param string $className
* @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;
@ -340,29 +353,29 @@ 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;
} }
} }