Skip to content

[Workflow] Add PHP attributes to register listeners and guards #51210

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

Merged
merged 1 commit into from
Aug 3, 2023

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Aug 1, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR

Allow this:

diff --git a/src/EventSubscriber/TransitionEventSubscriber.php b/src/EventSubscriber/TransitionEventSubscriber.php
index 3897e54..4ea0d46 100644
--- a/src/EventSubscriber/TransitionEventSubscriber.php
+++ b/src/EventSubscriber/TransitionEventSubscriber.php
@@ -2,18 +2,19 @@
 
 namespace App\EventSubscriber;
 
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
 use Symfony\Component\Security\Core\User\UserInterface;
+use Symfony\Component\Workflow\Attribute\AsTransitionListener;
 use Symfony\Component\Workflow\Event\TransitionEvent;
 
-class TransitionEventSubscriber implements EventSubscriberInterface
+class TransitionEventSubscriber
 {
     public function __construct(
         private readonly TokenStorageInterface $tokenStorage,
     ) {
     }
 
+    #[AsTransitionListener()]
     public function onWorkflowArticleTransition(TransitionEvent $event): void
     {
         $context = $event->getContext();

All theses syntax are supported:

#[AsTransitionListener()]
#[AsTransitionListener(workflow: 'my-workflow')]
#[AsTransitionListener(workflow: 'my-workflow', transition: 'some-transition')]
public function onWorkflowArticleTransition(TransitionEvent $event): void

Note: This is not possible to validate the workflow name,nor the transition name, because workflow can be build dynamically

@lyrixx lyrixx force-pushed the workflow-attributes branch from 1b14718 to 4c12430 Compare August 1, 2023 16:14
@lyrixx
Copy link
Member Author

lyrixx commented Aug 1, 2023

@nicolas-grekas Thanks for the review. I addressed your comments

@nicolas-grekas nicolas-grekas changed the title [Workflow] Add some PHP attributes to register listeners and guards [Workflow] Add PHP attributes to register listeners and guards Aug 1, 2023
@lyrixx lyrixx force-pushed the workflow-attributes branch from 4c12430 to 7d9bb01 Compare August 2, 2023 09:43
Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

👍

@lyrixx lyrixx force-pushed the workflow-attributes branch from 7d9bb01 to dcc3ce4 Compare August 2, 2023 14:26
@nicolas-grekas
Copy link
Member

Thank you @lyrixx.

@nicolas-grekas nicolas-grekas merged commit 623e921 into symfony:6.4 Aug 3, 2023
int $priority = 0,
string $dispatcher = null,
) {
parent::__construct($this->buildEventName('announce', 'transition', $workflow, $transition), $method, $priority, $dispatcher);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
parent::__construct($this->buildEventName('announce', 'transition', $workflow, $transition), $method, $priority, $dispatcher);
parent::__construct(self::buildEventName('announce', 'transition', $workflow, $transition), $method, $priority, $dispatcher);

(same for other attributes)

Copy link
Member

@nicolas-grekas nicolas-grekas Aug 3, 2023

Choose a reason for hiding this comment

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

static:: then nvm, private method

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.

4 participants