From 098f048e2963924864aa2c475bddaf1cbef9bb1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 17 Jun 2013 23:48:59 +0200 Subject: [PATCH] Implement command aliases Closes #35 --- file/lib/page/ChatPage.class.php | 3 +++ .../system/command/CommandHandler.class.php | 25 ++++++++++++++++--- option.xml | 8 +++--- package.xml | 2 +- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/file/lib/page/ChatPage.class.php b/file/lib/page/ChatPage.class.php index e55376d..e5ed79e 100644 --- a/file/lib/page/ChatPage.class.php +++ b/file/lib/page/ChatPage.class.php @@ -146,6 +146,9 @@ public function readCommands() { if ($command == 'Plain') continue; $this->commands[] = \wcf\util\StringUtil::toLowerCase($command); } + + $this->commands = array_merge($this->commands, array_keys(\chat\system\command\CommandHandler::getAliasMap())); + sort($this->commands); } /** diff --git a/file/lib/system/command/CommandHandler.class.php b/file/lib/system/command/CommandHandler.class.php index d8a60c6..bf3607c 100644 --- a/file/lib/system/command/CommandHandler.class.php +++ b/file/lib/system/command/CommandHandler.class.php @@ -14,19 +14,19 @@ final class CommandHandler { /** * char that indicates a command - * @var string + * @var string */ const COMMAND_CHAR = '/'; /** * message text - * @var string + * @var string */ private $text = ''; /** * current room - * @var \chat\data\room\Room + * @var \chat\data\room\Room */ private $room = null; @@ -41,6 +41,22 @@ public function __construct($text, \chat\data\room\Room $room = null) { $this->room = $room; } + /** + * Returns the alias map. Key is the alias, value is the target. + * + * @return array + */ + public static function getAliasMap() { + $result = array(); + foreach (explode("\n", StringUtil::unifyNewlines(StringUtil::toLowerCase(CHAT_COMMAND_ALIASES))) as $line) { + list($key, $val) = explode(':', $line, 2); + + $result[$key] = $val; + } + + return $result; + } + /** * Checks whether the given text is a command. */ @@ -90,6 +106,9 @@ public function loadCommand() { return new commands\PlainCommand($this); } + $aliases = self::getAliasMap(); + if (isset($aliases[$parts[0]])) $parts[0] = $aliases[$parts[0]]; + $class = '\chat\system\command\commands\\'.ucfirst(strtolower($parts[0])).'Command'; if (!class_exists($class)) { throw new NotFoundException(); diff --git a/option.xml b/option.xml index 29a6eff..bef3647 100644 --- a/option.xml +++ b/option.xml @@ -44,10 +44,12 @@ 1 5000 - diff --git a/package.xml b/package.xml index d6c16fa..d6f9f46 100644 --- a/package.xml +++ b/package.xml @@ -5,7 +5,7 @@ 1 - 3.0.0 Alpha 56 + 3.0.0 Alpha 58 2011-11-26 ]]>