diff --git a/src/Concerns/Embeddable.php b/src/Concerns/Embeddable.php index 9c820c6..3124fe9 100644 --- a/src/Concerns/Embeddable.php +++ b/src/Concerns/Embeddable.php @@ -20,13 +20,6 @@ */ trait Embeddable { - /** - * Per-class guard so the #[Embeds] scan only runs once per model class. - * - * @var array - */ - 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, @@ -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; } }