Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[EventDispatcher] Enable the possibility of deprecating events #52973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RegisterListenersPass implements CompilerPassInterface
{
private array $hotPathEvents = [];
private array $noPreloadEvents = [];
private array $deprecatedEvents = [];

/**
* @return $this
Expand All @@ -48,6 +49,13 @@ public function setNoPreloadEvents(array $noPreloadEvents): static
return $this;
}

public function addDeprecatedEvent(string $event, string $package, string $version, string $message, mixed ...$args): static
{
$this->deprecatedEvents[$event] = ['package' => $package, 'version' => $version, 'message' => $message, 'args' => $args];

return $this;
}

public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('event_dispatcher') && !$container->hasAlias('event_dispatcher')) {
Expand Down Expand Up @@ -107,6 +115,10 @@ public function process(ContainerBuilder $container): void
} elseif (isset($this->noPreloadEvents[$event['event']])) {
++$noPreload;
}

if (isset($this->deprecatedEvents[$event['event']])) {
trigger_deprecation(...array_values($this->deprecatedEvents[$event['event']]));
}
}

if ($noPreload && \count($events) === $noPreload) {
Expand Down Expand Up @@ -158,6 +170,10 @@ public function process(ContainerBuilder $container): void
} elseif (isset($this->noPreloadEvents[$args[0]])) {
++$noPreload;
}

if (isset($this->deprecatedEvents[$args[0]])) {
trigger_deprecation(...array_values($this->deprecatedEvents[$args[0]]));
}
}
if ($noPreload && \count($extractingDispatcher->listeners) === $noPreload) {
$container->getDefinition($id)->addTag('container.no_preload');
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class EventDispatcher implements EventDispatcherInterface
private array $listeners = [];
private array $sorted = [];
private array $optimized;
private array $deprecated;

public function __construct()
public function __construct(array $deprecated = [])
{
if (__CLASS__ === static::class) {
$this->optimized = [];
}
$this->deprecated = $deprecated;
}

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

public function addListener(string $eventName, callable|array $listener, int $priority = 0): void
{
if (isset($this->deprecated[$eventName])) {
trigger_deprecation(...array_values($this->deprecated[$eventName]));
}

$this->listeners[$eventName][$priority][] = $listener;
unset($this->sorted[$eventName], $this->optimized[$eventName]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\EventDispatcher\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\AttributeAutoconfigurationPass;
Expand All @@ -22,13 +23,16 @@
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Tests\Fixtures\CustomEvent;
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedInvokableListener;
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedMultiListener;

class RegisterListenersPassTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* Tests that event subscribers not implementing EventSubscriberInterface
* trigger an exception.
Expand Down Expand Up @@ -503,6 +507,26 @@ public function testOmitEventNameOnSubscriber()
];
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
}

/**
* @group legacy
*/
public function testEventDeprecations()
{
$container = new ContainerBuilder();
$pass = new RegisterListenersPass();
$pass->addDeprecatedEvent('event', 'symfony/event-dispatcher', '7.1', 'Event is deprecated');

$container->addCompilerPass($pass);
$container->register('event_dispatcher', EventDispatcher::class);
$container->register('listener', Event::class)
->setPublic(true)
->addTag('kernel.event_listener', ['event' => 'event'])
;

$this->expectDeprecation('Since symfony/event-dispatcher 7.1: Event is deprecated');
$container->compile();
}
}

class SubscriberService implements EventSubscriberInterface
Expand Down