Skip to content

Commit

Permalink
Refactor key generation into a single
Browse files Browse the repository at this point in the history
  • Loading branch information
takuyamashita committed Jan 3, 2024
1 parent 38fa79e commit 027364d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ public function removeObservableEvents($observables)
protected static function registerModelEvent($event, $callback)
{
if (isset(static::$dispatcher)) {
$name = static::class;

static::$dispatcher->listen("eloquent.{$event}: {$name}", $callback);
static::$dispatcher->listen(static::makeModelEventKey($event), $callback);
}
}

Expand Down Expand Up @@ -186,7 +185,7 @@ protected function fireModelEvent($event, $halt = true)
}

return ! empty($result) ? $result : static::$dispatcher->{$method}(
"eloquent.{$event}: ".static::class, $this
static::makeModelEventKey($event), $this
);
}

Expand Down Expand Up @@ -227,6 +226,17 @@ protected function filterModelEventResults($result)
return $result;
}

/**
* Make model event key for the given event.
*
* @param string $event
* @return string
*/
protected static function makeModelEventKey($event)
{
return "eloquent.{$event}: ".static::class;
}

/**
* Register a retrieved model event with the dispatcher.
*
Expand Down Expand Up @@ -351,7 +361,7 @@ public static function flushEventListeners()
$instance = new static;

foreach ($instance->getObservableEvents() as $event) {
static::$dispatcher->forget("eloquent.{$event}: ".static::class);
static::$dispatcher->forget(static::makeModelEventKey($event));
}

foreach (array_values($instance->dispatchesEvents) as $event) {
Expand Down

0 comments on commit 027364d

Please sign in to comment.