improved check for valid callback method
decreased amount of duplicated code
This commit is contained in:
parent
02ad48951b
commit
09f61f2ad4
@ -57,25 +57,20 @@ class CommandManager implements CallbackListener {
|
|||||||
}
|
}
|
||||||
return $success;
|
return $success;
|
||||||
}
|
}
|
||||||
|
|
||||||
$command = strtolower($commandName);
|
$command = strtolower($commandName);
|
||||||
if (!method_exists($listener, $method)) {
|
$listenerCallback = array($listener, $method);
|
||||||
trigger_error("Given listener can't handle command '{$command}' (no method '{$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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($adminCommand) {
|
if ($adminCommand) {
|
||||||
if (!array_key_exists($command, $this->adminCommandListeners) || !is_array($this->adminCommandListeners[$command])) {
|
$this->addListenerCallback($this->adminCommandListeners, $listenerCallback, $command);
|
||||||
// Init admin listeners array
|
|
||||||
$this->adminCommandListeners[$command] = array();
|
|
||||||
}
|
|
||||||
// Register admin command listener
|
|
||||||
array_push($this->adminCommandListeners[$command], array($listener, $method));
|
|
||||||
} else {
|
} else {
|
||||||
if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
|
$this->addListenerCallback($this->commandListeners, $listenerCallback, $command);
|
||||||
// Init listeners array
|
|
||||||
$this->commandListeners[$command] = array();
|
|
||||||
}
|
|
||||||
// Register command listener
|
|
||||||
array_push($this->commandListeners[$command], array($listener, $method));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO description
|
//TODO description
|
||||||
@ -84,6 +79,23 @@ class CommandManager implements CallbackListener {
|
|||||||
return true;
|
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
|
* Remove a Command Listener
|
||||||
*
|
*
|
||||||
@ -92,15 +104,25 @@ class CommandManager implements CallbackListener {
|
|||||||
*/
|
*/
|
||||||
public function unregisterCommandListener(CommandListener $listener) {
|
public function unregisterCommandListener(CommandListener $listener) {
|
||||||
$removed = false;
|
$removed = false;
|
||||||
foreach ($this->commandListeners as &$listeners) {
|
if ($this->removeCommandListener($this->commandListeners, $listener)) {
|
||||||
foreach ($listeners as $key => &$listenerCallback) {
|
|
||||||
if ($listenerCallback[0] == $listener) {
|
|
||||||
unset($listeners[$key]);
|
|
||||||
$removed = true;
|
$removed = true;
|
||||||
}
|
}
|
||||||
|
if ($this->removeCommandListener($this->adminCommandListeners, $listener)) {
|
||||||
|
$removed = true;
|
||||||
}
|
}
|
||||||
|
return $removed;
|
||||||
}
|
}
|
||||||
foreach ($this->adminCommandListeners as &$listeners) {
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
foreach ($listeners as $key => &$listenerCallback) {
|
||||||
if ($listenerCallback[0] == $listener) {
|
if ($listenerCallback[0] == $listener) {
|
||||||
unset($listeners[$key]);
|
unset($listeners[$key]);
|
||||||
@ -167,8 +189,8 @@ class CommandManager implements CallbackListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Inform command listeners
|
// Inform command listeners
|
||||||
foreach ($commandListeners[$command] as $listener) {
|
foreach ($commandListeners[$command] as $listenerCallback) {
|
||||||
call_user_func(array($listener[0], $listener[1]), $callback, $player);
|
call_user_func($listenerCallback, $callback, $player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user