From 09f61f2ad449c90415abe938eb186173f9788526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Tue, 13 May 2014 00:22:47 +0200 Subject: [PATCH] improved check for valid callback method decreased amount of duplicated code --- application/core/Commands/CommandManager.php | 72 +++++++++++++------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/application/core/Commands/CommandManager.php b/application/core/Commands/CommandManager.php index 7525ce5c..95929abf 100644 --- a/application/core/Commands/CommandManager.php +++ b/application/core/Commands/CommandManager.php @@ -57,25 +57,20 @@ class CommandManager implements CallbackListener { } return $success; } - $command = strtolower($commandName); - if (!method_exists($listener, $method)) { - trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!"); + + $command = strtolower($commandName); + $listenerCallback = array($listener, $method); + $listenerClass = get_class($listener); + + if (!is_callable($listenerCallback)) { + trigger_error("Given Listener '{$listenerClass}' can't handle Command '{$command}'! No callable Method '{$method}'!"); return false; } + if ($adminCommand) { - if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) { - // Init admin listeners array - $this->adminCommandListeners[$command] = array(); - } - // Register admin command listener - array_push($this->adminCommandListeners[$command], array($listener, $method)); + $this->addListenerCallback($this->adminCommandListeners, $listenerCallback, $command); } else { - if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) { - // Init listeners array - $this->commandListeners[$command] = array(); - } - // Register command listener - array_push($this->commandListeners[$command], array($listener, $method)); + $this->addListenerCallback($this->commandListeners, $listenerCallback, $command); } //TODO description @@ -84,6 +79,23 @@ class CommandManager implements CallbackListener { return true; } + /** + * Add a Listener Callback to the given Listener Array + * + * @param array $listenerArray + * @param array $listenerCallback + * @param string $command + */ + private function addListenerCallback(array &$listenerArray, array $listenerCallback, $command) { + if (!array_key_exists($command, $listenerArray) || !is_array($listenerArray[$command])) { + // Init listeners array + $listenerArray[$command] = array(); + } + + // Register command listener + array_push($listenerArray[$command], $listenerCallback); + } + /** * Remove a Command Listener * @@ -92,15 +104,25 @@ class CommandManager implements CallbackListener { */ public function unregisterCommandListener(CommandListener $listener) { $removed = false; - foreach ($this->commandListeners as &$listeners) { - foreach ($listeners as $key => &$listenerCallback) { - if ($listenerCallback[0] == $listener) { - unset($listeners[$key]); - $removed = true; - } - } + if ($this->removeCommandListener($this->commandListeners, $listener)) { + $removed = true; } - foreach ($this->adminCommandListeners as &$listeners) { + if ($this->removeCommandListener($this->adminCommandListeners, $listener)) { + $removed = true; + } + return $removed; + } + + /** + * Remove the Command Listener from the given Listeners Array + * + * @param array $listenerArray + * @param CommandListener $listener + * @return bool + */ + private function removeCommandListener(array &$listenerArray, CommandListener $listener) { + $removed = false; + foreach ($listenerArray as &$listeners) { foreach ($listeners as $key => &$listenerCallback) { if ($listenerCallback[0] == $listener) { unset($listeners[$key]); @@ -167,8 +189,8 @@ class CommandManager implements CallbackListener { } // Inform command listeners - foreach ($commandListeners[$command] as $listener) { - call_user_func(array($listener[0], $listener[1]), $callback, $player); + foreach ($commandListeners[$command] as $listenerCallback) { + call_user_func($listenerCallback, $callback, $player); } } }