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

[Workflow] Add EventNameTrait to compute event name strings in subscribers #54344

Merged
merged 1 commit into from Mar 21, 2024

Conversation

squrious
Copy link
Contributor

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues N/A
License MIT

Hello!

Using the event dispatcher, we usually use event's class name to configure the event to listen to. For workflow, we
still have to use raw strings:

class WorkflowPostSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            'workflow.post.entered.published' => 'onPublishedEntered',        
        ];
    }
}

Using class names is more clear about what event we use (easier to know which event to use in the listener). Even if we already have attributes to define event listeners, the event subscriber way could be improved.

Proposal

This PR adds a trait to improve DX when using workflow events in event subscribers.

class WorkflowPostSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            PublishedEvent::get(workflowName: 'post', placeName: 'entered') => 'onPublishedEntered',        
        ];
    }
}

For a better DX, the EventNameTrait provides two methods: getNameForPlace and getNameForTransition, so the second
argument of ::get and its PHPDoc are consistent with the event type.

In event classes, it is used like:

class EnterEvent extends Event
{
    use EventNameTrait {
        use getNameForPlace as public get;
    }

    // ...
}

Cheers!

@lyrixx
Copy link
Member

lyrixx commented Mar 20, 2024

Did you see #51210?

@squrious
Copy link
Contributor Author

Did you see #51210?

Yes! But as event subscribers allow to register events too, shouldn't we take care of their DX too? Or maybe I could leverage the existing code for this?

@lyrixx
Copy link
Member

lyrixx commented Mar 20, 2024

IMHO, event listener are better thank event subscriber now.

But I think we can do something for the subscriber too. I'll review it

@derrabus
Copy link
Member

But as event subscribers allow to register events too, shouldn't we take care of their DX too?

No. I'd rather get rid of subscribers, tbh. From my POV, the only reason subscribers still exist, is the amount of busy work we would force upon downstream code if we removed them. Regarding DX improvements, I'd keep our efforts focused on attribute-based listeners.

@lyrixx
Copy link
Member

lyrixx commented Mar 20, 2024

@derrabus with SF full stack, it's easy to register listener, but without the framework, it's harder. I think it's still better to merge this PR once ready

@squrious squrious force-pushed the workflow/improve-event-names-dx branch from 6ef6cb5 to 194a623 Compare March 20, 2024 14:10
@squrious
Copy link
Contributor Author

Thanks for your feedback! I addressed your comments :)

Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a note in the CHANGELOG ?

👍🏼

@squrious squrious force-pushed the workflow/improve-event-names-dx branch from 2e4ecd0 to d231549 Compare March 20, 2024 14:26
@squrious
Copy link
Contributor Author

Can you add a note in the CHANGELOG ?

👍🏼

Done!

Copy link
Member

@lyrixx lyrixx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

trait EventNameTrait
{
/**
* Get event name for workflow and transition.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Get event name for workflow and transition.
* Gets the event name for workflow and transition.

}

/**
* Get event name for workflow and place.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Get event name for workflow and place.
* Gets the event name for workflow and place.

@fabpot fabpot force-pushed the workflow/improve-event-names-dx branch from 0aad8af to 742221f Compare March 21, 2024 10:37
@fabpot
Copy link
Member

fabpot commented Mar 21, 2024

Thank you @squrious.

@fabpot fabpot merged commit 1272c0d into symfony:7.1 Mar 21, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants