Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb50dc1

Browse files
committed
[FrameworkBundle] fixed guard event names for transitions
1 parentfb88bfc commitbb50dc1

File tree

5 files changed

+70
-58
lines changed

5 files changed

+70
-58
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,44 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
602602
@trigger_error(sprintf('The "type" option of the "framework.workflows.%s" configuration entry must be defined since Symfony 3.3. The default value will be "state_machine" in Symfony 4.0.',$name),E_USER_DEPRECATED);
603603
}
604604
$type =$workflow['type'];
605+
$workflowId =sprintf('%s.%s',$type,$name);
605606

607+
// Create transitions
606608
$transitions =array();
607-
foreach ($workflow['transitions']as$transition) {
609+
$guardsConfiguration =array();
610+
// Global transition counter per workflow
611+
$transitionCounter =0;
612+
foreach (array_values($workflow['transitions'])as$transition) {
608613
if ('workflow' ===$type) {
609-
$transitions[] =newDefinition(Workflow\Transition::class,array($transition['name'],$transition['from'],$transition['to']));
614+
$transitionDefinition =newDefinition(Workflow\Transition::class,array($transition['name'],$transition['from'],$transition['to']));
615+
$transitionDefinition->setPublic(false);
616+
$transitionId =sprintf('%s.transition.%s',$workflowId,$transitionCounter++);
617+
$container->setDefinition($transitionId,$transitionDefinition);
618+
$transitions[] =newReference($transitionId);
619+
if (isset($transition['guard'])) {
620+
$configuration =newDefinition(Workflow\EventListener\GuardExpression::class);
621+
$configuration->addArgument(newReference($transitionId));
622+
$configuration->addArgument($transition['guard']);
623+
$configuration->setPublic(false);
624+
$eventName =sprintf('workflow.%s.guard.%s',$name,$transition['name']);
625+
$guardsConfiguration[$eventName][] =$configuration;
626+
}
610627
}elseif ('state_machine' ===$type) {
611628
foreach ($transition['from']as$from) {
612629
foreach ($transition['to']as$to) {
613-
$transitions[] =newDefinition(Workflow\Transition::class,array($transition['name'],$from,$to));
630+
$transitionDefinition =newDefinition(Workflow\Transition::class,array($transition['name'],$from,$to));
631+
$transitionDefinition->setPublic(false);
632+
$transitionId =sprintf('%s.transition.%s',$workflowId,$transitionCounter++);
633+
$container->setDefinition($transitionId,$transitionDefinition);
634+
$transitions[] =newReference($transitionId);
635+
if (isset($transition['guard'])) {
636+
$configuration =newDefinition(Workflow\EventListener\GuardExpression::class);
637+
$configuration->addArgument(newReference($transitionId));
638+
$configuration->addArgument($transition['guard']);
639+
$configuration->setPublic(false);
640+
$eventName =sprintf('workflow.%s.guard.%s',$name,$transition['name']);
641+
$guardsConfiguration[$eventName][] =$configuration;
642+
}
614643
}
615644
}
616645
}
@@ -641,7 +670,6 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
641670
}
642671

643672
// Create Workflow
644-
$workflowId =sprintf('%s.%s',$type,$name);
645673
$workflowDefinition =newChildDefinition(sprintf('%s.abstract',$type));
646674
$workflowDefinition->replaceArgument(0,newReference(sprintf('%s.definition',$workflowId)));
647675
if (isset($markingStoreDefinition)) {
@@ -677,16 +705,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
677705
}
678706

679707
// Add Guard Listener
680-
$guard =newDefinition(Workflow\EventListener\GuardListener::class);
681-
$guard->setPrivate(true);
682-
$configuration =array();
683-
foreach ($workflow['transitions']as$config) {
684-
$transitionName =$config['name'];
685-
686-
if (!isset($config['guard'])) {
687-
continue;
688-
}
689-
708+
if ($guardsConfiguration) {
690709
if (!class_exists(ExpressionLanguage::class)) {
691710
thrownewLogicException('Cannot guard workflows as the ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
692711
}
@@ -695,20 +714,21 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
695714
thrownewLogicException('Cannot guard workflows as the Security component is not installed. Try running "composer require symfony/security".');
696715
}
697716

698-
$eventName =sprintf('workflow.%s.guard.%s',$name,$transitionName);
699-
$guard->addTag('kernel.event_listener',array('event' =>$eventName,'method' =>'onTransition'));
700-
$configuration[$eventName] =$config['guard'];
701-
}
702-
if ($configuration) {
717+
$guard =newDefinition(Workflow\EventListener\GuardListener::class);
718+
$guard->setPrivate(true);
719+
703720
$guard->setArguments(array(
704-
$configuration,
721+
$guardsConfiguration,
705722
newReference('workflow.security.expression_language'),
706723
newReference('security.token_storage'),
707724
newReference('security.authorization_checker'),
708725
newReference('security.authentication.trust_resolver'),
709726
newReference('security.role_hierarchy'),
710727
newReference('validator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
711728
));
729+
foreach ($guardsConfigurationas$eventName =>$config) {
730+
$guard->addTag('kernel.event_listener',array('event' =>$eventName,'method' =>'onTransition'));
731+
}
712732

713733
$container->setDefinition(sprintf('%s.listener.guard',$workflowId),$guard);
714734
$container->setParameter('workflow.has_guard_listeners',true);

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/workflow_with_guard_expression.xml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
<framework:argument>a</framework:argument>
1414
</framework:marking-store>
1515
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
16-
<framework:placename="draft" />
17-
<framework:placename="wait_for_journalist" />
18-
<framework:placename="approved_by_journalist" />
19-
<framework:placename="wait_for_spellchecker" />
20-
<framework:placename="approved_by_spellchecker" />
21-
<framework:placename="published" />
16+
<framework:place>draft</framework:place>
17+
<framework:place>wait_for_journalist</framework:place>
18+
<framework:place>approved_by_journalist</framework:place>
19+
<framework:place>wait_for_spellchecker</framework:place>
20+
<framework:place>approved_by_spellchecker</framework:place>
21+
<framework:place>published</framework:place>
2222
<framework:transitionname="request_review">
2323
<framework:from>draft</framework:from>
2424
<framework:to>wait_for_journalist</framework:to>

‎src/Symfony/Component/Workflow/EventListener/GuardExpression.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ class GuardExpression
1919

2020
private$expression;
2121

22-
publicfunctiongetTransition():Transition
22+
/**
23+
* @param string $expression
24+
*/
25+
publicfunction__construct(Transition$transition,$expression)
2326
{
24-
return$this->transition;
27+
$this->transition =$transition;
28+
$this->expression =$expression;
2529
}
2630

27-
publicfunctiongetExpression():string
31+
publicfunctiongetTransition()
2832
{
29-
return$this->expression;
33+
return$this->transition;
3034
}
3135

32-
publicfunction__construct(Transition$transition,string$expression)
36+
publicfunctiongetExpression()
3337
{
34-
$this->transition =$transition;
35-
$this->expression =$expression;
38+
return$this->expression;
3639
}
3740
}

‎src/Symfony/Component/Workflow/EventListener/GuardListener.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
useSymfony\Component\Validator\Validator\ValidatorInterface;
1919
useSymfony\Component\Workflow\Event\GuardEvent;
2020
useSymfony\Component\Workflow\Exception\InvalidTokenConfigurationException;
21-
useSymfony\Component\Workflow\TransitionBlocker;
2221

2322
/**
2423
* @author Grégoire Pineau <lyrixx@lyrixx.info>
@@ -63,16 +62,15 @@ public function onTransition(GuardEvent $event, $eventName)
6362
}
6463
}
6564

66-
privatefunctionvalidateGuardExpression(GuardEvent$event,string$expression)
65+
privatefunctionvalidateGuardExpression(GuardEvent$event,$expression)
6766
{
6867
if (!$this->expressionLanguage->evaluate($expression,$this->getVariables($event))) {
69-
$blocker = TransitionBlocker::createBlockedByExpressionGuardListener($expression);
70-
$event->addTransitionBlocker($blocker);
68+
$event->setBlocked(true);
7169
}
7270
}
7371

7472
// code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter
75-
privatefunctiongetVariables(GuardEvent$event):array
73+
privatefunctiongetVariables(GuardEvent$event)
7674
{
7775
$token =$this->tokenStorage->getToken();
7876

‎src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class GuardListenerTest extends TestCase
2121
private$authenticationChecker;
2222
private$validator;
2323
private$listener;
24-
private$transition;
24+
private$configuration;
2525

2626
protectedfunctionsetUp()
2727
{
28-
$configuration =array(
28+
$this->configuration =array(
2929
'test_is_granted' =>'is_granted("something")',
3030
'test_is_valid' =>'is_valid(subject)',
3131
'test_expression' =>array(
32-
newGuardExpression($this->getTransition(true),'!is_valid(subject)'),
33-
newGuardExpression($this->getTransition(true),'is_valid(subject)'),
32+
newGuardExpression(newTransition('name','from','to'),'!is_valid(subject)'),
33+
newGuardExpression(newTransition('name','from','to'),'is_valid(subject)'),
3434
),
3535
);
3636
$expressionLanguage =newExpressionLanguage();
@@ -41,7 +41,7 @@ protected function setUp()
4141
$this->authenticationChecker =$this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
4242
$trustResolver =$this->getMockBuilder(AuthenticationTrustResolverInterface::class)->getMock();
4343
$this->validator =$this->getMockBuilder(ValidatorInterface::class)->getMock();
44-
$this->listener =newGuardListener($configuration,$expressionLanguage,$tokenStorage,$this->authenticationChecker,$trustResolver,null,$this->validator);
44+
$this->listener =newGuardListener($this->configuration,$expressionLanguage,$tokenStorage,$this->authenticationChecker,$trustResolver,null,$this->validator);
4545
}
4646

4747
protectedfunctiontearDown()
@@ -104,16 +104,16 @@ public function testWithValidatorSupportedEventAndAccept()
104104

105105
publicfunctiontestWithGuardExpressionWithNotSupportedTransition()
106106
{
107-
$event =$this->createEvent(true);
108-
$this->configureValidator(false,false);
107+
$event =$this->createEvent();
108+
$this->configureValidator(false);
109109
$this->listener->onTransition($event,'test_expression');
110110

111111
$this->assertFalse($event->isBlocked());
112112
}
113113

114114
publicfunctiontestWithGuardExpressionWithSupportedTransition()
115115
{
116-
$event =$this->createEvent();
116+
$event =$this->createEvent($this->configuration['test_expression'][1]->getTransition());
117117
$this->configureValidator(true,true);
118118
$this->listener->onTransition($event,'test_expression');
119119

@@ -122,18 +122,18 @@ public function testWithGuardExpressionWithSupportedTransition()
122122

123123
publicfunctiontestGuardExpressionBlocks()
124124
{
125-
$event =$this->createEvent();
125+
$event =$this->createEvent($this->configuration['test_expression'][1]->getTransition());
126126
$this->configureValidator(true,false);
127127
$this->listener->onTransition($event,'test_expression');
128128

129129
$this->assertTrue($event->isBlocked());
130130
}
131131

132-
privatefunctioncreateEvent($newTransition =false)
132+
privatefunctioncreateEvent(Transition$transition =null)
133133
{
134134
$subject =new \stdClass();
135135
$subject->marking =newMarking();
136-
$transition =$this->getTransition($newTransition);
136+
$transition =$transition ?:newTransition('name','from','to');
137137

138138
returnnewGuardEvent($subject,$subject->marking,$transition);
139139
}
@@ -173,13 +173,4 @@ private function configureValidator($isUsed, $valid = true)
173173
->willReturn($valid ?array() :array('a violation'))
174174
;
175175
}
176-
177-
privatefunctiongetTransition($new =false)
178-
{
179-
if ($new || !$this->transition) {
180-
$this->transition =newTransition('name','from','to');
181-
}
182-
183-
return$this->transition;
184-
}
185176
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp