Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df6cd73

Browse files
author
Amrouche Hamza
committed
[Workflow] Introduce a Workflow interface
1 parent d8ee14f commit df6cd73

9 files changed

+173
-40
lines changed

UPGRADE-4.0.md

Lines 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 `supports(Workflow $workflow, $subject)` method in `SupportStrategyInterface` in favor of the method `supports(WorkflowInterface $workflow, $subject)` in `SupportStrategryWorkflowInterface`
918+
917919
Yaml
918920
----
919921

src/Symfony/Component/Workflow/CHANGELOG.md

Lines 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

Lines changed: 13 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 <[email protected]>
@@ -28,8 +29,17 @@ 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 SupportStategyInterface in Registry::addWorkflow is deprecated since 4.0. Use SupportStrategyWorkflowInterface instead', E_USER_DEPRECATED);
40+
}
41+
if (!($supportStrategy instanceof SupportStrategyWorkflowInterface || $supportStrategy instanceof SupportStrategyInterface)) {
42+
throw new \InvalidArgumentException('The "supportStrategy" is not an instance of SupportStrategyWorkflowInterface.');
3343
}
3444

3545
$this->workflows[] = array($workflow, $supportStrategy);
@@ -61,7 +71,7 @@ public function get($subject, $workflowName = null)
6171
return $matched;
6272
}
6373

64-
private function supports(Workflow $workflow, SupportStrategyInterface $supportStrategy, $subject, $workflowName): bool
74+
private function supports(WorkflowInterface $workflow, SupportStrategyWorkflowInterface $supportStrategy, $subject, $workflowName): bool
6575
{
6676
if (null !== $workflowName && $workflowName !== $workflow->getName()) {
6777
return false;

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

Lines 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 <[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

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

716
/**
817
* @author Andreas Kleemann <[email protected]>
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

Lines 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
/**
Lines 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 <[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 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

Lines changed: 29 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,24 @@ 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+
* @legacy
34+
*/
35+
public function testAddIsDeprecated()
36+
{
37+
$this->registry->add(new Workflow(new Definition(array(), array()), $this->getMockBuilder(MarkingStoreInterface::class)->getMock(), $this->getMockBuilder(EventDispatcherInterface::class)->getMock(), 'workflow1'), $this->createSupportStrategy(Subject1::class));
38+
}
39+
3140
public function testGetWithSuccess()
3241
{
3342
$workflow = $this->registry->get(new Subject1());
@@ -65,6 +74,9 @@ public function testGetWithNoMatch()
6574
$this->assertSame('workflow1', $w1->getName());
6675
}
6776

77+
/**
78+
* @legacy
79+
*/
6880
private function createSupportStrategy($supportedClassName)
6981
{
7082
$strategy = $this->getMockBuilder(SupportStrategyInterface::class)->getMock();
@@ -75,6 +87,20 @@ private function createSupportStrategy($supportedClassName)
7587

7688
return $strategy;
7789
}
90+
91+
/**
92+
* @legacy
93+
*/
94+
private function createWorkflowSupportStrategy($supportedClassName)
95+
{
96+
$strategy = $this->getMockBuilder(SupportStrategyWorkflowInterface::class)->getMock();
97+
$strategy->expects($this->any())->method('supports')
98+
->will($this->returnCallback(function ($workflow, $subject) use ($supportedClassName) {
99+
return $subject instanceof $supportedClassName;
100+
}));
101+
102+
return $strategy;
103+
}
78104
}
79105

80106
class Subject1

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Grégoire Pineau <[email protected]>
2424
* @author Tobias Nyholm <[email protected]>
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
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;
13+
14+
use Symfony\Component\Workflow\Exception\LogicException;
15+
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
16+
17+
/**
18+
* @author Amrouche Hamza <[email protected]>
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+
public function can($subject, $transitionName);
42+
43+
/**
44+
* Fire a transition.
45+
*
46+
* @param object $subject A subject
47+
* @param string $transitionName A transition
48+
*
49+
* @return Marking The new Marking
50+
*
51+
* @throws LogicException If the transition is not applicable
52+
* @throws LogicException If the transition does not exist
53+
*/
54+
public function apply($subject, $transitionName);
55+
56+
/**
57+
* Returns all enabled transitions.
58+
*
59+
* @param object $subject A subject
60+
*
61+
* @return Transition[] All enabled transitions
62+
*/
63+
public function getEnabledTransitions($subject);
64+
65+
public function getName();
66+
67+
/**
68+
* @return Definition
69+
*/
70+
public function getDefinition();
71+
72+
/**
73+
* @return MarkingStoreInterface
74+
*/
75+
public function getMarkingStore();
76+
}

0 commit comments

Comments
 (0)