-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBusPassWithAttributes.php
105 lines (86 loc) · 3.49 KB
/
MessageBusPassWithAttributes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\MessageBus\Symfony;
use ICanBoogie\MessageBus\Attribute;
use LogicException;
use olvlvl\ComposerAttributeCollector\Attributes;
use ReflectionException;
use ReflectionMethod;
use ReflectionNamedType;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use function assert;
/**
* Creates handler and voter services according to their attributes.
*
* This compiler pass is meant to run before {@link MessageBusPass}.
*/
final class MessageBusPassWithAttributes implements CompilerPassInterface
{
public function __construct(
private string $tagForHandler = MessageBusPass::DEFAULT_TAG_FOR_HANDLER,
private string $attributeForMessage = MessageBusPass::DEFAULT_ATTRIBUTE_FOR_MESSAGE,
private string $tagForPermission = MessageBusPass::DEFAULT_TAG_FOR_PERMISSION,
private string $attributeForPermission = MessageBusPass::DEFAULT_ATTRIBUTE_FOR_PERMISSION,
private string $tagForVoter = MessageBusPass::DEFAULT_TAG_FOR_VOTER,
) {
}
/**
* @throws ReflectionException
*/
public function process(ContainerBuilder $container): void
{
/** @var array<class-string, Definition> $definitions */
$definitions = [];
/**
* @var array<class-string, string[]> $permissions
* Where _key_ is a command and _value_ its associated permissions.
*/
$permissions = [];
foreach (Attributes::findTargetClasses(Attribute\Permission::class) as $targetClass) {
$permissions[$targetClass->name][] = $targetClass->attribute->permission;
}
foreach (Attributes::findTargetClasses(Attribute\Handler::class) as $targetClass) {
$handler = $targetClass->name;
$message = self::resolveMessage($handler);
$definition = new Definition($handler);
$definition->addTag($this->tagForHandler, [ $this->attributeForMessage => $message ]);
foreach ($permissions[$message] ?? [] as $permission) {
$definition->addTag($this->tagForPermission, [ $this->attributeForPermission => $permission ]);
}
$definitions[$handler] = $definition;
}
foreach (Attributes::findTargetClasses(Attribute\Vote::class) as $targetClass) {
$voter = $targetClass->name;
$permission = $targetClass->attribute->permission;
$definition = new Definition($voter);
$definition->addTag($this->tagForVoter, [ $this->attributeForPermission => $permission ]);
$definitions[$voter] = $definition;
}
$container->addDefinitions($definitions);
}
/**
* @param class-string $handler
*
* @return class-string
*
* @throws ReflectionException
*/
private static function resolveMessage(string $handler): string
{
$type = (new ReflectionMethod($handler, '__invoke'))
->getParameters()[0]
->getType() ?? throw new LogicException("Expected a type for the first argument");
assert($type instanceof ReflectionNamedType);
// @phpstan-ignore-next-line
return $type->getName();
}
}