Files
kiri-core/System/Event.php
T

231 lines
5.1 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake;
2020-10-29 18:17:25 +08:00
use Exception;
2020-08-31 01:27:08 +08:00
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\ArrayAccess;
/**
* Class Event
* @package Snowflake
*/
class Event extends BaseObject
{
2020-10-28 16:38:05 +08:00
public bool $isVide = true;
2020-08-31 01:27:08 +08:00
2020-10-28 16:38:05 +08:00
private array $_events = [];
2020-08-31 01:27:08 +08:00
const EVENT_ERROR = 'WORKER:ERROR';
const EVENT_STOP = 'WORKER:STOP';
const EVENT_EXIT = 'WORKER:EXIT';
2020-09-02 17:02:13 +08:00
const PIPE_MESSAGE = 'SERVER:PIPE:MESSAGE';
2020-08-31 01:27:08 +08:00
const EVENT_AFTER_REQUEST = 'SERVER:REQUEST:AFTER:START';
const EVENT_BEFORE_REQUEST = 'SERVER:REQUEST:BEFORE:START';
const RECEIVE_CONNECTION = 'SERVER:RECEIVE:CONNECTION';
2021-02-20 15:21:42 +08:00
const SYSTEM_RESOURCE_RELEASES = 'SYSTEM::RESOURCE::RELEASES';
const SYSTEM_RESOURCE_CLEAN = 'SYSTEM::RESOURCE::CLEAN';
2020-08-31 01:27:08 +08:00
2021-02-20 15:21:42 +08:00
const PROCESS_WORKER_STOP = 'SERVER:PROCESS:WORKER:STOP';
2020-08-31 01:27:08 +08:00
2020-09-02 17:33:48 +08:00
const SERVER_AFTER_RELOAD = 'SERVER:AFTER:RELOAD';
const SERVER_BEFORE_RELOAD = 'SERVER:BEFORE:RELOAD';
2020-08-31 01:27:08 +08:00
const SERVER_EVENT_START = 'SERVER:EVENT:START';
const SERVER_MANAGER_START = 'SERVER:EVENT:MANAGER:START';
const SERVER_MANAGER_STOP = 'SERVER:EVENT:MANAGER:START';
const SERVER_WORKER_STOP = 'SERVER:EVENT:WORKER:STOP';
const SERVER_WORKER_START = 'SERVER:EVENT:WORKER:START';
2021-01-19 19:33:48 +08:00
const SERVER_BEFORE_START = 'SERVER:EVENT:BEFORE:START';
2021-01-04 17:16:54 +08:00
const SERVER_TASK_START = 'SERVER:EVENT:TASK:START';
2020-08-31 01:27:08 +08:00
const SERVER_WORKER_EXIT = 'SERVER:EVENT:WORKER:EXIT';
const SERVER_WORKER_ERROR = 'SERVER:EVENT:WORKER:ERROR';
2020-09-02 17:33:48 +08:00
const SERVER_SHUTDOWN = 'SERVER:EVENT:SHUTDOWN';
2020-08-31 01:27:08 +08:00
const SERVER_HANDSHAKE = 'on handshake';
const SERVER_MESSAGE = 'on message';
const SERVER_CLOSE = 'on close';
/**
* @param $name
* @param $callback
* @param array $parameter
* @param bool $isAppend
2020-10-29 18:17:25 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
public function on($name, $callback, $parameter = [], $isAppend = true)
{
if (!isset($this->_events[$name])) {
$this->_events[$name] = [];
}
if ($callback instanceof \Closure) {
2020-09-03 11:39:20 +08:00
$callback = \Closure::bind($callback, Snowflake::app());
2020-08-31 01:27:08 +08:00
} else if (is_array($callback) && is_string($callback[0])) {
if (!class_exists($callback[0])) {
2020-10-29 18:17:25 +08:00
throw new Exception('Undefined callback class.');
2020-08-31 01:27:08 +08:00
}
$callback[0] = Snowflake::createObject($callback[0]);
}
2020-09-18 21:09:36 +08:00
if ($this->exists($name, $callback)) {
return;
}
2020-08-31 01:27:08 +08:00
if (!empty($this->_events[$name]) && $isAppend === true) {
array_unshift($this->_events[$name], [$callback, $parameter]);
} else {
$this->_events[$name][] = [$callback, $parameter];
}
}
/**
* @param $name
* @param $callback
*/
2020-12-17 14:09:14 +08:00
public function of($name, $callback): void
2020-08-31 01:27:08 +08:00
{
if (!isset($this->_events[$name])) {
return;
}
foreach ($this->_events[$name] as $index => $event) {
[$handler, $parameter] = $event;
if ($handler !== $callback) {
continue;
}
unset($this->_events[$name][$index]);
}
}
/**
* @param $name
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function offName($name): bool
2020-08-31 01:27:08 +08:00
{
if (!$this->exists($name)) {
return true;
}
unset($this->_events[$name]);
return $this->exists($name);
}
/**
* @param $name
* @param null $callback
* @return bool
*/
2020-12-17 14:09:14 +08:00
public function exists($name, $callback = null): bool
2020-08-31 01:27:08 +08:00
{
if (!isset($this->_events[$name])) {
return false;
}
if ($callback === null) {
return true;
}
foreach ($this->_events[$name] as $event) {
[$handler, $parameter] = $event;
if ($handler === $callback) {
return true;
}
}
return false;
}
/**
* @param $name
* @param $handler
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function get($name, $handler): mixed
2020-08-31 01:27:08 +08:00
{
if (!$this->exists($name)) {
return null;
}
foreach ($this->_events[$name] as $event) {
[$callback, $parameter] = $event;
if ($callback === $handler) {
return $event;
}
}
return null;
}
2020-08-31 12:38:32 +08:00
public function clean()
{
$this->_events = [];
}
2020-08-31 01:27:08 +08:00
/**
* @param $name
2021-02-08 15:29:49 +08:00
* @param array $params
* @return mixed
*/
public function dispatch($name, $params = []): mixed
{
return $this->trigger($name, $params);
}
/**
* @param $name
2020-08-31 01:27:08 +08:00
* @param null $parameter
2021-02-08 15:29:49 +08:00
* @param null $handler
2020-08-31 01:27:08 +08:00
* @param false $is_remove
2021-02-08 15:29:49 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function trigger($name, $parameter = null, $handler = null, $is_remove = false): mixed
2020-08-31 01:27:08 +08:00
{
if (!$this->exists($name)) {
return false;
}
if (!empty($handler) && $this->exists($name, $handler)) {
[$handler, $defaultParameter] = $this->get($name, $handler);
if (!empty($parameter)) {
$defaultParameter = ArrayAccess::merge($defaultParameter, $parameter);
}
if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter];
}
$result = call_user_func($handler, ...$defaultParameter);
2020-10-30 02:12:02 +08:00
if ($is_remove) {
2020-08-31 01:27:08 +08:00
$this->of($name, $handler);
}
return $result;
}
2020-10-30 01:46:19 +08:00
foreach ($this->_events[$name] as $index => $event) {
2020-08-31 01:27:08 +08:00
try {
2020-10-30 01:46:19 +08:00
[$handler, $defaultParameter] = $event;
2020-10-28 16:42:40 +08:00
if (!empty($parameter)) {
$defaultParameter = ArrayAccess::merge($defaultParameter, $parameter);
}
2020-08-31 01:27:08 +08:00
if (!is_array($defaultParameter)) {
$defaultParameter = [$defaultParameter];
}
call_user_func($handler, ...$defaultParameter);
} catch (\Throwable $exception) {
2021-02-20 15:45:48 +08:00
$this->error($exception);
2020-08-31 01:27:08 +08:00
}
}
if ($is_remove) {
$this->offName($name);
}
return true;
}
}