Skip to content

Commit 9795930

Browse files
committed
[Turbo] Make the Broadcast attribute repeatable
1 parent 6afcd61 commit 9795930

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/Turbo/Attribute/Broadcast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @experimental
2525
*/
26-
#[\Attribute(\Attribute::TARGET_CLASS)]
26+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
2727
final class Broadcast
2828
{
2929
public const ACTION_CREATE = 'create';

src/Turbo/Doctrine/BroadcastListener.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class BroadcastListener implements ResetInterface
3333
private $annotationReader;
3434

3535
/**
36-
* @var array<class-string, mixed[]|false>
36+
* @var array<class-string, array[]>
3737
*/
3838
private $broadcastedClasses;
3939

@@ -96,16 +96,23 @@ public function postFlush(EventArgs $eventArgs): void
9696
try {
9797
foreach ($this->createdEntities as $entity) {
9898
$options = $this->createdEntities[$entity];
99-
$options['id'] = $em->getClassMetadata(\get_class($entity))->getIdentifierValues($entity);
100-
$this->broadcaster->broadcast($entity, Broadcast::ACTION_CREATE, $options);
99+
$id = $em->getClassMetadata(\get_class($entity))->getIdentifierValues($entity);
100+
foreach ($options as $option) {
101+
$option['id'] = $id;
102+
$this->broadcaster->broadcast($entity, Broadcast::ACTION_CREATE, $option);
103+
}
101104
}
102105

103106
foreach ($this->updatedEntities as $entity) {
104-
$this->broadcaster->broadcast($entity, Broadcast::ACTION_UPDATE, $this->updatedEntities[$entity]);
107+
foreach ($this->updatedEntities[$entity] as $option) {
108+
$this->broadcaster->broadcast($entity, Broadcast::ACTION_UPDATE, $option);
109+
}
105110
}
106111

107112
foreach ($this->removedEntities as $entity) {
108-
$this->broadcaster->broadcast($entity, Broadcast::ACTION_REMOVE, $this->removedEntities[$entity]);
113+
foreach ($this->removedEntities[$entity] as $option) {
114+
$this->broadcaster->broadcast($entity, Broadcast::ACTION_REMOVE, $option);
115+
}
109116
}
110117
} finally {
111118
$this->reset();
@@ -124,21 +131,30 @@ private function storeEntitiesToPublish(EntityManagerInterface $em, object $enti
124131
$class = \get_class($entity);
125132

126133
if (!isset($this->broadcastedClasses[$class])) {
127-
$this->broadcastedClasses[$class] = false;
134+
$this->broadcastedClasses[$class] = [];
128135
$r = null;
129136

130137
if (\PHP_VERSION_ID >= 80000 && $options = ($r = new \ReflectionClass($class))->getAttributes(Broadcast::class)) {
131-
$options = $options[0]->newInstance();
132-
$this->broadcastedClasses[$class] = $options->options;
133-
} elseif ($this->annotationReader && $options = $this->annotationReader->getClassAnnotation($r ?? new \ReflectionClass($class), Broadcast::class)) {
134-
$this->broadcastedClasses[$class] = $options->options;
138+
foreach ($options as $option) {
139+
$this->broadcastedClasses[$class][] = $option->newInstance()->options;
140+
}
141+
} elseif ($this->annotationReader && $options = $this->annotationReader->getClassAnnotations($r ?? new \ReflectionClass($class))) {
142+
foreach ($options as $option) {
143+
if ($option instanceof Broadcast) {
144+
$this->broadcastedClasses[$class][] = $option->options;
145+
}
146+
}
135147
}
136148
}
137149

138-
if (false !== $options = $this->broadcastedClasses[$class]) {
150+
if ($options = $this->broadcastedClasses[$class]) {
139151
if ('createdEntities' !== $property) {
140-
$options['id'] = $em->getClassMetadata($class)->getIdentifierValues($entity);
152+
$id = $em->getClassMetadata($class)->getIdentifierValues($entity);
153+
foreach ($options as $k => $option) {
154+
$options[$k]['id'] = $id;
155+
}
141156
}
157+
142158
$this->{$property}->attach($entity, $options);
143159
}
144160
}

0 commit comments

Comments
 (0)