Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions src/Concerns/Embeddable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
trait Embeddable
{
/**
* Per-class guard so the #[Embeds] scan only runs once per model class.
*
* @var array<string, bool>
*/
private static array $embedsBooted = [];

/**
* Per-class cache of the property names carrying #[Embeds], so a fresh
* INSERT (which #[Watches] never fires for) can still be embedded once,
Expand Down Expand Up @@ -100,35 +93,38 @@ protected function registerEmbedsAttributes(): void
{
$class = static::class;

if (isset(self::$embedsBooted[$class])) {
return;
}

self::$embedsBooted[$class] = true;
self::$embedsProperties[$class] = [];
if (!array_key_exists($class, self::$embedsProperties)) {
self::$embedsProperties[$class] = [];
$reflection = new \ReflectionClass($class);

$reflection = new \ReflectionClass($class);
foreach ($reflection->getProperties() as $property) {
$attributes = $property->getAttributes(Embeds::class);

foreach ($reflection->getProperties() as $property) {
$attributes = $property->getAttributes(Embeds::class);
if (empty($attributes)) {
continue;
}

if (empty($attributes)) {
continue;
$name = $property->getName();
self::$embedsProperties[$class][] = $name;
}
}

/** @var Embeds $embeds */
$embeds = $attributes[0]->newInstance();
$name = $property->getName();
$reflection = new \ReflectionClass($class);

foreach (self::$embedsProperties[$class] as $name) {
$property = $reflection->getProperty($name);
$embeds = $property->getAttributes(Embeds::class)[0]->newInstance();
$watcherKey = "embeds::{$class}::{$name}";

app()->singleton($watcherKey, fn() => new EmbeddingWatcher(
EmbedsManager::driver(),
$name,
$embeds->model,
));
if (!app()->has($watcherKey)) {
app()->singleton($watcherKey, fn() => new EmbeddingWatcher(
EmbedsManager::driver(),
$name,
$embeds->model,
));
}

WatchesHandler::register($class, $name, $watcherKey);
self::$embedsProperties[$class][] = $name;
}
}

Expand Down
Loading