Skip to content

Commit a2bc93a

Browse files
feature #51210 [Workflow] Add PHP attributes to register listeners and guards (lyrixx)
This PR was merged into the 6.4 branch. Discussion ---------- [Workflow] Add PHP attributes to register listeners and guards | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | --- Allow this: ```diff 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: ```php #[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 Commits ------- dcc3ce4cba [Workflow] Add some PHP attributes to register listeners and guards
2 parents 0d1cc11 + 733205b commit a2bc93a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,29 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
11091109
$container->setParameter('workflow.has_guard_listeners', true);
11101110
}
11111111
}
1112+
1113+
$listenerAttributes = [
1114+
Workflow\Attribute\AsAnnounceListener::class,
1115+
Workflow\Attribute\AsCompletedListener::class,
1116+
Workflow\Attribute\AsEnterListener::class,
1117+
Workflow\Attribute\AsEnteredListener::class,
1118+
Workflow\Attribute\AsGuardListener::class,
1119+
Workflow\Attribute\AsLeaveListener::class,
1120+
Workflow\Attribute\AsTransitionListener::class,
1121+
];
1122+
1123+
foreach ($listenerAttributes as $attribute) {
1124+
$container->registerAttributeForAutoconfiguration($attribute, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
1125+
$tagAttributes = get_object_vars($attribute);
1126+
if ($reflector instanceof \ReflectionMethod) {
1127+
if (isset($tagAttributes['method'])) {
1128+
throw new LogicException(sprintf('"%s" attribute cannot declare a method on "%s::%s()".', $attribute::class, $reflector->class, $reflector->name));
1129+
}
1130+
$tagAttributes['method'] = $reflector->getName();
1131+
}
1132+
$definition->addTag('kernel.event_listener', $tagAttributes);
1133+
});
1134+
}
11121135
}
11131136

11141137
private function registerDebugConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void

0 commit comments

Comments
 (0)