8000 feature #60195 [Workflow] Deprecate `Event::getWorkflow()` method (ly… · symfony/symfony@f2357a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2357a0

Browse files
feature #60195 [Workflow] Deprecate Event::getWorkflow() method (lyrixx)
This PR was merged into the 7.3 branch. Discussion ---------- [Workflow] Deprecate `Event::getWorkflow()` method | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | kind of | New feature? | no | Deprecations? | yes | Issues | Fix #59748 | License | MIT ```diff +use Symfony\Component\DependencyInjection\Attribute\Target; class ContinueToState3 implements EventSubscriberInterface { + public function __construct( + #[Target('your_workflow_name')] + private readonly WorkflowInterface $workflow, + ) { + } public static function getSubscribedEvents(): array { return [ 'workflow.your_workflow_name.completed.to_state2' => ['terminateOrder', \PHP_INT_MIN], ]; } public function terminateOrder(Event $event): void { $subject = $event->getSubject(); - if ($event->getWorkflow()->workflow->can($subject, 'to_state3')) { - $event->getWorkflow()->apply($subject, 'to_state3'); + if ($this->workflow->can($subject, 'to_state3')) { + $this->workflow->apply($subject, 'to_state3'); } } } ``` If one has a listener able to run on many workflow, it need to inject a locator ```php use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; use Symfony\Component\Workflow\Attribute\AsTransitionListener; use Symfony\Component\Workflow\Event\TransitionEvent; class GenericListener { public function __construct( #[AutowireLocator('workflow', 'name')] private ServiceLocator $workflows ) { } #[AsTransitionListener()] public function doSomething(TransitionEvent $event): void { $workflow = $this->workflows->get($event->getWorkflowName()); } } ``` Commits ------- 20fbc9c [Workflow] Deprecate `Event::getWorkflow()` method
2 parents 5276de0 + 20fbc9c commit f2357a0

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

UPGRADE-7.3.md

+75
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,78 @@ VarExporter
249249
* Deprecate using `ProxyHelper::generateLazyProxy()` when native lazy proxies can be used - the method should be used to generate abstraction-based lazy decorators only
250250
* Deprecate `LazyGhostTrait` and `LazyProxyTrait`, use native lazy objects instead
251251
* Deprecate `ProxyHelper::generateLazyGhost()`, use native lazy objects instead
252+
253+
Workflow
254+
--------
255+
256+
* Deprecate `Event::getWorkflow()` method
257+
258+
Before:
259+
260+
```php
261+
use Symfony\Component\Workflow\Attribute\AsCompletedListener;
262+
use Symfony\Component\Workflow\Event\CompletedEvent;
263+
264+
class MyListener
265+
{
266+
#[AsCompletedListener('my_workflow', 'to_state2')]
267+
public function terminateOrder(CompletedEvent $event): void
268+
{
269+
$subject = $event->getSubject();
270+
if ($event->getWorkflow()->can($subject, 'to_state3')) {
271+
$event->getWorkflow()->apply($subject, 'to_state3');
272+
}
273+
}
274+
}
275+
```
276+
277+
After:
278+
279+
```php
280+
use Symfony\Component\DependencyInjection\Attribute\Target;
281+
use Symfony\Component\Workflow\Attribute\AsCompletedListener;
282+
use Symfony\Component\Workflow\Event\CompletedEvent;
283+
use Symfony\Component\Workflow\WorkflowInterface;
284+
285+
class MyListener
286+
{
287+
public function __construct(
288+
#[Target('your_workflow_name')]
289+
private readonly WorkflowInterface $workflow,
290+
) {
291+
}
292+
293+
#[AsCompletedListener('your_workflow_name', 'to_state2')]
294+
public function terminateOrder(CompletedEvent $event): void
295+
{
296+
$subject = $event->getSubject();
297+
if ($this->workflow->can($subject, 'to_state3')) {
298+
$this->workflow->apply($subject, 'to_state3');
299+
}
300+
}
301+
}
302+
```
303+
304+
Or:
305+
306+
```php
307+
use Symfony\Component\DependencyInjection\ServiceLocator;
308+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
309+
use Symfony\Component\Workflow\Attribute\AsTransitionListener;
310+
use Symfony\Component\Workflow\Event\TransitionEvent;
311+
312+
class GenericListener
313+
{
314+
public function __construct(
315+
#[AutowireLocator('workflow', 'name')]
316+
private ServiceLocator $workflows
317+
) {
318+
}
319+
320+
#[AsTransitionListener()]
321+
public function doSomething(TransitionEvent $event): void
322+
{
323+
$workflow = $this->workflows->get($event->getWorkflowName());
324+
}
325+
}
326+
```

src/Symfony/Component/Workflow/CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Deprecate `Event::getWorkflow()` method
8+
49
7.1
510
---
611

712
* Add method `getEnabledTransition()` to `WorkflowInterface`
813
* Automatically register places from transitions
914
* Add support for workflows that need to store many tokens in the marking
10-
* Add method `getName()` in event classes to build event names in subscribers
15+
* Add method `getName()` in event classes to build event names in subscribers
1116

1217
7.0
1318
---

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

+5
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ public function getTransition(): ?Transition
4646
return $this->transition;
4747
}
4848

49+
/**
50+
* @deprecated since Symfony 7.3, inject the workflow in the constructor where you need it
51+
*/
4952
public function getWorkflow(): WorkflowInterface
5053
{
54+
trigger_deprecation('symfony/workflow', '7.3', 'The "%s()" method is deprecated, inject the workflow in the constructor where you need it.', __METHOD__);
55+
5156
return $this->workflow;
5257
}
5358

src/Symfony/Component/Workflow/composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
}
2121
],
2222
"require": {
23-
"php": ">=8.2"
23+
"php": ">=8.2",
24+
"symfony/deprecation-contracts": "2.5|^3"
2425
},
2526
"require-dev": {
2627
"psr/log": "^1|^2|^3",
2728
"symfony/dependency-injection": "^6.4|^7.0",
28-
"symfony/event-dispatcher": "^6.4|^7.0",
2929
"symfony/error-handler": "^6.4|^7.0",
30-
"symfony/http-kernel": "^6.4|^7.0",
30+
"symfony/event-dispatcher": "^6.4|^7.0",
3131
"symfony/expression-language": "^6.4|^7.0",
32+
"symfony/http-kernel": "^6.4|^7.0",
3233
"symfony/security-core": "^6.4|^7.0",
3334
"symfony/stopwatch": "^6.4|^7.0",
3435
"symfony/validator": "^6.4|^7.0"

0 commit comments

Comments
 (0)
0