server cache & logs folder cleaning on shutdown

This commit is contained in:
Steffen Schröder 2014-06-20 18:51:40 +02:00
parent 88a8a83fae
commit 14e92a10b8

View File

@ -2,6 +2,9 @@
namespace ManiaControl\Server; namespace ManiaControl\Server;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Files\FileUtil;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
@ -11,7 +14,7 @@ use ManiaControl\ManiaControl;
* @copyright 2014 ManiaControl Team * @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
*/ */
class Directory { class Directory implements CallbackListener {
/** /**
* Private Properties * Private Properties
*/ */
@ -24,6 +27,8 @@ class Directory {
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_SERVERSTOP, $this, 'handleServerStopCallback');
} }
/** /**
@ -44,6 +49,23 @@ class Directory {
return $this->maniaControl->client->getSkinsDirectory(); return $this->maniaControl->client->getSkinsDirectory();
} }
/**
* Handle Server Stop Callback
*/
public function handleServerStopCallback() {
$this->cleanLogsFolder();
$this->cleanCacheFolder();
}
/**
* Clean the server logs folder
*
* @return bool
*/
private function cleanLogsFolder() {
return FileUtil::cleanDirectory($this->getLogsFolder());
}
/** /**
* Retrieve the Logs Folder Path * Retrieve the Logs Folder Path
* *
@ -54,12 +76,10 @@ class Directory {
} }
/** /**
* Retrieve the Game Data Folder Path * @return bool
*
* @return string
*/ */
public function getGameDataFolder() { private function cleanCacheFolder() {
return $this->maniaControl->client->gameDataDirectory(); return FileUtil::cleanDirectory($this->getCacheFolder(), 50);
} }
/** /**
@ -70,4 +90,13 @@ class Directory {
public function getCacheFolder() { public function getCacheFolder() {
return $this->getGameDataFolder() . '..' . DIRECTORY_SEPARATOR . 'CommonData' . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR; return $this->getGameDataFolder() . '..' . DIRECTORY_SEPARATOR . 'CommonData' . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
} }
/**
* Retrieve the Game Data Folder Path
*
* @return string
*/
public function getGameDataFolder() {
return $this->maniaControl->client->gameDataDirectory();
}
} }