refactor codestyle (chaining)

This commit is contained in:
kremsy
2014-08-13 11:05:52 +02:00
parent 699c5951d9
commit 22915bb934
56 changed files with 1572 additions and 3132 deletions

View File

@ -40,14 +40,11 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$this->maniaControl = $maniaControl;
// Callbacks
$this->maniaControl->getCallbackManager()
->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
// Chat commands
$this->maniaControl->getCommandManager()
->registerCommandListener('checkpluginsupdate', $this, 'handle_CheckPluginsUpdate', true, 'Check for Plugin Updates.');
$this->maniaControl->getCommandManager()
->registerCommandListener('pluginsupdate', $this, 'handle_PluginsUpdate', true, 'Perform the Plugin Updates.');
$this->maniaControl->getCommandManager()->registerCommandListener('checkpluginsupdate', $this, 'handle_CheckPluginsUpdate', true, 'Check for Plugin Updates.');
$this->maniaControl->getCommandManager()->registerCommandListener('pluginsupdate', $this, 'handle_PluginsUpdate', true, 'Perform the Plugin Updates.');
}
/**
@ -57,11 +54,9 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
* @param Player $player
*/
public function handle_CheckPluginsUpdate(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()
->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATECHECK)
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATECHECK)
) {
$this->maniaControl->getAuthenticationManager()
->sendNotAllowed($player);
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
@ -76,65 +71,58 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
public function checkPluginsUpdate(Player $player = null) {
$message = 'Checking Plugins for newer Versions...';
if ($player) {
$this->maniaControl->getChat()
->sendInformation($message, $player);
$this->maniaControl->getChat()->sendInformation($message, $player);
}
Logger::log($message);
$this->maniaControl->getPluginManager()
->fetchPluginList(function ($data, $error) use (&$player) {
if (!$data || $error) {
$message = 'Error while checking Plugins for newer Versions!';
if ($player) {
$this->maniaControl->getChat()
->sendError($message, $player);
}
Logger::logError($message);
return;
}
$this->maniaControl->getPluginManager()->fetchPluginList(function ($data, $error) use (&$player) {
if (!$data || $error) {
$message = 'Error while checking Plugins for newer Versions!';
if ($player) {
$this->maniaControl->getChat()->sendError($message, $player);
}
Logger::logError($message);
return;
}
$pluginsData = $this->parsePluginsData($data);
$pluginClasses = $this->maniaControl->getPluginManager()
->getPluginClasses();
$pluginUpdates = array();
$pluginsData = $this->parsePluginsData($data);
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
$pluginUpdates = array();
foreach ($pluginClasses as $pluginClass) {
/** @var Plugin $pluginClass */
$pluginId = $pluginClass::getId();
if (!isset($pluginsData[$pluginId])) {
continue;
}
/** @var PluginUpdateData $pluginData */
$pluginData = $pluginsData[$pluginId];
$pluginVersion = $pluginClass::getVersion();
if ($pluginData->isNewerThan($pluginVersion)) {
$pluginUpdates[$pluginId] = $pluginData;
$message = "There is an Update of '{$pluginData->pluginName}' available! ('{$pluginClass}' - Version {$pluginData->version})";
if ($player) {
$this->maniaControl->getChat()
->sendSuccess($message, $player);
}
Logger::log($message);
}
}
foreach ($pluginClasses as $pluginClass) {
/** @var Plugin $pluginClass */
$pluginId = $pluginClass::getId();
if (!isset($pluginsData[$pluginId])) {
continue;
}
/** @var PluginUpdateData $pluginData */
$pluginData = $pluginsData[$pluginId];
$pluginVersion = $pluginClass::getVersion();
if ($pluginData->isNewerThan($pluginVersion)) {
$pluginUpdates[$pluginId] = $pluginData;
$message = "There is an Update of '{$pluginData->pluginName}' available! ('{$pluginClass}' - Version {$pluginData->version})";
if ($player) {
$this->maniaControl->getChat()->sendSuccess($message, $player);
}
Logger::log($message);
}
}
if (empty($pluginUpdates)) {
$message = 'Plugins Update Check completed: All Plugins are up-to-date!';
if ($player) {
$this->maniaControl->getChat()
->sendSuccess($message, $player);
}
Logger::log($message);
} else {
$updatesCount = count($pluginUpdates);
$message = "Plugins Update Check completed: There are {$updatesCount} Updates available!";
if ($player) {
$this->maniaControl->getChat()
->sendSuccess($message, $player);
}
Logger::log($message);
}
});
if (empty($pluginUpdates)) {
$message = 'Plugins Update Check completed: All Plugins are up-to-date!';
if ($player) {
$this->maniaControl->getChat()->sendSuccess($message, $player);
}
Logger::log($message);
} else {
$updatesCount = count($pluginUpdates);
$message = "Plugins Update Check completed: There are {$updatesCount} Updates available!";
if ($player) {
$this->maniaControl->getChat()->sendSuccess($message, $player);
}
Logger::log($message);
}
});
}
/**
@ -162,11 +150,9 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
* @param Player $player
*/
public function handle_PluginsUpdate(array $chatCallback, Player $player) {
if (!$this->maniaControl->getAuthenticationManager()
->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATE)
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, UpdateManager::SETTING_PERMISSION_UPDATE)
) {
$this->maniaControl->getAuthenticationManager()
->sendNotAllowed($player);
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
return;
}
@ -183,8 +169,7 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
if (empty($pluginsUpdates)) {
$message = 'There are no Plugin Updates available!';
if ($player) {
$this->maniaControl->getChat()
->sendInformation($message, $player);
$this->maniaControl->getChat()->sendInformation($message, $player);
}
Logger::log($message);
return;
@ -192,18 +177,15 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$message = "Starting Plugins Updating...";
if ($player) {
$this->maniaControl->getChat()
->sendInformation($message, $player);
$this->maniaControl->getChat()->sendInformation($message, $player);
}
Logger::log($message);
$performBackup = $this->maniaControl->getSettingManager()
->getSettingValue($this->maniaControl->getUpdateManager(), UpdateManager::SETTING_PERFORM_BACKUPS);
$performBackup = $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getUpdateManager(), UpdateManager::SETTING_PERFORM_BACKUPS);
if ($performBackup && !BackupUtil::performPluginsBackup()) {
$message = 'Creating Backup before Plugins Update failed!';
if ($player) {
$this->maniaControl->getChat()
->sendError($message, $player);
$this->maniaControl->getChat()->sendError($message, $player);
}
Logger::logError($message);
}
@ -230,8 +212,7 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$pluginsUpdates = $this->parsePluginsData($pluginData);
$updates = array();
$pluginClasses = $this->maniaControl->getPluginManager()
->getPluginClasses();
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
foreach ($pluginClasses as $pluginClass) {
/** @var Plugin $pluginClass */
$pluginId = $pluginClass::getId();
@ -259,96 +240,86 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
* @param bool $update
*/
private function installPlugin(PluginUpdateData $pluginUpdateData, Player $player = null, $update = false) {
$this->maniaControl->getFileReader()
->loadFile($pluginUpdateData->url, function ($updateFileContent, $error) use (
&$pluginUpdateData, &$player, &$update
) {
if (!$updateFileContent || $error) {
$message = "Error loading Update Data for '{$pluginUpdateData->pluginName}': {$error}!";
if ($player) {
$this->maniaControl->getChat()
->sendInformation($message, $player);
}
Logger::logError($message);
return;
}
$this->maniaControl->getFileReader()->loadFile($pluginUpdateData->url, function ($updateFileContent, $error) use (
&$pluginUpdateData, &$player, &$update
) {
if (!$updateFileContent || $error) {
$message = "Error loading Update Data for '{$pluginUpdateData->pluginName}': {$error}!";
if ($player) {
$this->maniaControl->getChat()->sendInformation($message, $player);
}
Logger::logError($message);
return;
}
$actionNoun = ($update ? 'Update' : 'Install');
$actionVerb = ($update ? 'Updating' : 'Installing');
$actionVerbDone = ($update ? 'updated' : 'installed');
$actionNoun = ($update ? 'Update' : 'Install');
$actionVerb = ($update ? 'Updating' : 'Installing');
$actionVerbDone = ($update ? 'updated' : 'installed');
$message = "Now {$actionVerb} '{$pluginUpdateData->pluginName}'...";
if ($player) {
$this->maniaControl->getChat()
->sendInformation($message, $player);
}
Logger::log($message);
$message = "Now {$actionVerb} '{$pluginUpdateData->pluginName}'...";
if ($player) {
$this->maniaControl->getChat()->sendInformation($message, $player);
}
Logger::log($message);
$tempDir = FileUtil::getTempFolder();
$updateFileName = $tempDir . $pluginUpdateData->zipfile;
$tempDir = FileUtil::getTempFolder();
$updateFileName = $tempDir . $pluginUpdateData->zipfile;
$bytes = file_put_contents($updateFileName, $updateFileContent);
if (!$bytes || $bytes <= 0) {
$message = "Plugin {$actionNoun} failed: Couldn't save {$actionNoun} Zip!";
if ($player) {
$this->maniaControl->getChat()
->sendError($message, $player);
}
Logger::logError($message);
return;
}
$bytes = file_put_contents($updateFileName, $updateFileContent);
if (!$bytes || $bytes <= 0) {
$message = "Plugin {$actionNoun} failed: Couldn't save {$actionNoun} Zip!";
if ($player) {
$this->maniaControl->getChat()->sendError($message, $player);
}
Logger::logError($message);
return;
}
$zip = new \ZipArchive();
$result = $zip->open($updateFileName);
if ($result !== true) {
$message = "Plugin {$actionNoun} failed: Couldn't open {$actionNoun} Zip! ({$result})";
if ($player) {
$this->maniaControl->getChat()
->sendError($message, $player);
}
Logger::logError($message);
return;
}
$zip = new \ZipArchive();
$result = $zip->open($updateFileName);
if ($result !== true) {
$message = "Plugin {$actionNoun} failed: Couldn't open {$actionNoun} Zip! ({$result})";
if ($player) {
$this->maniaControl->getChat()->sendError($message, $player);
}
Logger::logError($message);
return;
}
$zip->extractTo(MANIACONTROL_PATH . 'plugins' . DIRECTORY_SEPARATOR);
$zip->close();
unlink($updateFileName);
FileUtil::deleteTempFolder();
$zip->extractTo(MANIACONTROL_PATH . 'plugins' . DIRECTORY_SEPARATOR);
$zip->close();
unlink($updateFileName);
FileUtil::deleteTempFolder();
$messageExtra = '';
if ($update) {
$messageExtra = ' (Restart ManiaControl to load the new Version!)';
}
$message = "Successfully {$actionVerbDone} '{$pluginUpdateData->pluginName}'!{$messageExtra}";
if ($player) {
$this->maniaControl->getChat()
->sendSuccess($message, $player);
}
Logger::log($message);
$messageExtra = '';
if ($update) {
$messageExtra = ' (Restart ManiaControl to load the new Version!)';
}
$message = "Successfully {$actionVerbDone} '{$pluginUpdateData->pluginName}'!{$messageExtra}";
if ($player) {
$this->maniaControl->getChat()->sendSuccess($message, $player);
}
Logger::log($message);
if (!$update) {
$newPluginClasses = $this->maniaControl->getPluginManager()
->loadPlugins();
if (empty($newPluginClasses)) {
$message = "Loading fresh installed Plugin '{$pluginUpdateData->pluginName}' failed!";
if ($player) {
$this->maniaControl->getChat()
->sendError($message, $player);
}
Logger::log($message);
} else {
$message = "Successfully loaded fresh installed Plugin '{$pluginUpdateData->pluginName}'!";
if ($player) {
$this->maniaControl->getChat()
->sendSuccess($message, $player);
}
Logger::log($message);
if (!$update) {
$newPluginClasses = $this->maniaControl->getPluginManager()->loadPlugins();
if (empty($newPluginClasses)) {
$message = "Loading fresh installed Plugin '{$pluginUpdateData->pluginName}' failed!";
if ($player) {
$this->maniaControl->getChat()->sendError($message, $player);
}
Logger::log($message);
} else {
$message = "Successfully loaded fresh installed Plugin '{$pluginUpdateData->pluginName}'!";
if ($player) {
$this->maniaControl->getChat()->sendSuccess($message, $player);
}
Logger::log($message);
$this->maniaControl->getConfigurator()
->showMenu($player, InstallMenu::getTitle());
}
}
});
$this->maniaControl->getConfigurator()->showMenu($player, InstallMenu::getTitle());
}
}
});
}
/**
@ -365,8 +336,7 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
}
$login = $callback[1][1];
$player = $this->maniaControl->getPlayerManager()
->getPlayer($login);
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
if ($update) {
$pluginClass = substr($actionId, strlen(PluginMenu::ACTION_PREFIX_UPDATEPLUGIN));
@ -378,34 +348,30 @@ class PluginUpdateManager implements CallbackListener, CommandListener, TimerLis
$this->installPlugin($pluginUpdateData, $player, true);
} else {
$message = 'Error loading Plugin Update Data!';
$this->maniaControl->getChat()
->sendError($message, $player);
$this->maniaControl->getChat()->sendError($message, $player);
}
}
} else {
$pluginId = substr($actionId, strlen(InstallMenu::ACTION_PREFIX_INSTALL_PLUGIN));
$url = ManiaControl::URL_WEBSERVICE . 'plugins/' . $pluginId;
$this->maniaControl->getFileReader()
->loadFile($url, function ($data, $error) use (&$player) {
if ($error || !$data) {
$message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()
->sendError($message, $player);
return;
}
$this->maniaControl->getFileReader()->loadFile($url, function ($data, $error) use (&$player) {
if ($error || !$data) {
$message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()->sendError($message, $player);
return;
}
$data = json_decode($data);
if (!$data) {
$message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()
->sendError($message, $player);
return;
}
$data = json_decode($data);
if (!$data) {
$message = "Error loading Plugin Install Data! {$error}";
$this->maniaControl->getChat()->sendError($message, $player);
return;
}
$pluginUpdateData = new PluginUpdateData($data);
$this->installPlugin($pluginUpdateData, $player);
});
$pluginUpdateData = new PluginUpdateData($data);
$this->installPlugin($pluginUpdateData, $player);
});
}
}