Skip to content

Commit 7c0b1cd

Browse files
committed
feature symfony#24751 [Workflow] Introduce a Workflow interface (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Workflow] Introduce a Workflow interface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | symfony#23910 | License | MIT | Doc PR | todo @chalasr I think all the points you made in 23910 has been done. Needs to update the docs too. Commits ------- e8351d8 [Workflow] Introduce a Workflow interface
2 parents 3ce9c29 + e8351d8 commit 7c0b1cd

15 files changed

+314
-47
lines changed

UPGRADE-4.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Translation
1616

1717
* The `FileDumper::setBackup()` method is deprecated and will be removed in 5.0.
1818
* The `TranslationWriter::disableBackup()` method is deprecated and will be removed in 5.0.
19+
20+
Workflow
21+
--------
22+
23+
* Deprecated the `add` method in favor of the `addWorkflow` method in `Workflow\Registry`.
24+
* Deprecated `SupportStrategyInterface` in favor of `WorkflowSupportStrategyInterface`.
25+
* Deprecated the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`.

UPGRADE-5.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Translation
1616

1717
* The `FileDumper::setBackup()` method has been removed.
1818
* The `TranslationWriter::disableBackup()` method has been removed.
19+
20+
Workflow
21+
--------
22+
23+
* `add` method has been removed use `addWorkflow` method in `Workflow\Registry` instead.
24+
* `SupportStrategyInterface` has been removed, use `WorkflowSupportStrategyInterface` instead.
25+
* `ClassInstanceSupportStrategy` has been removed, use `InstanceOfSupportStrategy` instead.

src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
1616
use Symfony\Component\Workflow\Definition;
1717
use Symfony\Component\Workflow\Registry;
18-
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
18+
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
1919
use Symfony\Component\Workflow\Transition;
2020
use Symfony\Component\Workflow\Workflow;
2121

@@ -38,11 +38,48 @@ protected function setUp()
3838
$workflow = new Workflow($definition);
3939

4040
$registry = new Registry();
41-
$registry->add($workflow, new ClassInstanceSupportStrategy(\stdClass::class));
41+
$registry->addWorkflow($workflow, new InstanceOfSupportStrategy(\stdClass::class));
4242

4343
$this->extension = new WorkflowExtension($registry);
4444
}
4545

46+
/**
47+
* @group legacy
48+
*/
49+
protected function setUpLegacyAdd()
50+
{
51+
if (!class_exists(Workflow::class)) {
52+
$this->markTestSkipped('The Workflow component is needed to run tests for this extension.');
53+
}
54+
55+
$places = array('ordered', 'waiting_for_payment', 'processed');
56+
$transitions = array(
57+
new Transition('t1', 'ordered', 'waiting_for_payment'),
58+
new Transition('t2', 'waiting_for_payment', 'processed'),
59+
);
60+
$definition = new Definition($places, $transitions);
61+
$workflow = new Workflow($definition);
62+
63+
$registry = new Registry();
64+
$registry->add($workflow, new InstanceOfSupportStrategy(\stdClass::class));
65+
66+
$this->extension = new WorkflowExtension($registry);
67+
}
68+
69+
/**
70+
* @group legacy
71+
* @expectedDeprecation Symfony\Component\Workflow\Registry::add is deprecated since Symfony 4.1. Use addWorkflow() instead.
72+
*/
73+
public function testCanTransitionLegacy()
74+
{
75+
$this->setUpLegacyAdd();
76+
$subject = new \stdClass();
77+
$subject->marking = array();
78+
79+
$this->assertTrue($this->extension->canTransition($subject, 't1'));
80+
$this->assertFalse($this->extension->canTransition($subject, 't2'));
81+
}
82+
4683
public function testCanTransition()
4784
{
4885
$subject = new \stdClass();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
498498
// Add workflow to Registry
499499
if ($workflow['supports']) {
500500
foreach ($workflow['supports'] as $supportedClassName) {
501-
$strategyDefinition = new Definition(Workflow\SupportStrategy\ClassInstanceSupportStrategy::class, array($supportedClassName));
501+
$strategyDefinition = new Definition(Workflow\SupportStrategy\InstanceOfSupportStrategy::class, array($supportedClassName));
502502
$strategyDefinition->setPublic(false);
503503
$registryDefinition->addMethodCall('add', array(new Reference($workflowId), $strategyDefinition));
504504
}

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* Deprecate the usage of `add(Workflow $workflow, $supportStrategy)` in `Workflow/Registry`, use `addWorkflow(WorkflowInterface, $supportStrategy)` instead.
8+
* Deprecate the usage of `SupportStrategyInterface`, use `WorkflowSupportStrategyInterface` instead.
9+
* The `Workflow` class now implements `WorkflowInterface`.
10+
* Deprecated the class `ClassInstanceSupportStrategy` in favor of the class `InstanceOfSupportStrategy`.
11+
412
4.0.0
513
-----
614

src/Symfony/Component/Workflow/Registry.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1515
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
16+
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
1617

1718
/**
1819
* @author Fabien Potencier <[email protected]>
@@ -25,13 +26,17 @@ class Registry
2526
/**
2627
* @param Workflow $workflow
2728
* @param SupportStrategyInterface $supportStrategy
29+
*
30+
* @deprecated since version 4.1, to be removed in 5.0. Use addWorkflow() instead.
2831
*/
2932
public function add(Workflow $workflow, $supportStrategy)
3033
{
31-
if (!$supportStrategy instanceof SupportStrategyInterface) {
32-
throw new \InvalidArgumentException('The "supportStrategy" is not an instance of SupportStrategyInterface.');
33-
}
34+
@trigger_error(sprintf('%s is deprecated since Symfony 4.1. Use addWorkflow() instead.', __METHOD__), E_USER_DEPRECATED);
35+
$this->workflows[] = array($workflow, $supportStrategy);
36+
}
3437

38+
public function addWorkflow(WorkflowInterface $workflow, WorkflowSupportStrategyInterface $supportStrategy)
39+
{
3540
$this->workflows[] = array($workflow, $supportStrategy);
3641
}
3742

@@ -61,7 +66,7 @@ public function get($subject, $workflowName = null)
6166
return $matched;
6267
}
6368

64-
private function supports(Workflow $workflow, SupportStrategyInterface $supportStrategy, $subject, $workflowName): bool
69+
private function supports(WorkflowInterface $workflow, $supportStrategy, $subject, $workflowName): bool
6570
{
6671
if (null !== $workflowName && $workflowName !== $workflow->getName()) {
6772
return false;

src/Symfony/Component/Workflow/SupportStrategy/ClassInstanceSupportStrategy.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Component\Workflow\SupportStrategy;
413

14+
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1. Use "%s" instead.', ClassInstanceSupportStrategy::class, InstanceOfSupportStrategy::class), E_USER_DEPRECATED);
15+
516
use Symfony\Component\Workflow\Workflow;
617

718
/**
819
* @author Andreas Kleemann <[email protected]>
20+
*
21+
* @deprecated since version 4.1, to be removed in 5.0. Use InstanceOfSupportStrategy instead
922
*/
1023
final class ClassInstanceSupportStrategy implements SupportStrategyInterface
1124
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\SupportStrategy;
13+
14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
16+
/**
17+
* @author Andreas Kleemann <[email protected]>
18+
* @author Amrouche Hamza <[email protected]>
19+
*/
20+
final class InstanceOfSupportStrategy implements WorkflowSupportStrategyInterface
21+
{
22+
private $className;
23+
24+
public function __construct(string $className)
25+
{
26+
$this->className = $className;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function supports(WorkflowInterface $workflow, $subject): bool
33+
{
34+
return $subject instanceof $this->className;
35+
}
36+
37+
public function getClassName(): string
38+
{
39+
return $this->className;
40+
}
41+
}

src/Symfony/Component/Workflow/SupportStrategy/SupportStrategyInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
/**
1717
* @author Andreas Kleemann <[email protected]>
18+
*
19+
* @deprecated since version 4.1, to be removed in 5.0. Use WorkflowSupportStrategyInterface instead
1820
*/
1921
interface SupportStrategyInterface
2022
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\SupportStrategy;
13+
14+
use Symfony\Component\Workflow\WorkflowInterface;
15+
16+
/**
17+
* @author Amrouche Hamza <[email protected]>
18+
*/
19+
interface WorkflowSupportStrategyInterface
20+
{
21+
public function supports(WorkflowInterface $workflow, $subject): bool;
22+
}

0 commit comments

Comments
 (0)