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__ . '/PluginMenu.php';
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
/**
@ -57,12 +57,12 @@ class PluginManager {
UNIQUE KEY `className` (`className`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
$tableStatement = $mysqli->prepare($pluginsTableQuery);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
return false;
}
$tableStatement->execute();
if ($tableStatement->error) {
if($tableStatement->error) {
trigger_error($tableStatement->error, E_USER_ERROR);
return false;
}
@ -77,7 +77,7 @@ class PluginManager {
* @return bool
*/
public function isPluginActive($pluginClass) {
if (is_object($pluginClass)) {
if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass);
}
return isset($this->activePlugins[$pluginClass]);
@ -90,10 +90,10 @@ class PluginManager {
* @return bool
*/
public function isPluginClass($pluginClass) {
if (is_object($pluginClass)) {
if(is_object($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 true;
@ -106,13 +106,13 @@ class PluginManager {
* @return bool
*/
public function addPluginClass($pluginClass) {
if (is_object($pluginClass)) {
if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass);
}
if (in_array($pluginClass, $this->pluginClasses)) {
if(in_array($pluginClass, $this->pluginClasses)) {
return false;
}
if (!$this->isPluginClass($pluginClass)) {
if(!$this->isPluginClass($pluginClass)) {
return false;
}
array_push($this->pluginClasses, $pluginClass);
@ -123,20 +123,28 @@ class PluginManager {
* Activate and start the plugin with the given name
*
* @param string $pluginClass
* @param string $adminLogin
* @return bool
*/
public function activatePlugin($pluginClass) {
if (!is_string($pluginClass)) {
public function activatePlugin($pluginClass, $adminLogin = false) {
if(!is_string($pluginClass)) {
return false;
}
if (!$this->isPluginClass($pluginClass)) {
if(!$this->isPluginClass($pluginClass)) {
return false;
}
if ($this->isPluginActive($pluginClass)) {
if($this->isPluginActive($pluginClass)) {
return false;
}
$plugin = new $pluginClass();
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->savePluginStatus($pluginClass, true);
return true;
@ -149,21 +157,21 @@ class PluginManager {
* @return bool
*/
public function deactivatePlugin($pluginClass) {
if (is_object($pluginClass)) {
if(is_object($pluginClass)) {
$pluginClass = get_class($pluginClass);
}
if (!$this->isPluginActive($pluginClass)) {
if(!$this->isPluginActive($pluginClass)) {
return false;
}
$plugin = $this->activePlugins[$pluginClass];
unset($this->activePlugins[$pluginClass]);
$plugin->unload();
$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->unregisterScriptCallbackListener($plugin);
}
if (in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) {
if(in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
}
$this->savePluginStatus($pluginClass, false);
@ -176,26 +184,26 @@ class PluginManager {
public function loadPlugins() {
$pluginsDirectory = ManiaControlDir . '/plugins/';
$pluginFiles = scandir($pluginsDirectory, 0);
foreach ($pluginFiles as $pluginFile) {
if (stripos($pluginFile, '.') === 0) {
foreach($pluginFiles as $pluginFile) {
if(stripos($pluginFile, '.') === 0) {
continue;
}
$classesBefore = get_declared_classes();
$success = include_once $pluginsDirectory . $pluginFile;
if (!$success) {
if(!$success) {
continue;
}
$classesAfter = get_declared_classes();
$newClasses = array_diff($classesAfter, $classesBefore);
foreach ($newClasses as $className) {
if (!$this->isPluginClass($className)) {
foreach($newClasses as $className) {
if(!$this->isPluginClass($className)) {
continue;
}
$this->addPluginClass($className);
if ($this->isPluginActive($className)) {
if($this->isPluginActive($className)) {
continue;
}
if (!$this->getSavedPluginStatus($className)) {
if(!$this->getSavedPluginStatus($className)) {
continue;
}
$this->activatePlugin($className);
@ -210,7 +218,7 @@ class PluginManager {
* @return Plugin
*/
public function getPlugin($pluginClass) {
if ($this->isPluginActive($pluginClass)) {
if($this->isPluginActive($pluginClass)) {
return $this->activePlugins[$pluginClass];
}
return null;
@ -251,14 +259,14 @@ class PluginManager {
) ON DUPLICATE KEY UPDATE
`active` = VALUES(`active`);";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$activeInt = ($active ? 1 : 0);
$pluginStatement->bind_param('si', $className, $activeInt);
$pluginStatement->execute();
if ($pluginStatement->error) {
if($pluginStatement->error) {
trigger_error($pluginStatement->error);
$pluginStatement->close();
return false;
@ -278,19 +286,19 @@ class PluginManager {
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
WHERE `className` = ?;";
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$pluginStatement->bind_param('s', $className);
$pluginStatement->execute();
if ($pluginStatement->error) {
if($pluginStatement->error) {
trigger_error($pluginStatement->error);
$pluginStatement->close();
return false;
}
$pluginStatement->store_result();
if ($pluginStatement->num_rows <= 0) {
if($pluginStatement->num_rows <= 0) {
$pluginStatement->free_result();
$pluginStatement->close();
$this->savePluginStatus($className, false);

View File

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