Skip to content

Commit ca5ba70

Browse files
committed
Enable the possibility of deprecating events
1 parent c4e97eb commit ca5ba70

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class RegisterListenersPass implements CompilerPassInterface
2727
{
2828
private array $hotPathEvents = [];
2929
private array $noPreloadEvents = [];
30+
private array $deprecatedEvents = [];
3031

3132
/**
3233
* @return $this
@@ -48,6 +49,13 @@ public function setNoPreloadEvents(array $noPreloadEvents): static
4849
return $this;
4950
}
5051

52+
public function addDeprecatedEvent(string $event, string $package, string $version, string $message, mixed ...$args): static
53+
{
54+
$this->deprecatedEvents[$event] = ['package' => $package, 'version' => $version, 'message' => $message, 'args' => $args];
55+
56+
return $this;
57+
}
58+
5159
public function process(ContainerBuilder $container): void
5260
{
5361
if (!$container->hasDefinition('event_dispatcher') && !$container->hasAlias('event_dispatcher')) {
@@ -107,6 +115,10 @@ public function process(ContainerBuilder $container): void
107115
} elseif (isset($this->noPreloadEvents[$event['event']])) {
108116
++$noPreload;
109117
}
118+
119+
if (isset($this->deprecatedEvents[$event['event']])) {
120+
trigger_deprecation(...array_values($this->deprecatedEvents[$event['event']]));
121+
}
110122
}
111123

112124
if ($noPreload && \count($events) === $noPreload) {
@@ -158,6 +170,10 @@ public function process(ContainerBuilder $container): void
158170
} elseif (isset($this->noPreloadEvents[$args[0]])) {
159171
++$noPreload;
160172
}
173+
174+
if (isset($this->deprecatedEvents[$args[0]])) {
175+
trigger_deprecation(...array_values($this->deprecatedEvents[$args[0]]));
176+
}
161177
}
162178
if ($noPreload && \count($extractingDispatcher->listeners) === $noPreload) {
163179
$container->getDefinition($id)->addTag('container.no_preload');

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ class EventDispatcher implements EventDispatcherInterface
3434
private array $listeners = [];
3535
private array $sorted = [];
3636
private array $optimized;
37+
private array $deprecated;
3738

38-
public function __construct()
39+
public function __construct(array $deprecated = [])
3940
{
4041
if (__CLASS__ === static::class) {
4142
$this->optimized = [];
4243
}
44+
$this->deprecated = $deprecated;
4345
}
4446

4547
public function dispatch(object $event, string $eventName = null): object
@@ -125,6 +127,10 @@ public function hasListeners(string $eventName = null): bool
125127

126128
public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
127129
{
130+
if (isset($this->deprecated[$eventName])) {
131+
trigger_deprecation(...array_values($this->deprecated[$eventName]));
132+
}
133+
128134
$this->listeners[$eventName][$priority][] = $listener;
129135
unset($this->sorted[$eventName], $this->optimized[$eventName]);
130136
}

src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\EventDispatcher\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1617
use Symfony\Component\DependencyInjection\ChildDefinition;
1718
use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass;
@@ -22,13 +23,16 @@
2223
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
2324
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
2425
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
26+
use Symfony\Component\EventDispatcher\EventDispatcher;
2527
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2628
use Symfony\Component\EventDispatcher\Tests\Fixtures\CustomEvent;
2729
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedInvokableListener;
2830
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedMultiListener;
2931

3032
class RegisterListenersPassTest extends TestCase
3133
{
34+
use ExpectDeprecationTrait;
35+
3236
/**
3337
* Tests that event subscribers not implementing EventSubscriberInterface
3438
* trigger an exception.
@@ -503,6 +507,26 @@ public function testOmitEventNameOnSubscriber()
503507
];
504508
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
505509
}
510+
511+
/**
512+
* @group legacy
513+
*/
514+
public function testEventDeprecations()
515+
{
516+
$container = new ContainerBuilder();
517+
$pass = new RegisterListenersPass();
518+
$pass->addDeprecatedEvent('event', 'symfony/event-dispatcher', '7.1', 'Event is deprecated');
519+
520+
$container->addCompilerPass($pass);
521+
$container->register('event_dispatcher', EventDispatcher::class);
522+
$container->register('listener', Event::class)
523+
->setPublic(true)
524+
->addTag('kernel.event_listener', ['event' => 'event'])
525+
;
526+
527+
$this->expectDeprecation('Since symfony/event-dispatcher 7.1: Event is deprecated');
528+
$container->compile();
529+
}
506530
}
507531

508532
class SubscriberService implements EventSubscriberInterface

0 commit comments

Comments
 (0)