UEvent
Userland Events in PHP
Install / Use
/learn @DatingVIP/UEventREADME
UEvent
Userland events in PHP
This here is an extension to allow users to decare and Zend to fire events at runtime, because delicious ...
Example
How ...
The following code demonstrates how to attach events to the invokation of methods or functions:
<?php
class foo {
public static function bar() {}
/* ... */
}
UEvent::addEvent("foo.bar", ["Foo", "bar"]);
UEvent::addListener("foo.bar", function(array $array = []){
echo "hello foo::bar\n";
});
/* ... */
foo::bar();
?>
Will output:
hello foo::bar
Trigger events based on arguments
A bit more complicated ...
The following code demonstrates how to use UEventInput in combination with UEventArgs to capture
and pass the argument stack from call to listener
<?php
class foo {
public static function bar($foo) {}
/* ... */
}
/* Will capture arguments at calltime and trigger event based on arguments
also stores argument stack for passing to listener ... so ... voodoo ... */
class EventArgs implements UEventInput, UEventArgs {
public function accept() {
$this->args = func_get_args();
if (count($this->args)) {
return ($this->args[0] == "trigger");
}
}
public function get() { return $this->args; }
protected $args;
}
$arguments = new EventArgs();
UEvent::addEvent("foo.bar", ["Foo", "bar"], $arguments);
UEvent::addListener("foo.bar", function($argv){
echo "Foo::bar({$argv}) called\n";
}, $arguments);
foo::bar('trigger');
foo::bar('no-trigger');
?>
Will output
Foo::bar(trigger) called
API
Then names of things and what not ...
<?php
interface UEventInput {
/**
* Shall recieve the argument stack at call time
* @returns boolean
* Note: use func_get_args
*/
public function accept();
}
interface UEventArgs {
/**
* Shall return arguments for event listener invocation
* @returns array
*/
public function get();
}
class UEvent {
/**
* Shall call $handler($args->get()) when $name is fired by uevent
* @param string name
* @param Closure handler
* @returns boolean
* @throws \RuntimeException
*/
public static function addListener($name, Closure $handler, UEventArgs $args = null);
/**
* Shall create an event of the given $name:
* $name shall be fired when $input->accept() returns true
* @param string name
* @param callable call
* @param UEventInput input
* @returns boolean
* @throws \RuntimeException
*/
public static function addEvent($name, callable $call, UEventInput $input = null);
/**
* Shall return the names of all events
* @returns array
*/
public static function getEvents();
}
?>
Related Skills
node-connect
350.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
