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

Skip to content

Commit 1b14718

Browse files
committed
[Workflow] Add some PHP attributes to register listeners and guards
1 parent 7597687 commit 1b14718

File tree

11 files changed

+388
-0
lines changed

11 files changed

+388
-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 ($listenerAttributes as $attribute) {
1124+
$container->registerAttributeForAutoconfiguration($attribute, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
1125+
$tagAttributes = get_object_vars($attribute);
1126+
if ($reflector instanceof \ReflectionMethod) {
1127+
if (isset($tagAttributes['method'])) {
1128+
throw new LogicException(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
private function registerDebugConfiguration(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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsAnnounceListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsCompletedListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsEnterListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsEnteredListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsGuardListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsLeaveListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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 <[email protected]>
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\Attribute;
13+
14+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
20+
final class AsTransitionListener extends AsEventListener
21+
{
22+
use BuildEventNameTrait;
23+
24+
public function __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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Attribute;
13+
14+
/**
15+
* @author Grégoire Pineau <[email protected]>
16+
*
17+
* @internal
18+
*/
19+
trait BuildEventNameTrait
20+
{
21+
private function buildEventName(string $keyword, string $argument, string $workflow = null, string $node = null)
22+
{
23+
if (null === $workflow) {
24+
if (null !== $node) {
25+
throw new \LogicException(sprintf('The "%s" argument of "%s" cannot be used without a "workflow" argument.', $argument, self::class));
26+
}
27+
28+
return sprintf('workflow.%s', $keyword);
29+
}
30+
31+
if (null === $node) {
32+
return sprintf('workflow.%s.%s', $workflow, $keyword);
33+
}
34+
35+
return sprintf('workflow.%s.%s.%s', $workflow, $keyword, $node);
36+
}
37+
}

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
transitions and workflow's metadata into dumped graph
99
* Add support for storing marking in a property
1010
* Add a profiler
11+
* Add some PHP attributes to register listeners and guards
1112

1213
6.2
1314
---

0 commit comments

Comments
 (0)