改名
This commit is contained in:
@@ -180,43 +180,42 @@ abstract class BaseApplication extends Service
|
||||
if (!isset($config['events']) || !is_array($config['events'])) {
|
||||
return;
|
||||
}
|
||||
$event = Snowflake::app()->getEvent();
|
||||
foreach ($config['events'] as $key => $value) {
|
||||
if (is_string($value)) {
|
||||
$value = Snowflake::createObject($value);
|
||||
}
|
||||
$this->addEvent($event, $key, $value);
|
||||
$this->addEvent($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @throws InitException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function addEvent($event, $key, $value): void
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @throws InitException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function addEvent($key, $value): void
|
||||
{
|
||||
if ($value instanceof \Closure) {
|
||||
$event->on($key, $value, [], true);
|
||||
Event::on($key, $value, [], true);
|
||||
return;
|
||||
}
|
||||
if (is_object($value)) {
|
||||
$event->on($key, $value, [], true);
|
||||
Event::on($key, $value, [], true);
|
||||
return;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
|
||||
$event->on($key, $value, [], true);
|
||||
Event::on($key, $value, [], true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_string($value[0])) {
|
||||
$value[0] = Snowflake::createObject($value[0]);
|
||||
$event->on($key, $value, [], true);
|
||||
Event::on($key, $value, [], true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -224,7 +223,7 @@ abstract class BaseApplication extends Service
|
||||
if (!is_callable($item, true)) {
|
||||
throw new InitException("Class does not hav callback.");
|
||||
}
|
||||
$event->on($key, $item, [], true);
|
||||
Event::on($key, $item, [], true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,16 +366,6 @@ abstract class BaseApplication extends Service
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Event
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getEvent(): Event
|
||||
{
|
||||
return $this->get('event');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Jwt
|
||||
* @throws Exception
|
||||
@@ -454,7 +443,6 @@ abstract class BaseApplication extends Service
|
||||
{
|
||||
$this->setComponents([
|
||||
'error' => ['class' => ErrorHandler::class],
|
||||
'event' => ['class' => Event::class],
|
||||
'connections' => ['class' => Connection::class],
|
||||
'redis_connections' => ['class' => SRedis::class],
|
||||
'pool' => ['class' => SPool::class],
|
||||
|
||||
@@ -35,10 +35,9 @@ class Redis extends Component
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
|
||||
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
|
||||
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
|
||||
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
|
||||
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
|
||||
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -74,8 +74,7 @@ class ErrorHandler extends Component implements ErrorInterface
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->trigger(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
@@ -103,8 +102,7 @@ class ErrorHandler extends Component implements ErrorInterface
|
||||
|
||||
Snowflake::app()->error($data, 'error');
|
||||
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->trigger(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
|
||||
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
||||
}
|
||||
|
||||
@@ -33,9 +33,8 @@ class Logger extends Component
|
||||
|
||||
public function init()
|
||||
{
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
|
||||
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
|
||||
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
|
||||
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-20
@@ -71,7 +71,7 @@ class Event extends BaseObject
|
||||
* @param bool $isAppend
|
||||
* @throws Exception
|
||||
*/
|
||||
public function on($name, $callback, $parameter = [], $isAppend = false)
|
||||
public static function on($name, $callback, $parameter = [], $isAppend = false)
|
||||
{
|
||||
if (!isset(static::$_events[$name])) {
|
||||
static::$_events[$name] = [];
|
||||
@@ -84,7 +84,7 @@ class Event extends BaseObject
|
||||
}
|
||||
$callback[0] = Snowflake::createObject($callback[0]);
|
||||
}
|
||||
if ($this->exists($name, $callback)) {
|
||||
if (static::exists($name, $callback)) {
|
||||
return;
|
||||
}
|
||||
if (!empty(static::$_events[$name]) && $isAppend === true) {
|
||||
@@ -99,7 +99,7 @@ class Event extends BaseObject
|
||||
* @param $name
|
||||
* @param $callback
|
||||
*/
|
||||
public function of($name, $callback): void
|
||||
public static function of($name, $callback): void
|
||||
{
|
||||
if (!isset(static::$_events[$name])) {
|
||||
return;
|
||||
@@ -118,13 +118,13 @@ class Event extends BaseObject
|
||||
* @param $name
|
||||
* @return bool
|
||||
*/
|
||||
public function offName($name): bool
|
||||
public static function offName($name): bool
|
||||
{
|
||||
if (!$this->exists($name)) {
|
||||
if (!static::exists($name)) {
|
||||
return true;
|
||||
}
|
||||
unset(static::$_events[$name]);
|
||||
return $this->exists($name);
|
||||
return static::exists($name);
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ class Event extends BaseObject
|
||||
* @param null $callback
|
||||
* @return bool
|
||||
*/
|
||||
public function exists($name, $callback = null): bool
|
||||
public static function exists($name, $callback = null): bool
|
||||
{
|
||||
if (!isset(static::$_events[$name])) {
|
||||
return false;
|
||||
@@ -156,9 +156,9 @@ class Event extends BaseObject
|
||||
* @param $handler
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($name, $handler): mixed
|
||||
public static function get($name, $handler): mixed
|
||||
{
|
||||
if (!$this->exists($name, $handler)) {
|
||||
if (!static::exists($name, $handler)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ class Event extends BaseObject
|
||||
}
|
||||
|
||||
|
||||
public function clean()
|
||||
public static function clean()
|
||||
{
|
||||
static::$_events = [];
|
||||
}
|
||||
@@ -188,9 +188,9 @@ class Event extends BaseObject
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch($name, $params = [], $scope = null): bool
|
||||
public static function dispatch($name, $params = [], $scope = null): bool
|
||||
{
|
||||
return $this->trigger($name, $params, $scope);
|
||||
return static::trigger($name, $params, $scope);
|
||||
}
|
||||
|
||||
|
||||
@@ -202,17 +202,17 @@ class Event extends BaseObject
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool
|
||||
public static function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool
|
||||
{
|
||||
$events = $this->get($name, $handler);
|
||||
$events = static::get($name, $handler);
|
||||
if (empty($events)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($events as $event) {
|
||||
$this->execute($event, $parameter);
|
||||
static::execute($event, $parameter);
|
||||
}
|
||||
if ($is_remove) {
|
||||
$this->offName($name);
|
||||
static::offName($name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -224,16 +224,16 @@ class Event extends BaseObject
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function execute($event, $parameter): bool
|
||||
private static function execute($event, $parameter): bool
|
||||
{
|
||||
try {
|
||||
$meta = $this->mergeParams($event[1], $parameter);
|
||||
$meta = static::mergeParams($event[1], $parameter);
|
||||
if (call_user_func($event[0], ...$meta) === false) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (\Throwable $throwable) {
|
||||
return $this->addError($throwable,'throwable');
|
||||
return static::addError($throwable,'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ class Event extends BaseObject
|
||||
* @param $parameter
|
||||
* @return array
|
||||
*/
|
||||
private function mergeParams($defaultParameter, $parameter = []): array
|
||||
private static function mergeParams($defaultParameter, $parameter = []): array
|
||||
{
|
||||
if (empty($defaultParameter)) {
|
||||
$defaultParameter = $parameter;
|
||||
|
||||
Reference in New Issue
Block a user