8000 [Workflow] Added `Registry::has()` to check if a workflow exists · symfony/symfony@11866e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11866e1

Browse files
committed
[Workflow] Added Registry::has() to check if a workflow exists
1 parent 7719fc7 commit 11866e1

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
8+
* Added `Registry::has()` to check if a workflow exists
89

910
5.0.0
1011
-----

src/Symfony/Component/Workflow/Registry.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ public function addWorkflow(WorkflowInterface $workflow, WorkflowSupportStrategy
2727
$this->workflows[] = [$workflow, $supportStrategy];
2828
}
2929

30+
public function has(object $subject, string $workflowName = null): bool
31+
{
32+
foreach ($this->workflows as [$workflow, $supportStrategy]) {
33+
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) {
34+
return true;
35+
}
36+
}
37+
38+
return false;
39+
}
40+
3041
/**
3142
* @return Workflow
3243
*/
3344
public function get(object $subject, string $workflowName = null)
3445
{
3546
$matched = [];
3647

37-
foreach ($this->workflows as list($workflow, $supportStrategy)) {
48+
foreach ($this->workflows as [$workflow, $supportStrategy]) {
3849
if ($this->supports($workflow, $supportStrategy, $subject, $workflowName)) {
3950
$matched[] = $workflow;
4051
}
@@ -61,7 +72,7 @@ public function get(object $subject, string $workflowName = null)
6172
public function all(object $subject): array
6273
{
6374
$matched = [];
64-
foreach ($this->workflows as list($workflow, $supportStrategy)) {
75+
foreach ($this->workflows as [$workflow, $supportStrategy]) {
6576
if ($supportStrategy->supports($workflow, $subject)) {
6677
$matched[] = $workflow;
6778
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ protected function tearDown(): void
2828
$this->registry = null;
2929
}
3030

31+
public function testHasWithMatch()
32+
{
33+
$this->assertTrue($this->registry->has(new Subject1()));
34+
}
35+
36+
public function testHasWithoutMatch()
37+
{
38+
$this->assertFalse($this->registry->has(new Subject1(), 'nope'));
39+
}
40+
3141
public function testGetWithSuccess()
3242
{
3343
$workflow = $this->registry->get(new Subject1());

0 commit comments

Comments
 (0)
0