-
-
Notifications
You must be signed in to change notification settings - Fork 312
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] Fix broadcast with composite primary key #1857
Open
chapterjason
wants to merge
10
commits into
symfony:2.x
Choose a base branch
from
chapterjason:fix-composite-primary-key
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ccde693
Add test
chapterjason 67d29a5
Fix id formatting
chapterjason 1a98973
Fix composite keys for broadcast
chapterjason 2c01cb9
Add missing injection
chapterjason 59f44bd
Fix fabbot
chapterjason 47ba656
Fix PHPStan
chapterjason dfb3300
Fix fabbot
chapterjason 157de88
Fix service
chapterjason d5c5925
Fix real class resolve
chapterjason cba61dc
Fix fabbot
chapterjason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\Broadcaster; | ||
|
||
/** | ||
* Formats an id array to a string. | ||
* | ||
* In defaults the id array is something like `['id' => 1]` or `['uuid' => '00000000-0000-0000-0000-000000000000']`. | ||
* For a composite key it could be something like `['cart' => ['id' => 1], 'product' => ['id' => 1]]`. | ||
* | ||
* To create a string representation of the id, the values of the array are flattened and concatenated with a dash. | ||
* | ||
* @author Jason Schilling <jason@sourecode.dev> | ||
*/ | ||
class IdFormatter | ||
{ | ||
/** | ||
* @param array<string, array<string, string>>|array<string, string>|string $id | ||
*/ | ||
public function format(array|string $id): string | ||
{ | ||
if (\is_string($id)) { | ||
return $id; | ||
} | ||
|
||
$flatten = []; | ||
|
||
array_walk_recursive($id, static function ($item) use (&$flatten) { $flatten[] = $item; }); | ||
|
||
return implode('-', $flatten); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ final class BroadcastListener implements ResetInterface | |
{ | ||
private $broadcaster; | ||
private $annotationReader; | ||
private $doctrineIdAccessor; | ||
private $doctrineClassResolver; | ||
|
||
/** | ||
* @var array<class-string, array<mixed>> | ||
|
@@ -48,12 +50,14 @@ final class BroadcastListener implements ResetInterface | |
*/ | ||
private $removedEntities; | ||
|
||
public function __construct(BroadcasterInterface $broadcaster, ?Reader $annotationReader = null) | ||
public function __construct(BroadcasterInterface $broadcaster, ?Reader $annotationReader = null, ?DoctrineIdAccessor $doctrineIdAccessor = null, ?DoctrineClassResolver $doctrineClassResolver = null) | ||
{ | ||
$this->reset(); | ||
|
||
$this->broadcaster = $broadcaster; | ||
$this->annotationReader = $annotationReader; | ||
$this->doctrineIdAccessor = $doctrineIdAccessor ?? new DoctrineIdAccessor(); | ||
$this->doctrineClassResolver = $doctrineClassResolver ?? new DoctrineClassResolver(); | ||
} | ||
|
||
/** | ||
|
@@ -94,7 +98,7 @@ public function postFlush(EventArgs $eventArgs): void | |
try { | ||
foreach ($this->createdEntities as $entity) { | ||
$options = $this->createdEntities[$entity]; | ||
$id = $em->getClassMetadata($entity::class)->getIdentifierValues($entity); | ||
$id = $this->doctrineIdAccessor->getEntityId($entity, $em); | ||
foreach ($options as $option) { | ||
$option['id'] = $id; | ||
$this->broadcaster->broadcast($entity, Broadcast::ACTION_CREATE, $option); | ||
|
@@ -126,28 +130,28 @@ public function reset(): void | |
|
||
private function storeEntitiesToPublish(EntityManagerInterface $em, object $entity, string $property): void | ||
{ | ||
$class = ClassUtil::getEntityClass($entity); | ||
$entityClass = $this->doctrineClassResolver->resolve($entity, $em); | ||
|
||
if (!isset($this->broadcastedClasses[$class])) { | ||
$this->broadcastedClasses[$class] = []; | ||
$r = new \ReflectionClass($class); | ||
if (!isset($this->broadcastedClasses[$entityClass])) { | ||
$this->broadcastedClasses[$entityClass] = []; | ||
$r = new \ReflectionClass($entityClass); | ||
|
||
if ($options = $r->getAttributes(Broadcast::class)) { | ||
foreach ($options as $option) { | ||
$this->broadcastedClasses[$class][] = $option->newInstance()->options; | ||
$this->broadcastedClasses[$entityClass][] = $option->newInstance()->options; | ||
} | ||
} elseif ($this->annotationReader && $options = $this->annotationReader->getClassAnnotations($r)) { | ||
foreach ($options as $option) { | ||
if ($option instanceof Broadcast) { | ||
$this->broadcastedClasses[$class][] = $option->options; | ||
$this->broadcastedClasses[$entityClass][] = $option->options; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ($options = $this->broadcastedClasses[$class]) { | ||
if ($options = $this->broadcastedClasses[$entityClass]) { | ||
if ('createdEntities' !== $property) { | ||
$id = $em->getClassMetadata($class)->getIdentifierValues($entity); | ||
$id = $this->doctrineIdAccessor->getEntityId($entity, $em); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you illustrate precisely what cannot be done before, what it would look like, and same after your changes ? :) |
||
foreach ($options as $k => $option) { | ||
$options[$k]['id'] = $id; | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is worth the change.