Skip to content

Commit

Permalink
模型事件方法支持依赖注入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 5, 2024
1 parent 21339ab commit 843f985
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,17 @@ public static function setInvoker(callable $callable): void
*/
public function invoke($method, array $vars = [])
{
if (is_string($method)) {
$method = [$this, $method];
}

if (self::$invoker) {
$call = self::$invoker;

return $call($method instanceof Closure ? $method : Closure::fromCallable([$this, $method]), $vars);
return $call($method instanceof Closure ? $method : Closure::fromCallable($method), $vars);
}

return call_user_func_array($method instanceof Closure ? $method : [$this, $method], $vars);
return call_user_func_array($method, $vars);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/model/concern/ModelEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function trigger(string $event): bool
}

if (method_exists($observer, $call)) {
$result = call_user_func([$observer, $call], $this);
$result = $this->invoke([$observer, $call], [$this]);
} elseif (is_object(self::$event) && method_exists(self::$event, 'trigger')) {
$result = self::$event->trigger(static::class . '.' . $event, $this);
$result = empty($result) ? true : end($result);
Expand Down

0 comments on commit 843f985

Please sign in to comment.