Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit623e921

Browse files
feature#51210 [Workflow] Add PHP attributes to register listeners and guards (lyrixx)
This PR was merged into the 6.4 branch.Discussion----------[Workflow] Add PHP attributes to register listeners and guards| Q | A| ------------- | ---| Branch? | 6.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets || License | MIT| Doc PR |---Allow this:```diffdiff --git a/src/EventSubscriber/TransitionEventSubscriber.php b/src/EventSubscriber/TransitionEventSubscriber.phpindex 3897e54..4ea0d46 100644--- a/src/EventSubscriber/TransitionEventSubscriber.php+++ b/src/EventSubscriber/TransitionEventSubscriber.php@@ -2,18 +2,19 @@ namespace App\EventSubscriber;-use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\User\UserInterface;+use Symfony\Component\Workflow\Attribute\AsTransitionListener; use Symfony\Component\Workflow\Event\TransitionEvent;-class TransitionEventSubscriber implements EventSubscriberInterface+class TransitionEventSubscriber { public function __construct( private readonly TokenStorageInterface $tokenStorage, ) { }+ #[AsTransitionListener()] public function onWorkflowArticleTransition(TransitionEvent $event): void { $context = $event->getContext();```All theses syntax are supported:```php#[AsTransitionListener()]#[AsTransitionListener(workflow: 'my-workflow')]#[AsTransitionListener(workflow: 'my-workflow', transition: 'some-transition')]public function onWorkflowArticleTransition(TransitionEvent $event): void```---Note: This is not possible to validate the workflow name,nor the transition name, because workflow can be build dynamicallyCommits-------dcc3ce4 [Workflow] Add some PHP attributes to register listeners and guards
2 parents0ba946a +dcc3ce4 commit623e921

File tree

11 files changed

+391
-0
lines changed

11 files changed

+391
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,29 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
11091109
$container->setParameter('workflow.has_guard_listeners',true);
11101110
}
11111111
}
1112+
1113+
$listenerAttributes = [
1114+
Workflow\Attribute\AsAnnounceListener::class,
1115+
Workflow\Attribute\AsCompletedListener::class,
1116+
Workflow\Attribute\AsEnterListener::class,
1117+
Workflow\Attribute\AsEnteredListener::class,
1118+
Workflow\Attribute\AsGuardListener::class,
1119+
Workflow\Attribute\AsLeaveListener::class,
1120+
Workflow\Attribute\AsTransitionListener::class,
1121+
];
1122+
1123+
foreach ($listenerAttributesas$attribute) {
1124+
$container->registerAttributeForAutoconfiguration($attribute,staticfunction (ChildDefinition$definition,AsEventListener$attribute,\ReflectionClass|\ReflectionMethod$reflector) {
1125+
$tagAttributes =get_object_vars($attribute);
1126+
if ($reflectorinstanceof \ReflectionMethod) {
1127+
if (isset($tagAttributes['method'])) {
1128+
thrownewLogicException(sprintf('"%s" attribute cannot declare a method on "%s::%s()".',$attribute::class,$reflector->class,$reflector->name));
1129+
}
1130+
$tagAttributes['method'] =$reflector->getName();
1131+
}
1132+
$definition->addTag('kernel.event_listener',$tagAttributes);
1133+
});
1134+
}
11121135
}
11131136

11141137
privatefunctionregisterDebugConfiguration(array$config,ContainerBuilder$container,PhpFileLoader$loader):void
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsAnnounceListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$transition =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('announce','transition',$workflow,$transition),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsCompletedListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$transition =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('completed','transition',$workflow,$transition),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsEnterListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$place =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('enter','place',$workflow,$place),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsEnteredListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$place =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('entered','place',$workflow,$place),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsGuardListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$transition =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('guard','transition',$workflow,$transition),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsLeaveListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$place =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('leave','place',$workflow,$place),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
finalclass AsTransitionListenerextends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
publicfunction__construct(
25+
string$workflow =null,
26+
string$transition =null,
27+
string$method =null,
28+
int$priority =0,
29+
string$dispatcher =null,
30+
) {
31+
parent::__construct($this->buildEventName('transition','transition',$workflow,$transition),$method,$priority,$dispatcher);
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
namespaceSymfony\Component\Workflow\Attribute;
13+
14+
useSymfony\Component\Workflow\Exception\LogicException;
15+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*
19+
* @internal
20+
*/
21+
trait BuildEventNameTrait
22+
{
23+
privatestaticfunctionbuildEventName(string$keyword,string$argument,string$workflow =null,string$node =null):string
24+
{
25+
if (null ===$workflow) {
26+
if (null !==$node) {
27+
thrownewLogicException(sprintf('The "%s" argument of "%s" cannot be used without a "workflow" argument.',$argument,self::class));
28+
}
29+
30+
returnsprintf('workflow.%s',$keyword);
31+
}
32+
33+
if (null ===$node) {
34+
returnsprintf('workflow.%s.%s',$workflow,$keyword);
35+
}
36+
37+
returnsprintf('workflow.%s.%s.%s',$workflow,$keyword,$node);
38+
}
39+
}

‎src/Symfony/Component/Workflow/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add support for storing marking in a property
1010
* Add a profiler
1111
* Add support for multiline descriptions in PlantUML diagrams
12+
* Add PHP attributes to register listeners and guards
1213

1314
6.2
1415
---

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp