Skip to content

Commit 843f985

Browse files
committed
模型事件方法支持依赖注入
1 parent 21339ab commit 843f985

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Model.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,17 @@ public static function setInvoker(callable $callable): void
235235
*/
236236
public function invoke($method, array $vars = [])
237237
{
238+
if (is_string($method)) {
239+
$method = [$this, $method];
240+
}
241+
238242
if (self::$invoker) {
239243
$call = self::$invoker;
240244

241-
return $call($method instanceof Closure ? $method : Closure::fromCallable([$this, $method]), $vars);
245+
return $call($method instanceof Closure ? $method : Closure::fromCallable($method), $vars);
242246
}
243247

244-
return call_user_func_array($method instanceof Closure ? $method : [$this, $method], $vars);
248+
return call_user_func_array($method, $vars);
245249
}
246250

247251
/**

src/model/concern/ModelEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function trigger(string $event): bool
9393
}
9494

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

0 commit comments

Comments
 (0)