SSE : Add params support for Handlers.

SSE : Split Handlers in Event and Dataonly


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6073 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2020-09-03 08:10:04 +00:00
parent c136fb4a40
commit f9214b0c0e
2 changed files with 98 additions and 10 deletions

60
sse.php
View File

@@ -35,6 +35,9 @@ class sse
/** The handlers storage
*/
private $handlers = array ();
/** The handlers parameters
*/
private $handlersParams = array ();
// }}}
//////////////////////////////
@@ -92,23 +95,49 @@ class sse
}
// }}}
/** The optional handler to use. Must be array of callable methods
* @param array|callable $handlers The handler method, array[event=>callable]
/** The optional handler to use in DataOnly. Must be callable method
* @param callable|null $handler
* If callable is null, remove the handler for this event
* @param mixed... $params The optional needed parameters
*/
final public function setHandlers ($handlers)
final public function setHandlerDataonly ($handler, $params = null)
// {{{
{
if (! is_callable ($handler) && $handler !== null)
throw new \Exception (dgettext ("domframework",
"SSE : can not use handler : not callable"), 500);
$this->handlers["data"] = $handler;
$args = func_get_args ();
array_shift ($args);
$this->handlersParams["data"] = $args;
return $this;
}
// }}}
/** The optional handler to use in Events. Must be array of callable methods
* @param array $handlers The handlers method, array[event=>callable]
* @param array|mixed $params The parameters of the handlers
* array[event=>callable]
* If callable is null, remove the handler for this event
* @param array|null $params The optional needed parameters
* array(event=>array(params))
* if event=>null is set, remove the parameters for the event
*/
final public function setHandlersEvent ($handlers, $params = null)
// {{{
{
if (is_callable ($handlers) || $handlers === null)
$handlers = array ("data" => $handlers);
if (! is_array ($handlers))
throw new \Exception (dgettext ("domframework",
"SSE : can not use handler : not array"), 500);
if (! is_array ($params) && $params !== null)
throw new \Exception (dgettext ("domframework",
"SSE : can not use handler params : not array"), 500);
foreach ($handlers as $event => $handler)
{
if ($handler === null && key_exists ($event, $this->handlers))
{
unset ($this->handlers[$event]);
unset ($this->handlersParams[$event]);
continue;
}
if (! is_callable ($handler))
@@ -116,6 +145,19 @@ class sse
"SSE : can not use handler : not callable"), 500);
$this->handlers[$event] = $handler;
}
if (is_array ($params))
{
foreach ($params as $event => $param)
{
if (! key_exists ($event, $this->handlers))
throw new \Exception (dgettext ("domframework",
"SSE : can not use parameter '$event' : no associated handler"), 500);
if ($param === null)
unset ($this->handlersParams[$event]);
else
$this->handlersParams[$event] = $param;
}
}
return $this;
}
// }}}
@@ -158,9 +200,15 @@ class sse
$data = $this->backendGet ($event);
if ($data === false)
continue;
$data = rtrim ($data);
if (key_exists ($event, $this->handlers) &&
is_callable ($this->handlers[$event]))
$data = call_user_func ($this->handlers[$event], $data);
{
$array = array ($data);
if (key_exists ($event, $this->handlersParams))
$array = array_merge ($array, $this->handlersParams[$event]);
$data = call_user_func_array ($this->handlers[$event], $array);
}
if ($data === false)
continue;
// If the event is data, do not display "event:data", but data: