Skip to content

Commit 4b6c286

Browse files
Merge branch '4.1'
* 4.1: Undeprecate the single-colon notation for controllers Command::addOption should allow int in $default Update symfony links to https [Form] Fixed keeping hash of equal \DateTimeInterface on submit [PhpUnitBridge] Fix typo [Routing] generate(null) should throw an exception [Form] Minor fixes in docs and cs [Workflow] Made code simpler [Config] Unset key during normalization [Form] Fixed empty data for compound date types invalidate forms on transformation failures [FrameworkBundle] fixed guard event names for transitions method buildTransitionBlockerList returns TransitionBlockerList of expected transition [FrameworkBundle] fixed guard event names for transitions [PropertyAccessor] Fix unable to write to singular property using setter while plural adder/remover exist
2 parents 9a84061 + 3d18f0a commit 4b6c286

File tree

7 files changed

+254
-27
lines changed

7 files changed

+254
-27
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
507507

508508
foreach ($config['workflows'] as $name => $workflow) {
509509
$type = $workflow['type'];
510+
$workflowId = sprintf('%s.%s', $type, $name);
510511

511512
// Process Metadata (workflow + places (transition is done in the "create transition" block))
512513
$metadataStoreDefinition = new Definition(Workflow\Metadata\InMemoryMetadataStore::class, array(array(), array(), null));
@@ -525,11 +526,25 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
525526

526527
// Create transitions
527528
$transitions = array();
529+
$guardsConfiguration = array();
528530
$transitionsMetadataDefinition = new Definition(\SplObjectStorage::class);
531+
// Global transition counter per workflow
532+
$transitionCounter = 0;
529533
foreach ($workflow['transitions'] as $transition) {
530534
if ('workflow' === $type) {
531535
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $transition['from'], $transition['to']));
532-
$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+
}
533548
if ($transition['metadata']) {
534549
$transitionsMetadataDefinition->addMethodCall('attach', array(
535550
$transitionDefinition,
@@ -540,7 +555,18 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
540555
foreach ($transition['from'] as $from) {
541556
foreach ($transition['to'] as $to) {
542557
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $from, $to));
543-
$transitions[] = $transitionDefinition;
558+
$transitionDefinition->setPublic(false);
559+
$transitionId = sprintf('%s.transition.%s', $workflowId, $transitionCounter++);
560+
$container->setDefinition($transitionId, $transitionDefinition);
561+
$transitions[] = new Reference($transitionId);
562+
if (isset($transition['guard'])) {
563+
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
564+
$configuration->addArgument(new Reference($transitionId));
565+
$configuration->addArgument($transition['guard']);
566+
$configuration->setPublic(false);
567+
$eventName = sprintf('workflow.%s.guard.%s', $name, $transition['name']);
568+
$guardsConfiguration[$eventName][] = $configuration;
569+
}
544570
if ($transition['metadata']) {
545571
$transitionsMetadataDefinition->addMethodCall('attach', array(
546572
$transitionDefinition,
@@ -582,7 +608,6 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
582608
}
583609

584610
// Create Workflow
585-
$workflowId = sprintf('%s.%s', $type, $name);
586611
$workflowDefinition = new ChildDefinition(sprintf('%s.abstract', $type));
587612
$workflowDefinition->replaceArgument(0, new Reference(sprintf('%s.definition', $workflowId)));
588613
if (isset($markingStoreDefinition)) {
@@ -619,16 +644,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
619644
}
620645

621646
// Add Guard Listener
622-
$guard = new Definition(Workflow\EventListener\GuardListener::class);
623-
$guard->setPrivate(true);
624-
$configuration = array();
625-
foreach ($workflow['transitions'] as $config) {
626-
$transitionName = $config['name'];
627-
628-
if (!isset($config['guard'])) {
629-
continue;
630-
}
631-
647+
if ($guardsConfiguration) {
632648
if (!class_exists(ExpressionLanguage::class)) {
633649
throw new LogicException('Cannot guard workflows as the ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".');
634650
}
@@ -637,20 +653,21 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
637653
throw new LogicException('Cannot guard workflows as the Security component is not installed. Try running "composer require symfony/security".');
638654
}
639655

640-
$eventName = sprintf('workflow.%s.guard.%s', $name, $transitionName);
641-
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
642-
$configuration[$eventName] = $config['guard'];
643-
}
644-
if ($configuration) {
656+
$guard = new Definition(Workflow\EventListener\GuardListener::class);
657+
$guard->setPrivate(true);
658+
645659
$guard->setArguments(array(
646-
$configuration,
660+
$guardsConfiguration,
647661
new Reference('workflow.security.expression_language'),
648662
new Reference('security.token_storage'),
649663
new Reference('security.authorization_checker'),
650664
new Reference('security.authentication.trust_resolver'),
651665
new Reference('security.role_hierarchy'),
652666
new Reference('validator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
653667
));
668+
foreach ($guardsConfiguration as $eventName => $config) {
669+
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
670+
}
654671

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

Resources/config/form.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
<argument type="service" id="form.choice_list_factory"/>
7171
</service>
7272

73+
<service id="form.type_extension.form.transformation_failure_handling" class="Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension">
74+
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
75+
<argument type="service" id="translator" on-invalid="ignore" />
76+
</service>
77+
7378
<!-- FormTypeHttpFoundationExtension -->
7479
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
7580
<argument type="service" id="form.type_extension.form.request_handler" />

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
<xsd:element name="from" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
315315
<xsd:element name="to" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
316316
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
317+
<xsd:element name="guard" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
317318
</xsd:sequence>
318319
<xsd:attribute name="name" type="xsd:string" use="required" />
319320
</xsd:complexType>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;
4+
5+
$container->loadFromExtension('framework', array(
6+
'workflows' => array(
7+
'article' => array(
8+
'type' => 'workflow',
9+
'marking_store' => array(
10+
'type' => 'multiple_state',
11+
),
12+
'supports' => array(
13+
FrameworkExtensionTest::class,
14+
),
15+
'initial_place' => 'draft',
16+
'places' => array(
17+
'draft',
18+
'wait_for_journalist',
19+
'approved_by_journalist',
20+
'wait_for_spellchecker',
21+
'approved_by_spellchecker',
22+
'published',
23+
),
24+
'transitions' => array(
25+
'request_review' => array(
26+
'from' => 'draft',
27+
'to' => array('wait_for_journalist', 'wait_for_spellchecker'),
28+
),
29+
'journalist_approval' => array(
30+
'from' => 'wait_for_journalist',
31+
'to' => 'approved_by_journalist',
32+
),
33+
'spellchecker_approval' => array(
34+
'from' => 'wait_for_spellchecker',
35+
'to' => 'approved_by_spellchecker',
36+
),
37+
'publish' => array(
38+
'from' => array('approved_by_journalist', 'approved_by_spellchecker'),
39+
'to' => 'published',
40+
'guard' => '!!true',
41+
),
42+
'publish_editor_in_chief' => array(
43+
'name' => 'publish',
44+
'from' => 'draft',
45+
'to' => 'published',
46+
'guard' => '!!false',
47+
),
48+
),
49+
),
50+
),
51+
));
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:workflow name="article" type="workflow" initial-place="draft">
11+
<framework:marking-store type="multiple_state">
12+
<framework:argument>a</framework:argument>
13+
<framework:argument>a</framework:argument>
14+
</framework:marking-store>
15+
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
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>
22+
<framework:transition name="request_review">
23+
<framework:from>draft</framework:from>
24+
<framework:to>wait_for_journalist</framework:to>
25+
<framework:to>wait_for_spellchecker</framework:to>
26+
</framework:transition>
27+
<framework:transition name="journalist_approval">
28+
<framework:from>wait_for_journalist</framework:from>
29+
<framework:to>approved_by_journalist</framework:to>
30+
</framework:transition>
31+
<framework:transition name="spellchecker_approval">
32+
<framework:from>wait_for_spellchecker</framework:from>
33+
<framework:to>approved_by_spellchecker</framework:to>
34+
</framework:transition>
35+
<framework:transition name="publish">
36+
<framework:from>approved_by_journalist</framework:from>
37+
<framework:from>approved_by_spellchecker</framework:from>
38+
<framework:to>published</framework:to>
39+
<framework:guard>!!true</framework:guard>
40+
</framework:transition>
41+
<framework:transition name="publish">
42+
<framework:from>draft</framework:from>
43+
<framework:to>published</framework:to>
44+
<framework:guard>!!false</framework:guard>
45+
</framework:transition>
46+
</framework:workflow>
47+
</framework:config>
48+
</container>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
framework:
2+
workflows:
3+
article:
4+
type: workflow
5+
marking_store:
6+
type: multiple_state
7+
supports:
8+
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
9+
initial_place: draft
10+
places:
11+
- draft
12+
- wait_for_journalist
13+
- approved_by_journalist
14+
- wait_for_spellchecker
15+
- approved_by_spellchecker
16+
- published
17+
transitions:
18+
request_review:
19+
from: [draft]
20+
to: [wait_for_journalist, wait_for_spellchecker]
21+
journalist_approval:
22+
from: [wait_for_journalist]
23+
to: [approved_by_journalist]
24+
spellchecker_approval:
25+
from: [wait_for_spellchecker]
26+
to: [approved_by_spellchecker]
27+
publish:
28+
from: [approved_by_journalist, approved_by_spellchecker]
29+
to: [published]
30+
guard: "!!true"
31+
publish_editor_in_chief:
32+
name: publish
33+
from: [draft]
34+
to: [published]
35+
guard: "!!false"

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,84 @@ public function testWorkflowMultipleTransitionsWithSameName()
323323

324324
$this->assertCount(5, $transitions);
325325

326-
$this->assertSame('request_review', $transitions[0]->getArgument(0));
327-
$this->assertSame('journalist_approval', $transitions[1]->getArgument(0));
328-
$this->assertSame('spellchecker_approval', $transitions[2]->getArgument(0));
329-
$this->assertSame('publish', $transitions[3]->getArgument(0));
330-
$this->assertSame('publish', $transitions[4]->getArgument(0));
331-
332-
$this->assertSame(array('approved_by_journalist', 'approved_by_spellchecker'), $transitions[3]->getArgument(1));
333-
$this->assertSame(array('draft'), $transitions[4]->getArgument(1));
326+
$this->assertSame('workflow.article.transition.0', (string) $transitions[0]);
327+
$this->assertSame(array(
328+
'request_review',
329+
array(
330+
'draft',
331+
),
332+
array(
333+
'wait_for_journalist', 'wait_for_spellchecker',
334+
),
335+
), $container->getDefinition($transitions[0])->getArguments());
336+
337+
$this->assertSame('workflow.article.transition.1', (string) $transitions[1]);
338+
$this->assertSame(array(
339+
'journalist_approval',
340+
array(
341+
'wait_for_journalist',
342+
),
343+
array(
344+
'approved_by_journalist',
345+
),
346+
), $container->getDefinition($transitions[1])->getArguments());
347+
348+
$this->assertSame('workflow.article.transition.2', (string) $transitions[2]);
349+
$this->assertSame(array(
350+
'spellchecker_approval',
351+
array(
352+
'wait_for_spellchecker',
353+
),
354+
array(
355+
'approved_by_spellchecker',
356+
),
357+
), $container->getDefinition($transitions[2])->getArguments());
358+
359+
$this->assertSame('workflow.article.transition.3', (string) $transitions[3]);
360+
$this->assertSame(array(
361+
'publish',
362+
array(
363+
'approved_by_journalist',
364+
'approved_by_spellchecker',
365+
),
366+
array(
367+
'published',
368+
),
369+
), $container->getDefinition($transitions[3])->getArguments());
370+
371+
$this->assertSame('workflow.article.transition.4', (string) $transitions[4]);
372+
$this->assertSame(array(
373+
'publish',
374+
array(
375+
'draft',
376+
),
377+
array(
378+
'published',
379+
),
380+
), $container->getDefinition($transitions[4])->getArguments());
381+
}
382+
383+
public function testGuardExpressions()
384+
{
385+
$container = $this->createContainerFromFile('workflow_with_guard_expression');
386+
387+
$this->assertTrue($container->hasDefinition('workflow.article.listener.guard'), 'Workflow guard listener is registered as a service');
388+
$this->assertTrue($container->hasParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter exists');
389+
$this->assertTrue(true === $container->getParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter is enabled');
390+
$guardDefinition = $container->getDefinition('workflow.article.listener.guard');
391+
$this->assertSame(array(
392+
array(
393+
'event' => 'workflow.article.guard.publish',
394+
'method' => 'onTransition',
395+
),
396+
), $guardDefinition->getTag('kernel.event_listener'));
397+
$guardsConfiguration = $guardDefinition->getArgument(0);
398+
$this->assertTrue(1 === \count($guardsConfiguration), 'Workflow guard configuration contains one element per transition name');
399+
$transitionGuardExpressions = $guardsConfiguration['workflow.article.guard.publish'];
400+
$this->assertSame('workflow.article.transition.3', (string) $transitionGuardExpressions[0]->getArgument(0));
401+
$this->assertSame('!!true', $transitionGuardExpressions[0]->getArgument(1));
402+
$this->assertSame('workflow.article.transition.4', (string) $transitionGuardExpressions[1]->getArgument(0));
403+
$this->assertSame('!!false', $transitionGuardExpressions[1]->getArgument(1));
334404
}
335405

336406
public function testWorkflowServicesCanBeEnabled()

0 commit comments

Comments
 (0)