plugin class exception handling

This commit is contained in:
kremsy 2014-01-18 10:42:19 +01:00 committed by Steffen Schröder
parent d173a48159
commit fb30706cd4
2 changed files with 52 additions and 44 deletions

View File

@ -5,8 +5,8 @@ namespace ManiaControl\Plugins;
require_once __DIR__ . '/Plugin.php'; require_once __DIR__ . '/Plugin.php';
require_once __DIR__ . '/PluginMenu.php'; require_once __DIR__ . '/PluginMenu.php';
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Manialinks\ManialinkPageAnswerListener;
/** /**
@ -47,7 +47,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) COLLATE utf8_unicode_ci NOT NULL, `className` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
@ -56,13 +56,13 @@ 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;
} }
$tableStatement->execute(); $tableStatement->execute();
if ($tableStatement->error) { if($tableStatement->error) {
trigger_error($tableStatement->error, E_USER_ERROR); trigger_error($tableStatement->error, E_USER_ERROR);
return false; return false;
} }
@ -77,7 +77,7 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function isPluginActive($pluginClass) { public function isPluginActive($pluginClass) {
if (is_object($pluginClass)) { if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
return isset($this->activePlugins[$pluginClass]); return isset($this->activePlugins[$pluginClass]);
@ -90,10 +90,10 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function isPluginClass($pluginClass) { public function isPluginClass($pluginClass) {
if (is_object($pluginClass)) { if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if (!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) { if(!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) {
return false; return false;
} }
return true; return true;
@ -106,13 +106,13 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function addPluginClass($pluginClass) { public function addPluginClass($pluginClass) {
if (is_object($pluginClass)) { if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if (in_array($pluginClass, $this->pluginClasses)) { if(in_array($pluginClass, $this->pluginClasses)) {
return false; return false;
} }
if (!$this->isPluginClass($pluginClass)) { if(!$this->isPluginClass($pluginClass)) {
return false; return false;
} }
array_push($this->pluginClasses, $pluginClass); array_push($this->pluginClasses, $pluginClass);
@ -123,20 +123,28 @@ 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
* @return bool * @return bool
*/ */
public function activatePlugin($pluginClass) { public function activatePlugin($pluginClass, $adminLogin = false) {
if (!is_string($pluginClass)) { if(!is_string($pluginClass)) {
return false; return false;
} }
if (!$this->isPluginClass($pluginClass)) { if(!$this->isPluginClass($pluginClass)) {
return false; return false;
} }
if ($this->isPluginActive($pluginClass)) { if($this->isPluginActive($pluginClass)) {
return false; return false;
} }
$plugin = new $pluginClass(); $plugin = new $pluginClass();
$plugin->load($this->maniaControl); try {
$plugin->load($this->maniaControl);
} catch(\Exception $e) {
$this->maniaControl->chat->sendError('Error on Plugin activation ' . $e->getMessage(), $adminLogin);
$this->log('Error while plugin activation: ' . $e->getMessage());
return false;
}
$this->activePlugins[$pluginClass] = $plugin; $this->activePlugins[$pluginClass] = $plugin;
$this->savePluginStatus($pluginClass, true); $this->savePluginStatus($pluginClass, true);
return true; return true;
@ -149,21 +157,21 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function deactivatePlugin($pluginClass) { public function deactivatePlugin($pluginClass) {
if (is_object($pluginClass)) { if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if (!$this->isPluginActive($pluginClass)) { if(!$this->isPluginActive($pluginClass)) {
return false; return false;
} }
$plugin = $this->activePlugins[$pluginClass]; $plugin = $this->activePlugins[$pluginClass];
unset($this->activePlugins[$pluginClass]); unset($this->activePlugins[$pluginClass]);
$plugin->unload(); $plugin->unload();
$interfaces = class_implements($pluginClass); $interfaces = class_implements($pluginClass);
if (in_array(CallbackListener::CALLBACKLISTENER_INTERFACE, $interfaces)) { if(in_array(CallbackListener::CALLBACKLISTENER_INTERFACE, $interfaces)) {
$this->maniaControl->callbackManager->unregisterCallbackListener($plugin); $this->maniaControl->callbackManager->unregisterCallbackListener($plugin);
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin); $this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin);
} }
if (in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) { if(in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin); $this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
} }
$this->savePluginStatus($pluginClass, false); $this->savePluginStatus($pluginClass, false);
@ -175,27 +183,27 @@ class PluginManager {
*/ */
public function loadPlugins() { public function loadPlugins() {
$pluginsDirectory = ManiaControlDir . '/plugins/'; $pluginsDirectory = ManiaControlDir . '/plugins/';
$pluginFiles = scandir($pluginsDirectory, 0); $pluginFiles = scandir($pluginsDirectory, 0);
foreach ($pluginFiles as $pluginFile) { foreach($pluginFiles as $pluginFile) {
if (stripos($pluginFile, '.') === 0) { if(stripos($pluginFile, '.') === 0) {
continue; continue;
} }
$classesBefore = get_declared_classes(); $classesBefore = get_declared_classes();
$success = include_once $pluginsDirectory . $pluginFile; $success = include_once $pluginsDirectory . $pluginFile;
if (!$success) { if(!$success) {
continue; continue;
} }
$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);
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);
@ -210,7 +218,7 @@ class PluginManager {
* @return Plugin * @return Plugin
*/ */
public function getPlugin($pluginClass) { public function getPlugin($pluginClass) {
if ($this->isPluginActive($pluginClass)) { if($this->isPluginActive($pluginClass)) {
return $this->activePlugins[$pluginClass]; return $this->activePlugins[$pluginClass];
} }
return null; return null;
@ -238,11 +246,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`
@ -250,15 +258,15 @@ 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;
} }
$activeInt = ($active ? 1 : 0); $activeInt = ($active ? 1 : 0);
$pluginStatement->bind_param('si', $className, $activeInt); $pluginStatement->bind_param('si', $className, $activeInt);
$pluginStatement->execute(); $pluginStatement->execute();
if ($pluginStatement->error) { if($pluginStatement->error) {
trigger_error($pluginStatement->error); trigger_error($pluginStatement->error);
$pluginStatement->close(); $pluginStatement->close();
return false; return false;
@ -274,23 +282,23 @@ class PluginManager {
* @return bool * @return bool
*/ */
private function getSavedPluginStatus($className) { private 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;
} }
$pluginStatement->bind_param('s', $className); $pluginStatement->bind_param('s', $className);
$pluginStatement->execute(); $pluginStatement->execute();
if ($pluginStatement->error) { if($pluginStatement->error) {
trigger_error($pluginStatement->error); trigger_error($pluginStatement->error);
$pluginStatement->close(); $pluginStatement->close();
return false; return false;
} }
$pluginStatement->store_result(); $pluginStatement->store_result();
if ($pluginStatement->num_rows <= 0) { if($pluginStatement->num_rows <= 0) {
$pluginStatement->free_result(); $pluginStatement->free_result();
$pluginStatement->close(); $pluginStatement->close();
$this->savePluginStatus($className, false); $this->savePluginStatus($className, false);

View File

@ -199,7 +199,7 @@ class PluginMenu implements CallbackListener, ConfiguratorMenu {
} }
if($enable) { if($enable) {
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_ENABLEPLUGIN)); $pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_ENABLEPLUGIN));
$activated = $this->maniaControl->pluginManager->activatePlugin($pluginClass); $activated = $this->maniaControl->pluginManager->activatePlugin($pluginClass, $player->login);
if($activated) { if($activated) {
$this->maniaControl->chat->sendSuccess($pluginClass::getName() . ' activated!', $player->login); $this->maniaControl->chat->sendSuccess($pluginClass::getName() . ' activated!', $player->login);
$this->maniaControl->configurator->showMenu($player); $this->maniaControl->configurator->showMenu($player);