Skip to content

Commit b80b6f9

Browse files
committed
[Turbo] Fixing a bug where saving a proxy would not trigger Broadcasts
1 parent b1aba24 commit b80b6f9

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Turbo/src/Doctrine/BroadcastListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use Doctrine\Common\Annotations\Reader;
1515
use Doctrine\Common\EventArgs;
16+
use Doctrine\Common\Util\ClassUtils;
1617
use Doctrine\ORM\EntityManagerInterface;
1718
use Doctrine\ORM\Event\OnFlushEventArgs;
1819
use Doctrine\ORM\Event\PostFlushEventArgs;
20+
use Symfony\Component\VarExporter\LazyObjectInterface;
1921
use Symfony\Contracts\Service\ResetInterface;
2022
use Symfony\UX\Turbo\Attribute\Broadcast;
2123
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
@@ -126,7 +128,12 @@ public function reset(): void
126128

127129
private function storeEntitiesToPublish(EntityManagerInterface $em, object $entity, string $property): void
128130
{
129-
$class = $entity::class;
131+
// handle proxies (both styles)
132+
if ($entity instanceof LazyObjectInterface) {
133+
$class = get_parent_class($entity);
134+
} else {
135+
$class = ClassUtils::getClass($entity);
136+
}
130137

131138
if (!isset($this->broadcastedClasses[$class])) {
132139
$this->broadcastedClasses[$class] = [];

src/Turbo/tests/BroadcastTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,15 @@ public function testExpressionLanguageBroadcast(): void
8888
'Artist 2 shows a song that does not belong to them'
8989
);
9090
}
91+
92+
public function testBroadcastWithProxy(): void
93+
{
94+
// testing that Artist is updated, even though it's saved as Proxy
95+
($client = self::createPantherClient())->request('GET', '/artistFromSong');
96+
97+
$crawler = $client->submitForm('Submit');
98+
$this->assertSelectorWillContain('#artists', 'testing artist after change');
99+
// this part is from the stream template
100+
$this->assertSelectorWillContain('#artists', ', updated)');
101+
}
91102
}

src/Turbo/tests/app/Kernel.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ protected function configureContainer(ContainerConfigurator $container): void
117117
],
118118
],
119119
]);
120-
121-
$container->services()->alias(Hub::class, 'mercure.hub.default'); // FIXME: temporary fix for a bug in https://github.com/symfony/mercure-bundle/pull/42
122120
}
123121

124122
protected function configureRoutes(RoutingConfigurator $routes): void
@@ -135,6 +133,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void
135133
$routes->add('songs', '/songs')->controller('kernel::songs');
136134
$routes->add('artists', '/artists')->controller('kernel::artists');
137135
$routes->add('artist', '/artists/{id}')->controller('kernel::artist');
136+
$routes->add('artist', '/artistFromSong')->controller('kernel::artistFromSong');
138137
}
139138

140139
public function getProjectDir(): string
@@ -265,4 +264,31 @@ public function artist(Request $request, EntityManagerInterface $doctrine, Envir
265264

266265
return new Response($twig->render('artist.html.twig', ['artist' => $artist]));
267266
}
267+
268+
public function artistFromSong(Request $request, EntityManagerInterface $doctrine, Environment $twig): Response
269+
{
270+
if (!$request->isMethod('POST')) {
271+
$artist = new Artist();
272+
$artist->name = 'testing artist';
273+
274+
$song = new Song();
275+
$song->artist = $artist;
276+
$song->title = 'testing song title';
277+
278+
$doctrine->persist($artist);
279+
$doctrine->persist($song);
280+
$doctrine->flush();
281+
} else {
282+
$id = $request->get('id');
283+
$song = $doctrine->find(Song::class, $id);
284+
$artist = $song->artist;
285+
$artist->name = $artist->name.' after change';
286+
287+
$doctrine->flush();
288+
}
289+
290+
return new Response($twig->render('artist_from_song.html.twig', [
291+
'song' => $song,
292+
]));
293+
}
268294
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block body %}
4+
<h1>Update Artist Via Song</h1>
5+
6+
<div id="artists" {{ turbo_stream_listen('App\\Entity\\Artist') }}></div>
7+
8+
<form method="post" enctype="application/x-www-form-urlencoded">
9+
<input name="id" value="{{ song.id }}">
10+
<button>Submit</button>
11+
</form>
12+
{% endblock %}

0 commit comments

Comments
 (0)