Skip to content

Navigation Menu

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

Appearance settings

Commit f59403d

Browse filesBrowse files
author
Amrouche Hamza
committed
[Workflow] Introduce a Workflow interface
1 parent a522e04 commit f59403d
Copy full SHA for f59403d

10 files changed

+178
-41
lines changed

‎UPGRADE-4.0.md

Copy file name to clipboardExpand all lines: UPGRADE-4.0.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,9 @@ Workflow
913913
--------
914914

915915
* Removed class name support in `WorkflowRegistry::add()` as second parameter.
916-
916+
* Deprecated the `add` method in favor of the `addWorkflow` method in `Workflow\Registry`
917+
* Deprecated the interface `SupportStrategyInterface` in favor of the interface `SupportStrategryWorkflowInterface`
918+
917919
Yaml
918920
----
919921

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/WorkflowExtensionTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ 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 ClassInstanceSupportStrategy(\stdClass::class));
4242

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

‎src/Symfony/Component/Workflow/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/CHANGELOG.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CHANGELOG
55
-----
66

77
* Removed class name support in `WorkflowRegistry::add()` as second parameter.
8+
* Deprecate the usage of `add(Workflow $workflow, $supportStrategy)` in `Workflow/Registry` use `addWorkflow(WorkflowInterface, $supportStrategy)` instead
9+
* Deprecate the usage of `SupportStrategyInterface` use `SupportStrategyWorkflowInterface` instead
10+
* The `Workflow` class now implements `WorkflowInterface`
811

912
3.4.0
1013
-----

‎src/Symfony/Component/Workflow/Registry.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Registry.php
+12-3Lines changed: 12 additions & 3 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\SupportStrategyWorkflowInterface;
1617

1718
/**
1819
* @author Fabien Potencier <fabien@symfony.com>
@@ -28,8 +29,16 @@ class Registry
2829
*/
2930
public function add(Workflow $workflow, $supportStrategy)
3031
{
31-
if (!$supportStrategy instanceof SupportStrategyInterface) {
32-
throw new \InvalidArgumentException('The "supportStrategy" is not an instance of SupportStrategyInterface.');
32+
@trigger_error('add is deprecated since Symfony 4.0. Use addWorkflow instead.', E_USER_DEPRECATED);
33+
$this->addWorkflow($workflow, $supportStrategy);
34+
}
35+
36+
public function addWorkflow(WorkflowInterface $workflow, $supportStrategy)
37+
{
38+
if ($supportStrategy instanceof SupportStrategyInterface) {
39+
@trigger_error('Passing a "SupportStrategyInterface" in "Registry::addWorkflow" is deprecated since 4.0. Use "SupportStrategyWorkflowInterface" instead.', E_USER_DEPRECATED);
40+
} elseif (!($supportStrategy instanceof SupportStrategyWorkflowInterface || $supportStrategy instanceof SupportStrategyInterface)) {
41+
throw new \InvalidArgumentException('The "supportStrategy" is not an instance of SupportStrategyWorkflowInterface.');
3342
}
3443

3544
$this->workflows[] = array($workflow, $supportStrategy);
@@ -61,7 +70,7 @@ public function get($subject, $workflowName = null)
6170
return $matched;
6271
}
6372

64-
private function supports(Workflow $workflow, SupportStrategyInterface $supportStrategy, $subject, $workflowName): bool
73+
private function supports(WorkflowInterface $workflow, SupportStrategyWorkflowInterface $supportStrategy, $subject, $workflowName): bool
6574
{
6675
if (null !== $workflowName && $workflowName !== $workflow->getName()) {
6776
return false;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/SupportStrategy/ClassInstanceSupportStrategy.php
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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

5-
use Symfony\Component\Workflow\Workflow;
14+
use Symfony\Component\Workflow\WorkflowInterface;
615

716
/**
817
* @author Andreas Kleemann <akleemann@inviqa.com>
918
*/
10-
final class ClassInstanceSupportStrategy implements SupportStrategyInterface
19+
final class ClassInstanceSupportStrategy implements SupportStrategyWorkflowInterface
1120
{
1221
private $className;
1322

@@ -19,7 +28,7 @@ public function __construct(string $className)
1928
/**
2029
* {@inheritdoc}
2130
*/
22-
public function supports(Workflow $workflow, $subject)
31+
public function supports(WorkflowInterface $workflow, $subject)
2332
{
2433
return $subject instanceof $this->className;
2534
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/SupportStrategy/SupportStrategyInterface.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Workflow\SupportStrategy;
1313

14+
@trigger_error('"SupportStrategyInterface" is deprecated since Symfony 4.0. Use SupportStrategyWorkflowInterface instead.', E_USER_DEPRECATED);
15+
1416
use Symfony\Component\Workflow\Workflow;
1517

1618
/**
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 <hamza.simperfit@gmail.com>
18+
*/
19+
interface SupportStrategyWorkflowInterface
20+
{
21+
/**
22+
* @param WorkflowInterface $workflow
23+
* @param object $subject
24+
*
25+
* @return bool
26+
*/
27+
public function supports(WorkflowInterface $workflow, $subject);
28+
}

‎src/Symfony/Component/Workflow/Tests/RegistryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Tests/RegistryTest.php
+32-3Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
99
use Symfony\Component\Workflow\Registry;
1010
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface;
11+
use Symfony\Component\Workflow\SupportStrategy\SupportStrategyWorkflowInterface;
1112
use Symfony\Component\Workflow\Workflow;
1213

1314
class RegistryTest extends TestCase
@@ -18,16 +19,27 @@ protected function setUp()
1819
{
1920
$this->registry = new Registry();
2021

21-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
22-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createSupportStrategy(Subject2::class));
23-
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createSupportStrategy(Subject2::class));
22+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createWorkflowSupportStrategy(Subject1::class));
23+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow2'), $this->createWorkflowSupportStrategy(Subject2::class));
24+
$this->registry->addWorkflow(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow3'), $this->createWorkflowSupportStrategy(Subject2::class));
2425
}
2526

2627
protected function tearDown()
2728
{
2829
$this->registry = null;
2930
}
3031

32+
/**
33+
* @group legacy
34+
* @expectedDeprecation "SupportStrategyInterface" is deprecated since Symfony 4.0. Use "SupportStrategyWorkflowInterface" instead.
35+
* @expectedDeprecation add is deprecated since Symfony 4.0. Use addWorkflow instead.
36+
* @expectedDeprecation Passing a SupportStrategyInterface in Registry::addWorkflow is deprecated since 4.0. Use SupportStrategyWorkflowInterface instead.
37+
*/
38+
public function testAddIsDeprecated()
39+
{
40+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
41+
}
42+
3143
public function testGetWithSuccess()
3244
{
3345
$workflow = $this->registry->get(new Subject1());
@@ -65,6 +77,9 @@ public function testGetWithNoMatch()
6577
$this->assertSame('workflow1', $w1->getName());
6678
}
6779

80+
/**
81+
* @group legacy
82+
*/
6883
private function createSupportStrategy($supportedClassName)
6984
{
7085
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock();
@@ -75,6 +90,20 @@ private function createSupportStrategy($supportedClassName)
7590

7691
return $strategy;
7792
}
93+
94+
/**
95+
* @group legacy
96+
*/
97+
private function createWorkflowSupportStrategy($supportedClassName)
98+
{
99+
$strategy = $this->getMockBuilder(SupportStrategyWorkflowInterface::class)->getMock();
100+
$strategy->expects($this->any())->method('supports')
101+
->will($this->returnCallback(function ($workflow, $subject) use ($supportedClassName) {
102+
return $subject instanceof $supportedClassName;
103+
}));
104+
105+
return $strategy;
106+
}
78107
}
79108

80109
class Subject1

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Workflow.php
+7-30Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2424
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2525
*/
26-
class Workflow
26+
class Workflow implements WorkflowInterface
2727
{
2828
private $definition;
2929
private $markingStore;
@@ -39,13 +39,7 @@ public function __construct(Definition $definition, MarkingStoreInterface $marki
3939
}
4040

4141
/**
42-
* Returns the object's Marking.
43-
*
44-
* @param object $subject A subject
45-
*
46-
* @return Marking The Marking
47-
*
48-
* @throws LogicException
42+
* {@inheritdoc}
4943
*/
5044
public function getMarking($subject)
5145
{
@@ -83,12 +77,7 @@ public function getMarking($subject)
8377
}
8478

8579
/**
86-
* Returns true if the transition is enabled.
87-
*
88-
* @param object $subject A subject
89-
* @param string $transitionName A transition
90-
*
91-
* @return bool true if the transition is enabled
80+
* {@inheritdoc}
9281
*/
9382
public function can($subject, $transitionName)
9483
{
@@ -113,15 +102,7 @@ public function can($subject, $transitionName)
113102
}
114103

115104
/**
116-
* Fire a transition.
117-
*
118-
* @param object $subject A subject
119-
* @param string $transitionName A transition
120-
*
121-
* @return Marking The new Marking
122-
*
123-
* @throws LogicException If the transition is not applicable
124-
* @throws LogicException If the transition does not exist
105+
* {@inheritdoc}
125106
*/
126107
public function apply($subject, $transitionName)
127108
{
@@ -164,11 +145,7 @@ public function apply($subject, $transitionName)
164145
}
165146

166147
/**
167-
* Returns all enabled transitions.
168-
*
169-
* @param object $subject A subject
170-
*
171-
* @return Transition[] All enabled transitions
148+
* {@inheritdoc}
172149
*/
173150
public function getEnabledTransitions($subject)
174151
{
@@ -190,15 +167,15 @@ public function getName()
190167
}
191168

192169
/**
193-
* @return Definition
170+
* {@inheritdoc}
194171
*/
195172
public function getDefinition()
196173
{
197174
return $this->definition;
198175
}
199176

200177
/**
201-
* @return MarkingStoreInterface
178+
* {@inheritdoc}
202179
*/
203180
public function getMarkingStore()
204181
{
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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;
13+
14+
use Symfony\Component\Workflow\Exception\LogicException;
15+
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
16+
17+
/**
18+
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
19+
*/
20+
interface WorkflowInterface
21+
{
22+
/**
23+
* Returns the object's Marking.
24+
*
25+
* @param object $subject A subject
26+
*
27+
* @return Marking The Marking
28+
*
29+
* @throws LogicException
30+
*/
31+
public function getMarking($subject);
32+
33+
/**
34+
* Returns true if the transition is enabled.
35+
*
36+
* @param object $subject A subject
37+
* @param string $transitionName A transition
38+
*
39+
* @return bool true if the transition is enabled
40+
*
41+
* @throws LogicException
42+
*/
43+
public function can($subject, $transitionName);
44+
45+
/**
46+
* Fire a transition.
47+
*
48+
* @param object $subject A subject
49+
* @param string $transitionName A transition
50+
*
51+
* @return Marking The new Marking
52+
*
53+
* @throws LogicException If the transition is not applicable
54+
* @throws LogicException If the transition does not exist
55+
*/
56+
public function apply($subject, $transitionName);
57+
58+
/**
59+
* Returns all enabled transitions.
60+
*
61+
* @param object $subject A subject
62+
*
63+
* @return Transition[] All enabled transitions
64+
*/
65+
public function getEnabledTransitions($subject);
66+
67+
public function getName();
68+
69+
/**
70+
* @return Definition
71+
*/
72+
public function getDefinition();
73+
74+
/**
75+
* @return MarkingStoreInterface
76+
*/
77+
public function getMarkingStore();
78+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.