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

Skip to content

Commit 0ff2f8e

Browse files
committed
feature #26656 [Workflow][Registry] Added a new 'all' method (alexpozzi, lyrixx)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Workflow][Registry] Added a new 'all' method [Workflow][Registry] Added the 'all' method which returns all the workflows associated to a specific object #26618 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26618 | License | MIT | Doc PR | As explained in [26628](#26618 (comment)) I added the 'all' method which returns all the workflows associated to a specific object. Commits ------- ca1352d Fixed CHANGELOG baec431 [Workflow][Registry] Added the 'all' method which returns all the workflows associated to a specific object #26618
2 parents 6e95c2a + ca1352d commit 0ff2f8e

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ CHANGELOG
1111
* Added TransitionBlockers as a way to pass around reasons why exactly
1212
transitions can't be made.
1313
* Added a `MetadataStore`.
14+
* Added `Registry::all` to return all the workflows associated with the
15+
specific subject.
1416

1517
4.0.0
1618
-----

src/Symfony/Component/Workflow/Registry.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ public function get($subject, $workflowName = null)
6666
return $matched;
6767
}
6868

69+
/**
70+
* @param object $subject
71+
*
72+
* @return Workflow[]
73+
*/
74+
public function all($subject): array
75+
{
76+
$matched = array();
77+
foreach ($this->workflows as list($workflow, $supportStrategy)) {
78+
if ($supportStrategy->supports($workflow, $subject)) {
79+
$matched[] = $workflow;
80+
}
81+
}
82+
83+
return $matched;
84+
}
85+
6986
private function supports(WorkflowInterface $workflow, $supportStrategy, $subject, $workflowName): bool
7087
{
7188
if (null !== $workflowName && $workflowName !== $workflow->getName()) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ public function testGetWithNoMatch()
8181
$this->assertSame('workflow1', $w1->getName());
8282
}
8383

84+
public function testAllWithOneMatchWithSuccess()
85+
{
86+
$workflows = $this->registry->all(new Subject1());
87+
$this->assertInternalType('array', $workflows);
88+
$this->assertCount(1, $workflows);
89+
$this->assertInstanceOf(Workflow::class, $workflows[0]);
90+
$this->assertSame('workflow1', $workflows[0]->getName());
91+
}
92+
93+
public function testAllWithMultipleMatchWithSuccess()
94+
{
95+
$workflows = $this->registry->all(new Subject2());
96+
$this->assertInternalType('array', $workflows);
97+
$this->assertCount(2, $workflows);
98+
$this->assertInstanceOf(Workflow::class, $workflows[0]);
99+
$this->assertInstanceOf(Workflow::class, $workflows[1]);
100+
$this->assertSame('workflow2', $workflows[0]->getName());
101+
$this->assertSame('workflow3', $workflows[1]->getName());
102+
}
103+
104+
public function testAllWithNoMatch()
105+
{
106+
$workflows = $this->registry->all(new \stdClass());
107+
$this->assertInternalType('array', $workflows);
108+
$this->assertCount(0, $workflows);
109+
}
110+
84111
/**
85112
* @group legacy
86113
*/

0 commit comments

Comments
 (0)