From a1b801591099b37aad6e9d84eccbf5d3329afb88 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 7 Feb 2024 15:11:57 -0500 Subject: [PATCH] [Turbo] Fixing support for not using old ClassUtils --- src/Turbo/src/Bridge/Mercure/Broadcaster.php | 12 +----- src/Turbo/src/Broadcaster/TwigBroadcaster.php | 13 +------ src/Turbo/src/Doctrine/BroadcastListener.php | 12 +----- src/Turbo/src/Doctrine/ClassUtil.php | 39 +++++++++++++++++++ 4 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 src/Turbo/src/Doctrine/ClassUtil.php 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 1c3b85c2c59..3f659d05fe3 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 af99a144e4f..8579c3f5081 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,15 +126,7 @@ 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] = []; diff --git a/src/Turbo/src/Doctrine/ClassUtil.php b/src/Turbo/src/Doctrine/ClassUtil.php new file mode 100644 index 00000000000..a2f417d779f --- /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 \get_class($entity); + } +}