Skip to content

Commit 744e6e3

Browse files
committed
Composer > Add handy scripts
1 parent cac2265 commit 744e6e3

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

src/DependencyInjection/Compiler/AutoRegister.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ReflectionClass;
66
use ReflectionMethod;
77
use ReflectionNamedType;
8+
use ReflectionUnionType;
89
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
1011

@@ -63,17 +64,30 @@ public function process(ContainerBuilder $container): void
6364
}
6465

6566
$type = $parameters[0]->getType();
66-
if (null === $type || !$type instanceof ReflectionNamedType) {
67+
if (null === $type) {
6768
continue;
6869
}
6970

70-
// get the class name
71-
$handles = $type->getName();
71+
if (!$type instanceof ReflectionNamedType && !$type instanceof ReflectionUnionType) {
72+
continue;
73+
}
74+
75+
if ($type instanceof ReflectionUnionType) {
76+
$types = $type->getTypes();
77+
} else {
78+
$types = [$type];
79+
}
80+
81+
foreach ($types as $type) {
82+
if (!$type instanceof ReflectionNamedType) {
83+
continue;
84+
}
7285

73-
$tagAttributes[] = [
74-
$this->tagAttribute => $handles,
75-
'method' => $method->getName(),
76-
];
86+
$tagAttributes[] = [
87+
$this->tagAttribute => $type->getName(),
88+
'method' => $method->getName(),
89+
];
90+
}
7791
}
7892

7993
if (0 !== count($tags)) {

tests/Functional/Php8SmokeTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional;
4+
5+
use SimpleBus\Message\Bus\MessageBus;
6+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent2;
7+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEvent3;
8+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingPublicMethodAndUnion;
9+
use SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\TestKernel;
10+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11+
12+
/**
13+
* @internal
14+
* @coversNothing
15+
* @requires PHP 8.0
16+
*/
17+
class Php8SmokeTest extends KernelTestCase
18+
{
19+
protected function tearDown(): void
20+
{
21+
parent::tearDown();
22+
23+
static::$class = null;
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function itCanAutoRegisterEventSubscribersUsingPublicMethodAndUnion(): void
30+
{
31+
self::bootKernel(['environment' => 'config2_php8']);
32+
$container = self::$kernel->getContainer();
33+
34+
$event2 = new AutoEvent2();
35+
$event3 = new AutoEvent3();
36+
37+
$this->assertFalse($event2->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
38+
$this->assertFalse($event3->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
39+
40+
$eventBus = $container->get('event_bus');
41+
42+
$this->assertInstanceOf(MessageBus::class, $eventBus);
43+
44+
$eventBus->handle($event2);
45+
$eventBus->handle($event3);
46+
47+
$this->assertTrue($event2->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
48+
$this->assertTrue($event3->isHandledBy(AutoEventSubscriberUsingPublicMethodAndUnion::class));
49+
}
50+
51+
protected static function getKernelClass(): string
52+
{
53+
return TestKernel::class;
54+
}
55+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto;
4+
5+
final class AutoEventSubscriberUsingPublicMethodAndUnion
6+
{
7+
public function __invoke(AutoEvent2 | AutoEvent3 $event): void
8+
{
9+
$event->setHandledBy($this);
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
imports:
2+
- { resource: config.yml }
3+
4+
services:
5+
auto_event_subscriber_using_public_method_and_union:
6+
class: SimpleBus\SymfonyBridge\Tests\Functional\SmokeTest\Auto\AutoEventSubscriberUsingPublicMethodAndUnion
7+
tags:
8+
- { name: event_subscriber, register_public_methods: true }
9+
public: false

0 commit comments

Comments
 (0)