Skip to content

Commit adb2cbb

Browse files
committed
bug #1471 [Turbo] Fixing support for not using old ClassUtils (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Turbo] Fixing support for not using old ClassUtils | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #1469 | License | MIT Commits ------- 4753fc3 [Turbo] Fixing support for not using old ClassUtils
2 parents 40d9f11 + 4753fc3 commit adb2cbb

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

src/Turbo/src/Bridge/Mercure/Broadcaster.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
namespace Symfony\UX\Turbo\Bridge\Mercure;
1313

14-
use Doctrine\Common\Util\ClassUtils;
1514
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1615
use Symfony\Component\Mercure\HubInterface;
1716
use Symfony\Component\Mercure\Update;
18-
use Symfony\Component\VarExporter\LazyObjectInterface;
1917
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
18+
use Symfony\UX\Turbo\Doctrine\ClassUtil;
2019

2120
/**
2221
* Broadcasts updates rendered using Twig with Mercure.
@@ -63,14 +62,7 @@ public function broadcast(object $entity, string $action, array $options): void
6362
return;
6463
}
6564

66-
if ($entity instanceof LazyObjectInterface) {
67-
$entityClass = get_parent_class($entity);
68-
if (false === $entityClass) {
69-
throw new \LogicException('Parent class missing');
70-
}
71-
} else {
72-
$entityClass = ClassUtils::getClass($entity);
73-
}
65+
$entityClass = ClassUtil::getEntityClass($entity);
7466

7567
if (!isset($options['rendered_action'])) {
7668
throw new \InvalidArgumentException(sprintf('Cannot broadcast entity of class "%s" as option "rendered_action" is missing.', $entityClass));

src/Turbo/src/Broadcaster/TwigBroadcaster.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
namespace Symfony\UX\Turbo\Broadcaster;
1313

14-
use Doctrine\Common\Util\ClassUtils;
15-
use Symfony\Component\VarExporter\LazyObjectInterface;
14+
use Symfony\UX\Turbo\Doctrine\ClassUtil;
1615
use Twig\Environment;
1716

1817
/**
@@ -44,15 +43,7 @@ public function broadcast(object $entity, string $action, array $options): void
4443
$options['id'] = $id;
4544
}
4645

47-
// handle proxies (both styles)
48-
if ($entity instanceof LazyObjectInterface) {
49-
$class = get_parent_class($entity);
50-
if (false === $class) {
51-
throw new \LogicException('Parent class missing');
52-
}
53-
} else {
54-
$class = ClassUtils::getClass($entity);
55-
}
46+
$class = ClassUtil::getEntityClass($entity);
5647

5748
if (null === $template = $options['template'] ?? null) {
5849
$template = $class;

src/Turbo/src/Doctrine/BroadcastListener.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
use Doctrine\Common\Annotations\Reader;
1515
use Doctrine\Common\EventArgs;
16-
use Doctrine\Common\Util\ClassUtils;
1716
use Doctrine\ORM\EntityManagerInterface;
1817
use Doctrine\ORM\Event\OnFlushEventArgs;
1918
use Doctrine\ORM\Event\PostFlushEventArgs;
20-
use Symfony\Component\VarExporter\LazyObjectInterface;
2119
use Symfony\Contracts\Service\ResetInterface;
2220
use Symfony\UX\Turbo\Attribute\Broadcast;
2321
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
@@ -128,21 +126,13 @@ public function reset(): void
128126

129127
private function storeEntitiesToPublish(EntityManagerInterface $em, object $entity, string $property): void
130128
{
131-
// handle proxies (both styles)
132-
if ($entity instanceof LazyObjectInterface) {
133-
$class = get_parent_class($entity);
134-
if (false === $class) {
135-
throw new \LogicException('Parent class missing');
136-
}
137-
} else {
138-
$class = ClassUtils::getClass($entity);
139-
}
129+
$class = ClassUtil::getEntityClass($entity);
140130

141131
if (!isset($this->broadcastedClasses[$class])) {
142132
$this->broadcastedClasses[$class] = [];
143133
$r = null;
144134

145-
if (\PHP_VERSION_ID >= 80000 && $options = ($r = new \ReflectionClass($class))->getAttributes(Broadcast::class)) {
135+
if ($options = ($r = new \ReflectionClass($class))->getAttributes(Broadcast::class)) {
146136
foreach ($options as $option) {
147137
$this->broadcastedClasses[$class][] = $option->newInstance()->options;
148138
}

src/Turbo/src/Doctrine/ClassUtil.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\Doctrine;
13+
14+
use Symfony\Component\VarExporter\LazyObjectInterface;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class ClassUtil
20+
{
21+
public static function getEntityClass(object $entity): string
22+
{
23+
if ($entity instanceof LazyObjectInterface) {
24+
$entityClass = get_parent_class($entity);
25+
if (false === $entityClass) {
26+
throw new \LogicException('Parent class missing');
27+
}
28+
29+
return $entityClass;
30+
}
31+
32+
// @legacy for old versions of Doctrine
33+
if (class_exists(ClassUtils::class)) {
34+
return ClassUtils::getClass($entity);
35+
}
36+
37+
return $entity::class;
38+
}
39+
}

0 commit comments

Comments
 (0)