Skip to content

Commit

Permalink
优化事件
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 23, 2024
1 parent 43dd470 commit a68d7ab
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 18 deletions.
16 changes: 8 additions & 8 deletions src/Event/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ class Events
/**
* 动作事件
*/
protected Action $action;
protected Action $actionHandle;

/**
* 过滤事件
*/
protected Filter $filter;
protected Filter $filterHandle;

public function __construct(App $app)
{
$this->action = new Action($app);
$this->filter = new Filter($app);
$this->actionHandle = new Action($app);
$this->filterHandle = new Filter($app);
}

/**
* 获取动作事件
*/
public function getAction(): Action
public function action(): Action
{
return $this->action;
return $this->actionHandle;
}

/**
* 获取过滤事件
*/
public function getFilter(): Filter
public function filter(): Filter
{
return $this->filter;
return $this->filterHandle;
}

}
115 changes: 115 additions & 0 deletions src/Flash/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare (strict_types = 1);

namespace Laket\Admin\Flash;

use Laket\Admin\Facade\Event as AdminEvent;

/**
* 事件
*
* @create 2024-7-23
* @author deatil
*/
class Event
{
/**
* 类型
*
* @var string
*/
private string $type;

/**
* 唯一句柄
*
* @var string
*/
private string $handle;

/**
* 初始化
*
* @param string $type
* @param string $handle
*/
public function __construct(string $type, string $handle)
{
$this->type = $type;
$this->handle = $this->nativeClassName($handle);
}

/**
* 操作
*
* @param string $handle 标识
* @return Plugin
*/
public static function action(string $handle): Event
{
return new self('action', $handle);
}

/**
* 过滤器
*
* @param string $handle 标识
* @return Plugin
*/
public static function filter(string $handle): Event
{
return new self('filter', $handle);
}

/**
* 设置回调函数
*
* @param string $component 当前组件
* @param callable $value 回调函数
*/
public function __set(string $component, callable $value)
{
$weight = 1;

if (strpos($component, '_') > 0) {
$parts = explode('_', $component, 2);
[$component, $weight] = $parts;
$weight = intval($weight);
}

$component = $this->handle . ':' . $component;

if ($this->type == 'action') {
AdminEvent::action()->listen($component, $value, $weight);
} else {
AdminEvent::filter()->listen($component, $value, $weight);
}
}

/**
* 回调函数
*
* @param string $component 当前组件
* @param array $args 参数
*/
public function __call(string $component, array $args)
{
$component = $this->handle . ':' . $component;

if ($this->type == 'action') {
AdminEvent::action()->trigger($component, ...$args);
} else {
return AdminEvent::filter()->trigger($component, ...$args);
}
}

/**
* @param string $className
* @return string
*/
public static function nativeClassName(string $className): string
{
return trim(str_replace('\\', '_', $className), '_');
}
}
20 changes: 10 additions & 10 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Laket\Admin\Model\Attachment as AttachmentModel;

// 版本号
define("LAKET_VERSION", "1.5.2");
define("LAKET_RELEASE", "1.5.2.20240723");
define("LAKET_VERSION", "1.5.3");
define("LAKET_RELEASE", "1.5.3.20240723");

if (! function_exists('make')) {
/**
Expand Down Expand Up @@ -63,7 +63,7 @@ function route(
*/
function add_action(string $event, $listener, int $sort = 1): void
{
Event::getAction()->listen($event, $listener, $sort);
Event::action()->listen($event, $listener, $sort);
}
}

Expand All @@ -77,7 +77,7 @@ function add_action(string $event, $listener, int $sort = 1): void
*/
function do_action($event, ...$var): void
{
Event::getAction()->trigger($event, ...$var);
Event::action()->trigger($event, ...$var);
}
}

Expand All @@ -92,7 +92,7 @@ function do_action($event, ...$var): void
*/
function remove_action(string $event, $listener, int $sort = 1): bool
{
return Event::getAction()->removeListener($event, $listener, $sort);
return Event::action()->removeListener($event, $listener, $sort);
}
}

Expand All @@ -106,7 +106,7 @@ function remove_action(string $event, $listener, int $sort = 1): bool
*/
function has_action(string $event, $listener): bool
{
return Event::getAction()->hasListener($event, $listener);
return Event::action()->hasListener($event, $listener);
}
}

Expand All @@ -121,7 +121,7 @@ function has_action(string $event, $listener): bool
*/
function add_filter(string $event, $listener, int $sort = 1): void
{
Event::getFilter()->listen($event, $listener, $sort);
Event::filter()->listen($event, $listener, $sort);
}
}

Expand All @@ -136,7 +136,7 @@ function add_filter(string $event, $listener, int $sort = 1): void
*/
function apply_filters($event, $value = null, ...$var)
{
return Event::getFilter()->trigger($event, $value, ...$var);
return Event::filter()->trigger($event, $value, ...$var);
}
}

Expand All @@ -151,7 +151,7 @@ function apply_filters($event, $value = null, ...$var)
*/
function remove_filter(string $event, $listener, int $sort = 1): bool
{
return Event::getFilter()->removeListener($event, $listener, $sort);
return Event::filter()->removeListener($event, $listener, $sort);
}
}

Expand All @@ -165,7 +165,7 @@ function remove_filter(string $event, $listener, int $sort = 1): bool
*/
function has_filter(string $event, $listener): bool
{
return Event::getFilter()->hasListener($event, $listener);
return Event::filter()->hasListener($event, $listener);
}
}

Expand Down

0 comments on commit a68d7ab

Please sign in to comment.