2014-05-13 01:18:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Callbacks;
|
|
|
|
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
|
|
|
|
2014-05-13 01:18:47 +02:00
|
|
|
/**
|
|
|
|
* Model Class for a Basic Listening
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2020-01-22 10:39:35 +01:00
|
|
|
* @copyright 2014-2020 ManiaControl Team
|
2014-05-13 01:18:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class Listening implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-05-13 01:18:47 +02:00
|
|
|
/*
|
|
|
|
* Public Properties
|
|
|
|
*/
|
|
|
|
public $listener = null;
|
2017-03-26 19:44:55 +02:00
|
|
|
public $method = null;
|
2014-05-13 01:18:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new Timer Listening
|
|
|
|
*
|
|
|
|
* @param object $listener
|
|
|
|
* @param mixed $method
|
|
|
|
*/
|
|
|
|
public function __construct($listener, $method) {
|
|
|
|
$this->listener = $listener;
|
|
|
|
$this->method = $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given Listener and Method build a valid Callback
|
|
|
|
*
|
|
|
|
* @param object $listener
|
|
|
|
* @param mixed $method
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function checkValidCallback($listener, $method) {
|
|
|
|
if (is_callable($method)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$listenerCallback = array($listener, $method);
|
|
|
|
if (is_callable($listenerCallback)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the Listener's Method
|
|
|
|
*/
|
|
|
|
public function triggerCallback() {
|
|
|
|
$params = func_get_args();
|
|
|
|
$this->triggerCallbackWithParams($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the Listener's Method with the given Array of Params
|
|
|
|
*
|
|
|
|
* @param array $params
|
2015-06-21 20:45:48 +02:00
|
|
|
* @return mixed
|
2014-05-13 01:18:47 +02:00
|
|
|
*/
|
|
|
|
public function triggerCallbackWithParams(array $params) {
|
2015-06-21 20:45:48 +02:00
|
|
|
return call_user_func_array($this->getUserFunction(), $params);
|
2014-05-13 01:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Callable User Function
|
|
|
|
*
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getUserFunction() {
|
|
|
|
if (is_callable($this->method)) {
|
|
|
|
return $this->method;
|
|
|
|
}
|
|
|
|
return array($this->listener, $this->method);
|
|
|
|
}
|
|
|
|
}
|