-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSubscriberGuardCompilePass.php
More file actions
71 lines (54 loc) · 2.52 KB
/
SubscriberGuardCompilePass.php
File metadata and controls
71 lines (54 loc) · 2.52 KB
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
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcingBundle\DependencyInjection;
use Patchlevel\EventSourcing\Subscription\Store\DoctrineSubscriptionStore;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use function array_keys;
use function sprintf;
final class SubscriberGuardCompilePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(DoctrineSubscriptionStore::class)) {
return;
}
$definition = $container->getDefinition(DoctrineSubscriptionStore::class);
$argument = $definition->getArgument(0);
if (!$argument instanceof Reference) {
throw new InvalidArgumentException(
sprintf(
'The first argument of the "%s" service must be a reference.',
DoctrineSubscriptionStore::class,
),
);
}
$subscriptionConnection = ServiceAliasResolver::resolve($container, (string)$argument);
$subscribers = $container->findTaggedServiceIds('event_sourcing.subscriber');
foreach (array_keys($subscribers) as $id) {
$definition = $container->getDefinition($id);
$arguments = $definition->getArguments();
foreach ($arguments as $index => $argument) {
if (!$argument instanceof Reference) {
continue;
}
if ($subscriptionConnection !== ServiceAliasResolver::resolve($container, (string)$argument)) {
continue;
}
$class = $definition->getClass();
$context = '';
if ($class !== null) {
$context = sprintf(' Argument %s on class %s.', $index + 1, $class);
}
throw new InvalidArgumentException(
sprintf(
'Using the same database connection for the eventstore and projections is not allowed. This configuration may result in transaction conflicts due to DDL operations, leading to system instability. Please use separate connections for the eventstore and projections to ensure safe operation.%s',
$context,
),
);
}
}
}
}