improved callback registering

This commit is contained in:
Steffen Schröder 2014-07-11 16:54:43 +02:00
parent 8049935945
commit 46665aa86c
2 changed files with 85 additions and 73 deletions

View File

@ -62,7 +62,9 @@ class CallbackManager {
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
/** @var Listening[][] $callbackListenings */
private $callbackListenings = array(); private $callbackListenings = array();
/** @var Listening[][] $scriptCallbackListenings */
private $scriptCallbackListenings = array(); private $scriptCallbackListenings = array();
/** /**
@ -88,10 +90,10 @@ class CallbackManager {
*/ */
public function registerCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
if (is_array($callbackName)) { if (is_array($callbackName)) {
$success = true;
foreach ($callbackName as $callback) {
if (!$this->registerCallbackListener($callback, $listener, $method)) {
$success = false; $success = false;
foreach ($callbackName as $callback) {
if ($this->registerCallbackListener($callback, $listener, $method)) {
$success = true;
} }
} }
return $success; return $success;
@ -123,10 +125,10 @@ class CallbackManager {
*/ */
public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) { public function registerScriptCallbackListener($callbackName, CallbackListener $listener, $method) {
if (is_array($callbackName)) { if (is_array($callbackName)) {
$success = true;
foreach ($callbackName as $callback) {
if (!$this->registerScriptCallbackListener($callback, $listener, $method)) {
$success = false; $success = false;
foreach ($callbackName as $callback) {
if ($this->registerScriptCallbackListener($callback, $listener, $method)) {
$success = true;
} }
} }
return $success; return $success;
@ -159,29 +161,11 @@ class CallbackManager {
} }
//TODO better name (used only in customvotesPlugin) //TODO better name (used only in customvotesPlugin)
/**
* Unregister a single Callback Listening from an Callback Listener
*
* @param String $callbackName
* @param CallbackListener $listener
* @return bool
*/
public function unregisterCallbackListening($callbackName, CallbackListener $listener){
foreach($this->callbackListenings as &$listenings){
foreach ($listenings as $key => &$listening) {
if($key == $callbackName && $listening->listener === $listener){
unset($listenings[$key]);
return true;
}
}
}
return false;
}
/** /**
* Remove the Callback Listener from the given Listeners Array * Remove the Callback Listener from the given Listeners Array
* *
* @param array $listeningsArray * @param Listening[] $listeningsArray
* @param CallbackListener $listener * @param CallbackListener $listener
* @return bool * @return bool
*/ */
@ -189,7 +173,6 @@ class CallbackManager {
$removed = false; $removed = false;
foreach ($listeningsArray as &$listenings) { foreach ($listeningsArray as &$listenings) {
foreach ($listenings as $key => &$listening) { foreach ($listenings as $key => &$listening) {
/** @var Listening $listening */
if ($listening->listener === $listener) { if ($listening->listener === $listener) {
unset($listenings[$key]); unset($listenings[$key]);
$removed = true; $removed = true;
@ -199,6 +182,26 @@ class CallbackManager {
return $removed; return $removed;
} }
/**
* Unregister a single Callback Listening from an Callback Listener
*
* @param String $callbackName
* @param CallbackListener $listener
* @return bool
*/
public function unregisterCallbackListening($callbackName, CallbackListener $listener) {
$removed = false;
foreach ($this->callbackListenings as &$listenings) {
foreach ($listenings as $key => &$listening) {
if ($key === $callbackName && $listening->listener === $listener) {
unset($listenings[$key]);
$removed = true;
}
}
}
return $removed;
}
/** /**
* Unregister a Script Callback Listener * Unregister a Script Callback Listener
* *
@ -285,7 +288,6 @@ class CallbackManager {
} }
foreach ($this->callbackListenings[$callbackName] as $listening) { foreach ($this->callbackListenings[$callbackName] as $listening) {
/** @var Listening $listening */
$listening->triggerCallbackWithParams($params); $listening->triggerCallbackWithParams($params);
} }
} }
@ -316,7 +318,6 @@ class CallbackManager {
$params = array_slice($params, 1, null, true); $params = array_slice($params, 1, null, true);
foreach ($this->scriptCallbackListenings[$callbackName] as $listening) { foreach ($this->scriptCallbackListenings[$callbackName] as $listening) {
/** @var Listening $listening */
$listening->triggerCallbackWithParams($params); $listening->triggerCallbackWithParams($params);
} }
} }

View File

@ -4,6 +4,7 @@ namespace ManiaControl\Commands;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Listening;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
@ -19,19 +20,20 @@ class CommandManager implements CallbackListener {
*/ */
private $maniaControl = null; private $maniaControl = null;
private $helpManager = array(); private $helpManager = array();
// TODO: use listening class /** @var Listening[][] $commandListenings */
private $adminCommandListeners = array(); private $commandListenings = array();
private $commandListeners = array(); /** @var Listening[][] $adminCommandListenings */
private $adminCommandListenings = array();
/** /**
* Construct a new Commands Manager * Construct a new Commands Manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
//Create help manager instance // Create help manager instance
$this->helpManager = new HelpManager($this->maniaControl); $this->helpManager = new HelpManager($this->maniaControl);
// Register for callback // Register for callback
@ -48,30 +50,31 @@ class CommandManager implements CallbackListener {
* @param string $description * @param string $description
* @return bool * @return bool
*/ */
public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false, $description = null) { public function registerCommandListener($commandName, CommandListener $listener, $method, $adminCommand = false,
$description = null) {
if (is_array($commandName)) { if (is_array($commandName)) {
$success = true;
foreach ($commandName as $command) {
if (!$this->registerCommandListener($command, $listener, $method, $adminCommand, $description)) {
$success = false; $success = false;
foreach ($commandName as $command) {
if ($this->registerCommandListener($command, $listener, $method, $adminCommand, $description)) {
$success = true;
} }
} }
return $success; return $success;
} }
$command = strtolower($commandName); if (!Listening::checkValidCallback($listener, $method)) {
$listenerCallback = array($listener, $method);
$listenerClass = get_class($listener); $listenerClass = get_class($listener);
trigger_error("Given Listener '{$listenerClass}' can't handle Command '{$commandName}': No callable Method '{$method}'!");
if (!is_callable($listenerCallback)) {
trigger_error("Given Listener '{$listenerClass}' can't handle Command '{$command}'! No callable Method '{$method}'!");
return false; return false;
} }
$command = strtolower($commandName);
$listening = new Listening($listener, $method);
if ($adminCommand) { if ($adminCommand) {
$this->addListenerCallback($this->adminCommandListeners, $listenerCallback, $command); $this->addListening($this->adminCommandListenings, $listening, $command);
} else { } else {
$this->addListenerCallback($this->commandListeners, $listenerCallback, $command); $this->addListening($this->commandListenings, $listening, $command);
} }
// TODO: description(?) // TODO: description(?)
@ -83,20 +86,20 @@ class CommandManager implements CallbackListener {
} }
/** /**
* Add a Listener Callback to the given Listener Array * Add a Listening to the given Listenings Array
* *
* @param array $listenerArray * @param array $listeningsArray
* @param callable $listenerCallback * @param Listening $listening
* @param string $command * @param string $command
*/ */
private function addListenerCallback(array &$listenerArray, callable $listenerCallback, $command) { private function addListening(array &$listeningsArray, Listening $listening, $command) {
if (!array_key_exists($command, $listenerArray) || !is_array($listenerArray[$command])) { if (!array_key_exists($command, $listeningsArray) || !is_array($listeningsArray[$command])) {
// Init listeners array // Init listenings array
$listenerArray[$command] = array(); $listeningsArray[$command] = array();
} }
// Register command listener // Register command listening
array_push($listenerArray[$command], $listenerCallback); array_push($listeningsArray[$command], $listening);
} }
/** /**
@ -107,28 +110,28 @@ class CommandManager implements CallbackListener {
*/ */
public function unregisterCommandListener(CommandListener $listener) { public function unregisterCommandListener(CommandListener $listener) {
$removed = false; $removed = false;
if ($this->removeCommandListener($this->commandListeners, $listener)) { if ($this->removeCommandListener($this->commandListenings, $listener)) {
$removed = true; $removed = true;
} }
if ($this->removeCommandListener($this->adminCommandListeners, $listener)) { if ($this->removeCommandListener($this->adminCommandListenings, $listener)) {
$removed = true; $removed = true;
} }
return $removed; return $removed;
} }
/** /**
* Remove the Command Listener from the given Listeners Array * Remove the Command Listener from the given Listenings Array
* *
* @param array $listenerArray * @param array $listeningsArray
* @param CommandListener $listener * @param CommandListener $listener
* @return bool * @return bool
*/ */
private function removeCommandListener(array &$listenerArray, CommandListener $listener) { private function removeCommandListener(array &$listeningsArray, CommandListener $listener) {
$removed = false; $removed = false;
foreach ($listenerArray as &$listeners) { foreach ($listeningsArray as &$listenings) {
foreach ($listeners as $key => &$listenerCallback) { foreach ($listenings as $key => &$listening) {
if ($listenerCallback[0] === $listener) { if ($listening->listener === $listener) {
unset($listeners[$key]); unset($listenings[$key]);
$removed = true; $removed = true;
} }
} }
@ -164,7 +167,7 @@ class CommandManager implements CallbackListener {
if (substr($message, 0, 2) === '//' || $command === 'admin') { if (substr($message, 0, 2) === '//' || $command === 'admin') {
// Admin command // Admin command
$commandListeners = $this->adminCommandListeners; $commandListenings = $this->adminCommandListenings;
if ($command === 'admin') { if ($command === 'admin') {
// Strip 'admin' keyword // Strip 'admin' keyword
@ -176,24 +179,32 @@ class CommandManager implements CallbackListener {
unset($commandArray[0]); unset($commandArray[0]);
// Compose uniformed message // Compose uniformed message
$message = '//' . $command; $message = '//' . $command . ' ' . implode(' ', $commandArray);
foreach ($commandArray as $commandPart) {
$message .= ' ' . $commandPart;
}
$callback[1][2] = $message; $callback[1][2] = $message;
} else { } else {
// User command // User command
$commandListeners = $this->commandListeners; $commandListenings = $this->commandListenings;
} }
if (!array_key_exists($command, $commandListeners) || !is_array($commandListeners[$command])) { if (!array_key_exists($command, $commandListenings) || !is_array($commandListenings[$command])) {
// No command listener registered // No command listener registered
return; return;
} }
// Inform command listeners // Inform command listeners
foreach ($commandListeners[$command] as $listenerCallback) { foreach ($commandListenings[$command] as $listening) {
call_user_func($listenerCallback, $callback, $player); /** @var Listening $listening */
$listening->triggerCallback($callback, $player);
} }
} }
/**
* Check if the given Chat Callback is a Command Message
*
* @param array $chatCallback
* @return bool
*/
private function isCommandMessage(array $chatCallback) {
return (bool)$chatCallback[1][3];
}
} }