diff --git a/src/Turbo/src/Bridge/Mercure/Broadcaster.php b/src/Turbo/src/Bridge/Mercure/Broadcaster.php index 9fa18117fc1..e727a1276db 100644 --- a/src/Turbo/src/Bridge/Mercure/Broadcaster.php +++ b/src/Turbo/src/Bridge/Mercure/Broadcaster.php @@ -11,12 +11,11 @@ namespace Symfony\UX\Turbo\Bridge\Mercure; -use Doctrine\Common\Util\ClassUtils; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; use Symfony\Component\Mercure\HubInterface; use Symfony\Component\Mercure\Update; -use Symfony\Component\VarExporter\LazyObjectInterface; use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface; +use Symfony\UX\Turbo\Doctrine\ClassUtil; /** * Broadcasts updates rendered using Twig with Mercure. @@ -63,14 +62,7 @@ public function broadcast(object $entity, string $action, array $options): void return; } - if ($entity instanceof LazyObjectInterface) { - $entityClass = get_parent_class($entity); - if (false === $entityClass) { - throw new \LogicException('Parent class missing'); - } - } else { - $entityClass = ClassUtils::getClass($entity); - } + $entityClass = ClassUtil::getEntityClass($entity); if (!isset($options['rendered_action'])) { throw new \InvalidArgumentException(sprintf('Cannot broadcast entity of class "%s" as option "rendered_action" is missing.', $entityClass)); diff --git a/src/Turbo/src/Broadcaster/TwigBroadcaster.php b/src/Turbo/src/Broadcaster/TwigBroadcaster.php index 5fe2b3d05ec..05a428cd53f 100644 --- a/src/Turbo/src/Broadcaster/TwigBroadcaster.php +++ b/src/Turbo/src/Broadcaster/TwigBroadcaster.php @@ -11,8 +11,7 @@ namespace Symfony\UX\Turbo\Broadcaster; -use Doctrine\Common\Util\ClassUtils; -use Symfony\Component\VarExporter\LazyObjectInterface; +use Symfony\UX\Turbo\Doctrine\ClassUtil; use Twig\Environment; /** @@ -44,15 +43,7 @@ public function broadcast(object $entity, string $action, array $options): void $options['id'] = $id; } - // handle proxies (both styles) - if ($entity instanceof LazyObjectInterface) { - $class = get_parent_class($entity); - if (false === $class) { - throw new \LogicException('Parent class missing'); - } - } else { - $class = ClassUtils::getClass($entity); - } + $class = ClassUtil::getEntityClass($entity); if (null === $template = $options['template'] ?? null) { $template = $class; diff --git a/src/Turbo/src/Doctrine/BroadcastListener.php b/src/Turbo/src/Doctrine/BroadcastListener.php index 1a900f05106..9298b117724 100644 --- a/src/Turbo/src/Doctrine/BroadcastListener.php +++ b/src/Turbo/src/Doctrine/BroadcastListener.php @@ -13,11 +13,9 @@ use Doctrine\Common\Annotations\Reader; use Doctrine\Common\EventArgs; -use Doctrine\Common\Util\ClassUtils; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Event\PostFlushEventArgs; -use Symfony\Component\VarExporter\LazyObjectInterface; use Symfony\Contracts\Service\ResetInterface; use Symfony\UX\Turbo\Attribute\Broadcast; use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface; @@ -128,21 +126,13 @@ public function reset(): void private function storeEntitiesToPublish(EntityManagerInterface $em, object $entity, string $property): void { - // handle proxies (both styles) - if ($entity instanceof LazyObjectInterface) { - $class = get_parent_class($entity); - if (false === $class) { - throw new \LogicException('Parent class missing'); - } - } else { - $class = ClassUtils::getClass($entity); - } + $class = ClassUtil::getEntityClass($entity); if (!isset($this->broadcastedClasses[$class])) { $this->broadcastedClasses[$class] = []; $r = null; - if (\PHP_VERSION_ID >= 80000 && $options = ($r = new \ReflectionClass($class))->getAttributes(Broadcast::class)) { + if ($options = ($r = new \ReflectionClass($class))->getAttributes(Broadcast::class)) { foreach ($options as $option) { $this->broadcastedClasses[$class][] = $option->newInstance()->options; } diff --git a/src/Turbo/src/Doctrine/ClassUtil.php b/src/Turbo/src/Doctrine/ClassUtil.php new file mode 100644 index 00000000000..c2c016d04b8 --- /dev/null +++ b/src/Turbo/src/Doctrine/ClassUtil.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\Turbo\Doctrine; + +use Symfony\Component\VarExporter\LazyObjectInterface; + +/** + * @internal + */ +final class ClassUtil +{ + public static function getEntityClass(object $entity): string + { + if ($entity instanceof LazyObjectInterface) { + $entityClass = get_parent_class($entity); + if (false === $entityClass) { + throw new \LogicException('Parent class missing'); + } + + return $entityClass; + } + + // @legacy for old versions of Doctrine + if (class_exists(ClassUtils::class)) { + return ClassUtils::getClass($entity); + } + + return $entity::class; + } +}