From f9c6187cd5ef5dfe21e58ed21939ad4382e5ecd7 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Tue, 17 Mar 2020 21:57:07 +0100 Subject: [PATCH] Fix FC layer for the event dispatcher under Symfony < 4.3 --- phpstan.neon | 8 ++++++ .../ParallelContainerDefinition.php | 17 ++++++------ .../ForwardCompatEventDispatcher.php | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/Lifecycle/ForwardCompatEventDispatcher.php diff --git a/phpstan.neon b/phpstan.neon index 24e1e668..2a5b7d04 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,6 +5,10 @@ parameters: - tests/ ignoreErrors: - "#no value type specified in iterable type Symfony\\\\Component\\\\Process\\\\Process\\.$#" + - + message: "#^Else branch is unreachable because previous condition is always true\\.$#" + count: 1 + path: src/Configuration/DependencyInjection/ParallelContainerDefinition.php - message: "#^Class Symfony\\\\Component\\\\EventDispatcher\\\\Event not found\\.$#" count: 2 @@ -21,6 +25,10 @@ parameters: message: "#^Method Tests\\\\Unit\\\\Configuration\\\\CoverageConfigurationTest\\:\\:getService\\(\\) has no return typehint specified\\.$#" count: 1 path: tests/Unit/Configuration/CoverageConfigurationTest.php + - + message: "#^Method Tests\\\\Unit\\\\Configuration\\\\OutputFileTest\\:\\:testIsEmpty\\(\\) has parameter \\$emptyFile with no typehint specified\\.$#" + count: 1 + path: tests/Unit/Configuration/OutputFileTest.php excludes_analyse: - tests/Stub/ - src/Lifecycle/ForwardCompatEventDispatcher.php diff --git a/src/Configuration/DependencyInjection/ParallelContainerDefinition.php b/src/Configuration/DependencyInjection/ParallelContainerDefinition.php index 2f3aca86..a7a86e35 100644 --- a/src/Configuration/DependencyInjection/ParallelContainerDefinition.php +++ b/src/Configuration/DependencyInjection/ParallelContainerDefinition.php @@ -11,6 +11,7 @@ use Paraunit\File\Cleaner; use Paraunit\File\TempDirectory; use Paraunit\Filter\Filter; +use Paraunit\Lifecycle\ForwardCompatEventDispatcher; use Paraunit\Printer\ConsoleFormatter; use Paraunit\Printer\FailuresPrinter; use Paraunit\Printer\FilesRecapPrinter; @@ -35,7 +36,6 @@ use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface; -use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; class ParallelContainerDefinition { @@ -82,15 +82,16 @@ private function configureConfiguration(ContainerBuilder $container): void private function configureEventDispatcher(ContainerBuilder $container): void { - $eventDispatcher = new Definition(EventDispatcher::class); - if (class_exists(LegacyEventDispatcherProxy::class)) { - $container->setDefinition(LegacyEventDispatcherProxy::class, new Definition(LegacyEventDispatcherProxy::class)); - $eventDispatcher->setDecoratedService(LegacyEventDispatcherProxy::class); + $dispatcher = new Definition(EventDispatcher::class); + $container->setDefinition(SymfonyEventDispatcherInterface::class, $dispatcher); + + if (is_subclass_of(EventDispatcher::class, PsrEventDispatcherInterface::class)) { + $container->setAlias(PsrEventDispatcherInterface::class, SymfonyEventDispatcherInterface::class); + } else { + $container->setDefinition(ForwardCompatEventDispatcher::class, new Definition(ForwardCompatEventDispatcher::class, [new Reference(SymfonyEventDispatcherInterface::class)])); + $container->setAlias(PsrEventDispatcherInterface::class, ForwardCompatEventDispatcher::class); } - $container->setDefinition(SymfonyEventDispatcherInterface::class, $eventDispatcher); - $container->setAlias(PsrEventDispatcherInterface::class, SymfonyEventDispatcherInterface::class); - $container->addCompilerPass( new RegisterListenersPass( SymfonyEventDispatcherInterface::class, diff --git a/src/Lifecycle/ForwardCompatEventDispatcher.php b/src/Lifecycle/ForwardCompatEventDispatcher.php new file mode 100644 index 00000000..0446901d --- /dev/null +++ b/src/Lifecycle/ForwardCompatEventDispatcher.php @@ -0,0 +1,26 @@ +eventDispatcher = $eventDispatcher; + } + + public function dispatch(object $event): object + { + $this->eventDispatcher->dispatch(\get_class($event), $event); + + return $event; + } +}