8000 feature #51493 Remove `GuardEvent::getContext()` method and add `HasC… · symfony/symfony@7288b84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7288b84

Browse files
committed
feature #51493 Remove GuardEvent::getContext() method and add HasContextTrait trait (hhamon)
This PR was merged into the 7.0 branch. Discussion ---------- Remove `GuardEvent::getContext()` method and add `HasContextTrait` trait | Q | A | ------------- | --- | Branch? | 7.0 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Related to #51484 | License | MIT | Doc PR | ~ As discussed with `@lyrixx`, the `GuardEvent::getContext` method was confusing as the context given as the 3rd argument to the WorflowInterface::appy() method is never passed along to the guard events. According to `@lyrixx`, the guard listeners must take any decisions based on the subject itself and not on the given contextual data (which are supposed to remain metadata). As a consequence, calling the `GuardEvent::getContext()` method always returned an empty context array so far. To prevent this confusion any longer, the method has been deprecated in Symfony 6.4 (see #51484) and removed in this PR. The `GuardEvent` class hierarchy no longer has the `getContext` method while other event classes keep having it. Commits ------- 2aee3ae Remove `GuardEvent::getContext()` method and add `HasContextTrait` trait
2 parents 864d54f + 2aee3ae commit 7288b84

File tree

10 files changed

+105
-9
lines changed

10 files changed

+105
-9
lines changed

UPGRADE-7.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ Workflow
490490
--------
491491

492492
* Require explicit argument when calling `Definition::setInitialPlaces()`
493+
* `GuardEvent::getContext()` method has been removed. Method was not supposed to be called within guard event listeners as it always returned an empty array anyway.
493494

494495
Yaml
495496
----

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
* Require explicit argument when calling `Definition::setInitialPlaces()`
8+
* `GuardEvent::getContext()` method has been removed. Method was not supposed to be called within guard event listeners as it always returned an empty array anyway.
89

910
6.4
1011
---

src/Symfony/Component/Workflow/Event/AnnounceEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class AnnounceEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
1628
}

src/Symfony/Component/Workflow/Event/CompletedEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class CompletedEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
1628
}

src/Symfony/Component/Workflow/Event/EnterEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class EnterEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
1628
}

src/Symfony/Component/Workflow/Event/EnteredEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class EnteredEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
1628
}

src/Symfony/Component/Workflow/Event/Event.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@
2323
*/
2424
class Event extends BaseEvent
2525
{
26-
protected array $context;
27-
2826
private object $subject;
2927
private Marking $marking;
3028
private ?Transition $transition;
3129
private ?WorkflowInterface $workflow;
3230

33-
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
31+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null)
3432
{
3533
$this->subject = $subject;
3634
$this->marking = $marking;
3735
$this->transition = $transition;
3836
$this->workflow = $workflow;
39-
$this->context = $context;
4037
}
4138

4239
public function getMarking(): Marking
@@ -68,9 +65,4 @@ public function getMetadata(string $key, string|Transition|null $subject): mixed
6865
{
6966
return $this->workflow->getMetadataStore()->getMetadata($key, $subject);
7067
}
71-
72-
public function getContext(): array
73-
{
74-
return $this->context;
75-
}
7668
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Event;
13+
14+
/**
15+
* @author Fabien Potencier <fabien@symfony.com>
16+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
17+
* @author Hugo Hamon <hugohamon@neuf.fr>
18+
*
19+
* @internal
20+
*/
21+
trait HasContextTrait
22+
{
23+
private array $context = [];
24+
25+
public function getContext(): array
26+
{
27+
return $this->context;
28+
}
29+
}

src/Symfony/Component/Workflow/Event/LeaveEvent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class LeaveEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
1628
}

src/Symfony/Component/Workflow/Event/TransitionEvent.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@
1111

1212
namespace Symfony\Component\Workflow\Event;
1313

14+
use Symfony\Component\Workflow\Marking;
15+
use Symfony\Component\Workflow\Transition;
16+
use Symfony\Component\Workflow\WorkflowInterface;
17+
1418
final class TransitionEvent extends Event
1519
{
20+
use HasContextTrait;
21+
22+
public function __construct(object $subject, Marking $marking, Transition $transition = null, WorkflowInterface $workflow = null, array $context = [])
23+
{
24+
parent::__construct($subject, $marking, $transition, $workflow);
25+
26+
$this->context = $context;
27+
}
28+
1629
public function setContext(array $context): void
1730
{
1831
$this->context = $context;

0 commit comments

Comments
 (0)
0