2011-12-27 12:58:28 +00:00
|
|
|
<?php
|
2012-03-24 21:37:47 +00:00
|
|
|
namespace wcf\system\chat\command;
|
2011-12-27 12:58:28 +00:00
|
|
|
use \wcf\util\StringUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles commands
|
|
|
|
*
|
|
|
|
* @author Tim Düsterhus
|
2012-01-30 16:48:13 +00:00
|
|
|
* @copyright 2010-2012 Tim Düsterhus
|
2011-12-27 12:58:28 +00:00
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
2012-06-04 19:43:03 +00:00
|
|
|
* @package be.bastelstu.wcf.chat
|
2012-03-24 21:37:47 +00:00
|
|
|
* @subpackage system.chat.command
|
2011-12-27 12:58:28 +00:00
|
|
|
*/
|
2012-02-04 11:32:56 +00:00
|
|
|
final class CommandHandler {
|
2012-10-20 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* char that indicates a command
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-12-27 12:58:28 +00:00
|
|
|
const COMMAND_CHAR = '/';
|
2012-10-20 14:23:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* message text
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-02-04 11:32:56 +00:00
|
|
|
private $text = '';
|
2011-12-27 12:58:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialises the CommandHandler
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
*/
|
|
|
|
public function __construct($text) {
|
|
|
|
$this->text = $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2011-12-27 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 11:32:56 +00:00
|
|
|
/**
|
|
|
|
* Returns the whole message.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getText() {
|
|
|
|
return $this->text;
|
|
|
|
}
|
|
|
|
|
2011-12-27 13:32:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the parameter-string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getParameters() {
|
|
|
|
$parts = explode(' ', StringUtil::substring($this->text, StringUtil::length(static::COMMAND_CHAR)), 2);
|
|
|
|
|
2012-02-04 11:32:56 +00:00
|
|
|
if (!isset($parts[1])) return '';
|
2011-12-27 13:32:28 +00:00
|
|
|
return $parts[1];
|
|
|
|
}
|
|
|
|
|
2011-12-27 12:58:28 +00:00
|
|
|
/**
|
|
|
|
* Loads the command.
|
|
|
|
*/
|
|
|
|
public function loadCommand() {
|
|
|
|
$parts = explode(' ', StringUtil::substring($this->text, StringUtil::length(static::COMMAND_CHAR)), 2);
|
|
|
|
|
|
|
|
if ($this->isCommand($parts[0])) {
|
2012-02-04 11:32:56 +00:00
|
|
|
return new commands\Plain($this);
|
2011-12-27 12:58:28 +00:00
|
|
|
}
|
|
|
|
|
2012-09-07 20:23:27 +00:00
|
|
|
$class = '\wcf\system\chat\command\commands\\'.ucfirst(strtolower($parts[0])).'Command';
|
2011-12-27 12:58:28 +00:00
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $class($this);
|
|
|
|
}
|
|
|
|
}
|