1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-26 22:20:08 +00:00
Tims-Chat/file/lib/system/command/CommandHandler.class.php

127 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace chat\system\command;
use \wcf\util\StringUtil;
/**
* Handles commands
2014-02-28 16:06:50 +00:00
*
* @author Tim Düsterhus
2014-02-27 22:05:09 +00:00
* @copyright 2010-2014 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
2013-01-19 19:36:40 +00:00
* @package be.bastelstu.chat
* @subpackage system.chat.command
*/
2012-02-04 11:32:56 +00:00
final class CommandHandler {
2012-10-20 14:23:00 +00:00
/**
* char that indicates a command
2013-06-17 21:48:59 +00:00
* @var string
2012-10-20 14:23:00 +00:00
*/
const COMMAND_CHAR = '/';
2012-10-20 14:23:00 +00:00
/**
* message text
2013-06-17 21:48:59 +00:00
* @var string
2012-10-20 14:23:00 +00:00
*/
2012-02-04 11:32:56 +00:00
private $text = '';
/**
* current room
2013-06-17 21:48:59 +00:00
* @var \chat\data\room\Room
*/
private $room = null;
/**
* Initialises the CommandHandler
*
* @param string $text
* @param \chat\data\room\Room $room
*/
public function __construct($text, \chat\data\room\Room $room = null) {
$this->text = $text;
$this->room = $room;
$aliases = self::getAliasMap();
foreach ($aliases as $search => $replace) {
2013-06-17 22:13:24 +00:00
$this->text = \wcf\system\Regex::compile('^'.preg_quote(self::COMMAND_CHAR.$search).'( |$)')->replace($this->text, self::COMMAND_CHAR.$replace.' ');
}
2013-06-17 22:13:24 +00:00
$this->text = \wcf\system\Regex::compile('^//')->replace($this->text, '/plain ');
}
2013-06-17 21:48:59 +00:00
/**
* Returns the alias map. Key is the alias, value is the target.
*
* @return array<string>
*/
public static function getAliasMap() {
if (StringUtil::trim(CHAT_COMMAND_ALIASES) === '') return array();
try {
$result = array();
foreach (explode("\n", StringUtil::unifyNewlines(StringUtil::toLowerCase(CHAT_COMMAND_ALIASES))) as $line) {
list($key, $val) = explode(':', $line, 2);
$result[StringUtil::trim($key)] = StringUtil::trim($val);
}
2013-06-17 21:48:59 +00:00
return $result;
}
catch (\wcf\system\exception\SystemException $e) {
throw new \wcf\system\exception\SystemException("Invalid alias specified: '".$line."'");
2013-06-17 21:48:59 +00:00
}
}
/**
* Checks whether the given text is a command.
*/
public function isCommand($text = null) {
if ($text === null) $text = $this->text;
2012-10-20 14:23:00 +00:00
return StringUtil::startsWith($text, static::COMMAND_CHAR);
}
2012-02-04 11:32:56 +00:00
/**
* Returns the whole message.
2014-02-28 16:06:50 +00:00
*
2012-02-04 11:32:56 +00:00
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Returns the current room.
*
* @return \chat\data\room\Room
*/
public function getRoom() {
return $this->room;
}
2011-12-27 13:32:28 +00:00
/**
* Returns the parameter-string.
*
* @return string
*/
public function getParameters() {
$parts = explode(' ', mb_substr($this->text, mb_strlen(static::COMMAND_CHAR)), 2);
2011-12-27 13:32:28 +00:00
2012-02-04 11:32:56 +00:00
if (!isset($parts[1])) return '';
2011-12-27 13:32:28 +00:00
return $parts[1];
}
/**
* Loads the command.
*/
public function loadCommand() {
$parts = explode(' ', mb_substr($this->text, mb_strlen(static::COMMAND_CHAR)), 2);
$class = '\chat\system\command\commands\\'.ucfirst(strtolower($parts[0])).'Command';
if (!class_exists($class)) {
throw new NotFoundException($parts[0]);
}
return new $class($this);
}
}