Skip to content

Commit

Permalink
Fix FC layer for the event dispatcher under Symfony < 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Mar 17, 2020
1 parent 3bdfc41 commit f9c6187
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions src/Lifecycle/ForwardCompatEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Paraunit\Lifecycle;

use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;

class ForwardCompatEventDispatcher implements EventDispatcherInterface
{
/** @var SymfonyEventDispatcherInterface */
private $eventDispatcher;

public function __construct(SymfonyEventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function dispatch(object $event): object
{
$this->eventDispatcher->dispatch(\get_class($event), $event);

return $event;
}
}

0 comments on commit f9c6187

Please sign in to comment.