Skip to content

Commit

Permalink
[Turbo] Fixing support for not using old ClassUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Feb 7, 2024
1 parent 9c6e3f7 commit 79cb86c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
12 changes: 2 additions & 10 deletions src/Turbo/src/Bridge/Mercure/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand Down
13 changes: 2 additions & 11 deletions src/Turbo/src/Broadcaster/TwigBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 1 addition & 11 deletions src/Turbo/src/Doctrine/BroadcastListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] = [];
Expand Down
39 changes: 39 additions & 0 deletions src/Turbo/src/Doctrine/ClassUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}

0 comments on commit 79cb86c

Please sign in to comment.