Skip to content

Commit

Permalink
优化事件
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 28, 2024
1 parent 1bcf4b5 commit 2d100f1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Event/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ public function trigger($event, ...$var): void

$listeners = $this->listener[$event] ?? [];

if (str_contains($event, '.')) {
if (str_contains($event, '.*')) {
$needSort = false;
[$prefix, $event] = explode('.', $event, 2);

foreach ($this->listener as $e => $listener) {
if ($event == '*' && str_starts_with($e, $prefix . '.')) {
$listeners = array_merge($listeners, $listener);
$needSort = true;
}
}

if ($needSort) {
$listeners = $this->arraySort($listeners, 'sort');
}
}

$listeners = $this->arraySort($listeners, 'sort');

foreach ($listeners as $key => $listener) {
$this->dispatch($listener['listener'], $var);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function listen(string $event, $listener, int $sort = 1)
'key' => $this->filterBuildUniqueId($listener),
];

$this->listener[$event] = $this->arraySort($this->listener[$event], 'sort');

return $this;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Event/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ public function trigger($event, $value = null, ...$var)
$event = $event::class;
}

$result = [];
$listeners = $this->listener[$event] ?? [];

if (str_contains($event, '.')) {
if (str_contains($event, '.*')) {
$needSort = false;
[$prefix, $event] = explode('.', $event, 2);

foreach ($this->listener as $e => $listener) {
if ($event == '*' && str_starts_with($e, $prefix . '.')) {
$listeners = array_merge($listeners, $listener);
$needSort = true;
}
}

if ($needSort) {
$listeners = $this->arraySort($listeners, 'sort');
}
}

$listeners = $this->arraySort($listeners, 'sort');

$tmp = $var;
$result = $value;
foreach ($listeners as $key => $listener) {
Expand Down
4 changes: 2 additions & 2 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.6");
define("LAKET_RELEASE", "1.5.6.20240723");
define("LAKET_VERSION", "1.5.7");
define("LAKET_RELEASE", "1.5.7.20240728");

if (! function_exists('make')) {
/**
Expand Down

0 comments on commit 2d100f1

Please sign in to comment.