1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[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 ICanBoogie\MessageBus\Symfony; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A compiler pass that inspect message bus handlers and voters to create mapping parameters. |
19
|
|
|
* |
20
|
|
|
* The following parameters are created (names are customizable): |
21
|
|
|
* |
22
|
|
|
* - `message_bus.message_to_handler`: array<class-string, string> where _key_ is the class of a message and _value_ |
23
|
|
|
* the service identifier of a message handler. |
24
|
|
|
* - `message_bus.permissions_by_message`: array<class-string, string[]> where _key_ is the class of a message and |
25
|
|
|
* _value_ an array of permissions. |
26
|
|
|
* - `message_bus.permission_to_voter`: array<string, string> where _key_ is a permission and _value_ the service |
27
|
|
|
* identifier of a voter. |
28
|
|
|
*/ |
29
|
|
|
final class MessageBusPass implements CompilerPassInterface |
30
|
|
|
{ |
31
|
|
|
public const DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER = 'message_bus.message_to_handler'; |
32
|
|
|
public const DEFAULT_TAG_FOR_HANDLER = 'message_bus.handler'; |
33
|
|
|
public const DEFAULT_ATTRIBUTE_FOR_MESSAGE = 'message'; |
34
|
|
|
public const DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER = 'message_bus.permissions_by_message'; |
35
|
|
|
public const DEFAULT_TAG_FOR_PERMISSION = 'message_bus.permission'; |
36
|
|
|
public const DEFAULT_ATTRIBUTE_FOR_PERMISSION = 'permission'; |
37
|
|
|
public const DEFAULT_PARAMETER_FOR_VOTERS = 'message_bus.permission_to_voter'; |
38
|
|
|
public const DEFAULT_TAG_FOR_VOTER = 'message_bus.voter'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $parameterForMessageToHandler |
42
|
|
|
* @param string $tagForHandler |
43
|
|
|
* @param string $attributeForMessage |
44
|
|
|
* @param string $parameterForPermissionToVoter |
45
|
|
|
* @param string $tagForPermission |
46
|
|
|
* @param string $attributeForPermission |
47
|
|
|
* @param string $parameterForVoter |
48
|
|
|
* @param string $tagForVoter |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
private string $parameterForMessageToHandler = self::DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER, |
52
|
|
|
private string $tagForHandler = self::DEFAULT_TAG_FOR_HANDLER, |
53
|
|
|
private string $attributeForMessage = self::DEFAULT_ATTRIBUTE_FOR_MESSAGE, |
54
|
|
|
private string $parameterForPermissionToVoter = self::DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER, |
55
|
|
|
private string $tagForPermission = self::DEFAULT_TAG_FOR_PERMISSION, |
56
|
|
|
private string $attributeForPermission = self::DEFAULT_ATTRIBUTE_FOR_PERMISSION, |
57
|
|
|
private string $parameterForVoter = self::DEFAULT_PARAMETER_FOR_VOTERS, |
58
|
|
|
private string $tagForVoter = self::DEFAULT_TAG_FOR_VOTER, |
59
|
|
|
) { |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function process(ContainerBuilder $container): void |
63
|
|
|
{ |
64
|
|
|
$this->processHandlersAndPermissions($container); |
65
|
|
|
$this->processVoters($container); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function processHandlersAndPermissions(ContainerBuilder $container): void |
69
|
|
|
{ |
70
|
|
|
$messageToHandler = []; |
71
|
|
|
$permissionsByMessage = []; |
72
|
|
|
|
73
|
|
|
foreach ($container->findTaggedServiceIds($this->tagForHandler) as $id => $hTags) { |
74
|
|
|
$message = $hTags[0][$this->attributeForMessage]; |
75
|
|
|
$messageToHandler[$message] = $id; |
76
|
|
|
|
77
|
|
|
foreach ($container->findDefinition($id)->getTag($this->tagForPermission) as $pTags) { |
78
|
|
|
$permissionsByMessage[$message][] = $pTags[$this->attributeForPermission]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$container->setParameter($this->parameterForMessageToHandler, $messageToHandler); |
83
|
|
|
$container->setParameter($this->parameterForPermissionToVoter, $permissionsByMessage); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function processVoters(ContainerBuilder $container): void |
87
|
|
|
{ |
88
|
|
|
$permissionToVoter = []; |
89
|
|
|
|
90
|
|
|
foreach ($container->findTaggedServiceIds($this->tagForVoter) as $id => $tags) { |
91
|
|
|
$permissionToVoter[$tags[0][$this->attributeForPermission]] = $id; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$container->setParameter($this->parameterForVoter, $permissionToVoter); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|