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

Prevent an infinite loop with afterFlush events #19

Merged
merged 3 commits into from
Sep 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EventDetectionCompilerPass implements CompilerPassInterface
* @throws \ReflectionException
*/
public function process(
ContainerBuilder $container
ContainerBuilder $container,
): void {
if (!$container->hasDefinition(Generator::class)
|| !$container->has(Generator::class)
Expand Down Expand Up @@ -98,12 +98,10 @@ public function process(
$match = [];
preg_match_all('/%(.+)%/', $path, $match);

if (count($match[0] ?? [])) {
foreach ($match[0] as $i => $item) {
/** @var string $param */
$param = $container->getParameter($match[1][$i]);
$path = str_replace($item, $param, $path);
}
foreach ($match[0] as $i => $item) {
/** @var string $param */
$param = $container->getParameter($match[1][$i]);
$path = str_replace($item, $param, $path);
}

/**
Expand Down Expand Up @@ -304,7 +302,7 @@ public function process(
*/
private function validateEventReflection(
string $class,
\ReflectionClass $reflection
\ReflectionClass $reflection,
): void {
if ($reflection->isFinal()) {
throw TargetClassFinalException::new([$reflection->getName()]);
Expand All @@ -325,7 +323,7 @@ private function validateEventReflection(
*/
private function getEntityClasses(
SubEvent|Event $annotation,
\ReflectionClass $reflection
\ReflectionClass $reflection,
): array {
$entities = $annotation->entity;

Expand All @@ -348,7 +346,7 @@ private function getEntityClasses(
*/
private function validateEntityClasses(
array $classes,
string $eventClass
string $eventClass,
): void {
foreach ($classes as $cls) {
if (is_subclass_of($cls, EntityInterface::class)) { // fits by inheritance
Expand All @@ -366,7 +364,7 @@ private function validateEntityClasses(
*/
private function getSubEventTypes(
SubEvent $event,
string $class
string $class,
): array {
$types = $event->types;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DoctrineEventConverterExtension extends SymfonyExtension
*/
public function load(
array $configs,
ContainerBuilder $container
ContainerBuilder $container,
): void {
$loader = new PhpFileLoader(
$container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class AbstractEventConfiguration
* @param non-empty-list<class-string<EntityInterface>> $entities
*/
public function setEntities(
array $entities
array $entities,
): static {
$this->entities = $entities;

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Model/EventConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getType(): string
}

public function setType(
string $type
string $type,
): static {
$this->type = $type;

Expand All @@ -32,7 +32,7 @@ public function isAfterFlush(): bool
}

public function setAfterFlush(
bool $afterFlush
bool $afterFlush,
): static {
$this->afterFlush = $afterFlush;

Expand Down
16 changes: 8 additions & 8 deletions src/DependencyInjection/Model/SubEventConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getEvents(): array
* @param non-empty-list<string> $events
*/
public function setEvents(
array $events
array $events,
): static {
$this->events = $events;

Expand All @@ -65,7 +65,7 @@ public function getChanges(): array
* @param array<string, null|array{0?: mixed, 1?: mixed}> $changes
*/
public function setChanges(
array $changes
array $changes,
): static {
$this->changes = $changes;

Expand All @@ -78,7 +78,7 @@ public function getLabel(): string
}

public function setLabel(
string $label
string $label,
): static {
$this->label = $label;

Expand All @@ -97,7 +97,7 @@ public function getRequirements(): array
* @param array<string, mixed> $requirements
*/
public function setRequirements(
array $requirements
array $requirements,
): static {
$this->requirements = $requirements;

Expand All @@ -110,7 +110,7 @@ public function getPriority(): int
}

public function setPriority(
int $priority
int $priority,
): static {
$this->priority = $priority;

Expand All @@ -123,7 +123,7 @@ public function isAllMode(): bool
}

public function setAllMode(
bool $allMode
bool $allMode,
): static {
$this->allMode = $allMode;

Expand All @@ -136,7 +136,7 @@ public function isAfterFlush(): bool
}

public function setAfterFlush(
bool $afterFlush
bool $afterFlush,
): static {
$this->afterFlush = $afterFlush;

Expand All @@ -147,7 +147,7 @@ public function setAfterFlush(
* @throws SubEventRequiredFieldsException
*/
public function validate(
string $class
string $class,
): static {
if (empty($this->getChanges()) && empty($this->getRequirements())) {
throw SubEventRequiredFieldsException::new([
Expand Down
2 changes: 1 addition & 1 deletion src/DoctrineEventConverterBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DoctrineEventConverterBundle extends Bundle
private $autoloader;

public function build(
ContainerBuilder $container
ContainerBuilder $container,
): void {
$container->addCompilerPass(new EventDetectionCompilerPass());
}
Expand Down
8 changes: 4 additions & 4 deletions src/Event/AbstractEntityEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function getEntityClass(): string|null
* @noinspection PhpDocSignatureInspection
*/
public function setEntity(
EntityInterface $entity
EntityInterface $entity,
): self {
$this->entity = $entity;

Expand All @@ -66,7 +66,7 @@ public function getEntity(): EntityInterface
* @param array<string, mixed> $fields
*/
public function setChanges(
array $fields
array $fields,
): static {
$this->changes = $fields;

Expand All @@ -82,7 +82,7 @@ public function getChanges(): array
}

public function setEventType(
string $enum
string $enum,
): static {
$this->eventType = $enum;

Expand All @@ -95,7 +95,7 @@ public function getEventType(): string
}

public function setDeletedId(
int|string|null $id
int|string|null $id,
): static {
$this->id = $id;

Expand Down
2 changes: 1 addition & 1 deletion src/Event/DispatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class DispatchEvent extends Event
{
public function __construct(
private readonly AbstractEntityEvent $event
private readonly AbstractEntityEvent $event,
) {
}

Expand Down
22 changes: 11 additions & 11 deletions src/EventSubscriber/DispatchingSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,39 @@ public function __construct(
private readonly EventService $eventService,
private readonly SubEventService $subEventService,
private readonly VerifierService $verifierService,
private readonly DelayableEventDispatcher $dispatcher
private readonly DelayableEventDispatcher $dispatcher,
) {
}

public function prePersist(
PrePersistEventArgs $args
PrePersistEventArgs $args,
): void {
if ($args->getObject() instanceof EntityInterface) {
$this->process(Events::prePersist, $args->getObject());
}
}

public function preFlush(
PreFlushEventArgs $args
PreFlushEventArgs $args,
): void {
$this->preFlush = true;
}

public function postFlush(
PostFlushEventArgs $args
PostFlushEventArgs $args,
): void {
$this->dispatcher->submitDelayed();
$this->preFlush = false;
}

public function postPersist(
PostPersistEventArgs $args
PostPersistEventArgs $args,
): void {
$this->process(Events::postPersist, $args->getObject());
}

public function preUpdate(
PreUpdateEventArgs $args
PreUpdateEventArgs $args,
): void {
$changes = [];
$object = $args->getObject();
Expand All @@ -87,7 +87,7 @@ public function preUpdate(
}

public function postUpdate(
PostUpdateEventArgs $args
PostUpdateEventArgs $args,
): void {
$object = $args->getObject();
$hash = spl_object_hash($object);
Expand All @@ -98,7 +98,7 @@ public function postUpdate(
}

public function preRemove(
PreRemoveEventArgs $args
PreRemoveEventArgs $args,
): void {
$object = $args->getObject();

Expand All @@ -109,7 +109,7 @@ public function preRemove(
}

public function postRemove(
PostRemoveEventArgs $args
PostRemoveEventArgs $args,
): void {
$object = $args->getObject();
$hash = spl_object_hash($object);
Expand All @@ -128,7 +128,7 @@ private function process(
string $type,
object $obj,
int|string|null $id = null,
array $changes = []
array $changes = [],
): void {
$class = ClassUtils::getClass($obj);

Expand Down Expand Up @@ -162,7 +162,7 @@ private function process(
}

private function subEvents(
AbstractEntityEvent $event
AbstractEntityEvent $event,
): void {
$entity = $event->getEntity();
$class = ClassUtils::getClass($entity);
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/AbstractEventDistributorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AbstractEventDistributorException extends \Exception
* @param list<mixed> $arguments
*/
public static function new(
array $arguments = []
array $arguments = [],
): static {
return new static(static::formatMessage($arguments)); // @phpstan-ignore-line
}
Expand All @@ -28,7 +28,7 @@ public static function new(
* @param list<mixed> $arguments
*/
public static function formatMessage(
array $arguments = []
array $arguments = [],
): string {
return sprintf(static::MESSAGE_TEMPLATE, ...$arguments); // @phpstan-ignore-line
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Change.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Change
public function __construct(
public readonly string $name,
public readonly mixed $from = new Undefined(),
public readonly mixed $to = new Undefined()
public readonly mixed $to = new Undefined(),
) {
}
}
2 changes: 1 addition & 1 deletion src/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Event
{
public function __construct(
public readonly string $eventClass,
public readonly bool $afterFlush = false
public readonly bool $afterFlush = false,
) {
}
}
Loading
Loading