@@ -485,6 +485,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
485
485
486
486
foreach ($ config ['workflows ' ] as $ name => $ workflow ) {
487
487
$ type = $ workflow ['type ' ];
488
+ $ workflowId = sprintf ('%s.%s ' , $ type , $ name );
488
489
489
490
// Process Metadata (workflow + places (transition is done in the "create transition" block))
490
491
$ metadataStoreDefinition = new Definition (Workflow \Metadata \InMemoryMetadataStore::class, array (array (), array (), null ));
@@ -503,11 +504,25 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
503
504
504
505
// Create transitions
505
506
$ transitions = array ();
507
+ $ guardsConfiguration = array ();
506
508
$ transitionsMetadataDefinition = new Definition (\SplObjectStorage::class);
509
+ // Global transition counter per workflow
510
+ $ transitionCounter = 0 ;
507
511
foreach ($ workflow ['transitions ' ] as $ transition ) {
508
512
if ('workflow ' === $ type ) {
509
513
$ transitionDefinition = new Definition (Workflow \Transition::class, array ($ transition ['name ' ], $ transition ['from ' ], $ transition ['to ' ]));
510
- $ transitions [] = $ transitionDefinition ;
514
+ $ transitionDefinition ->setPublic (false );
515
+ $ transitionId = sprintf ('%s.transition.%s ' , $ workflowId , $ transitionCounter ++);
516
+ $ container ->setDefinition ($ transitionId , $ transitionDefinition );
517
+ $ transitions [] = new Reference ($ transitionId );
518
+ if (isset ($ transition ['guard ' ])) {
519
+ $ configuration = new Definition (Workflow \EventListener \GuardExpression::class);
520
+ $ configuration ->addArgument (new Reference ($ transitionId ));
521
+ $ configuration ->addArgument ($ transition ['guard ' ]);
522
+ $ configuration ->setPublic (false );
523
+ $ eventName = sprintf ('workflow.%s.guard.%s ' , $ name , $ transition ['name ' ]);
524
+ $ guardsConfiguration [$ eventName ][] = $ configuration ;
525
+ }
511
526
if ($ transition ['metadata ' ]) {
512
527
$ transitionsMetadataDefinition ->addMethodCall ('attach ' , array (
513
528
$ transitionDefinition ,
@@ -518,7 +533,18 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
518
533
foreach ($ transition ['from ' ] as $ from ) {
519
534
foreach ($ transition ['to ' ] as $ to ) {
520
535
$ transitionDefinition = new Definition (Workflow \Transition::class, array ($ transition ['name ' ], $ from , $ to ));
521
- $ transitions [] = $ transitionDefinition ;
536
+ $ transitionDefinition ->setPublic (false );
537
+ $ transitionId = sprintf ('%s.transition.%s ' , $ workflowId , $ transitionCounter ++);
538
+ $ container ->setDefinition ($ transitionId , $ transitionDefinition );
539
+ $ transitions [] = new Reference ($ transitionId );
540
+ if (isset ($ transition ['guard ' ])) {
541
+ $ configuration = new Definition (Workflow \EventListener \GuardExpression::class);
542
+ $ configuration ->addArgument (new Reference ($ transitionId ));
543
+ $ configuration ->addArgument ($ transition ['guard ' ]);
544
+ $ configuration ->setPublic (false );
545
+ $ eventName = sprintf ('workflow.%s.guard.%s ' , $ name , $ transition ['name ' ]);
546
+ $ guardsConfiguration [$ eventName ][] = $ configuration ;
547
+ }
522
548
if ($ transition ['metadata ' ]) {
523
549
$ transitionsMetadataDefinition ->addMethodCall ('attach ' , array (
524
550
$ transitionDefinition ,
@@ -560,7 +586,6 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
560
586
}
561
587
562
588
// Create Workflow
563
- $ workflowId = sprintf ('%s.%s ' , $ type , $ name );
564
589
$ workflowDefinition = new ChildDefinition (sprintf ('%s.abstract ' , $ type ));
565
590
$ workflowDefinition ->replaceArgument (0 , new Reference (sprintf ('%s.definition ' , $ workflowId )));
566
591
if (isset ($ markingStoreDefinition )) {
@@ -596,16 +621,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
596
621
}
597
622
598
623
// Add Guard Listener
599
- $ guard = new Definition (Workflow \EventListener \GuardListener::class);
600
- $ guard ->setPrivate (true );
601
- $ configuration = array ();
602
- foreach ($ workflow ['transitions ' ] as $ config ) {
603
- $ transitionName = $ config ['name ' ];
604
-
605
- if (!isset ($ config ['guard ' ])) {
606
- continue ;
607
- }
608
-
624
+ if ($ guardsConfiguration ) {
609
625
if (!class_exists (ExpressionLanguage::class)) {
610
626
throw new LogicException ('Cannot guard workflows as the ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language". ' );
611
627
}
@@ -614,20 +630,21 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
614
630
throw new LogicException ('Cannot guard workflows as the Security component is not installed. Try running "composer require symfony/security". ' );
615
631
}
616
632
617
- $ eventName = sprintf ('workflow.%s.guard.%s ' , $ name , $ transitionName );
618
- $ guard ->addTag ('kernel.event_listener ' , array ('event ' => $ eventName , 'method ' => 'onTransition ' ));
619
- $ configuration [$ eventName ] = $ config ['guard ' ];
620
- }
621
- if ($ configuration ) {
633
+ $ guard = new Definition (Workflow \EventListener \GuardListener::class);
634
+ $ guard ->setPrivate (true );
635
+
622
636
$ guard ->setArguments (array (
623
- $ configuration ,
637
+ $ guardsConfiguration ,
624
638
new Reference ('workflow.security.expression_language ' ),
625
639
new Reference ('security.token_storage ' ),
626
640
new Reference ('security.authorization_checker ' ),
627
641
new Reference ('security.authentication.trust_resolver ' ),
628
642
new Reference ('security.role_hierarchy ' ),
629
643
new Reference ('validator ' , ContainerInterface::NULL_ON_INVALID_REFERENCE ),
630
644
));
645
+ foreach ($ guardsConfiguration as $ eventName => $ config ) {
646
+ $ guard ->addTag ('kernel.event_listener ' , array ('event ' => $ eventName , 'method ' => 'onTransition ' ));
647
+ }
631
648
632
649
$ container ->setDefinition (sprintf ('%s.listener.guard ' , $ workflowId ), $ guard );
633
650
$ container ->setParameter ('workflow.has_guard_listeners ' , true );
0 commit comments