Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbo] Fixing support for not using old ClassUtils #1471

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
14 changes: 2 additions & 12 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,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;
}
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return get_parent_class($entity) ?: \get_class($entity);

if (false === $entityClass) {
throw new \LogicException('Parent class missing');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this exception IMHO, using get_class should be the way if there's no parent

}

return $entityClass;
}

// @legacy for old versions of Doctrine
if (class_exists(ClassUtils::class)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is not imported so it will always return false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

return ClassUtils::getClass($entity);
}

return $entity::class;
}
}