From d1c77ea5797f39c1fabedd738b6daa9baaf0b481 Mon Sep 17 00:00:00 2001
From: Benjamin Franzke <bfr@qbus.de>
Date: Wed, 9 Sep 2020 10:11:35 +0200
Subject: [PATCH] [BUGFIX] Add support for symfony/event-dispatcher-contracts
 v1

With the introduction of symfony/event-dispatcher-contracts
in https://git.typo3.org/Packages/TYPO3.CMS.git/commit/e4a1ae8762ee99712
we forced usage of symfony5 in composer-based installations where
installation with symfony4 was possible before. This is because event
contracts v2 is only compatible with symfony v5, but contracts v1 needs to
be supported for symfony4 compatibility.

Note: although non-composer-based installations used symfony5 bundled
already, composer mode could still use symfony v4 if needed.

We now add a new intermediate composer package that is available in two
different versions, and offers a compatible symfony event dispatcher
adapter for the respective interfaces in symfony v4 and symfony v5 in each
of these version. typo3/cms-core allows to install both versions which
means we can provide a compatible adapter for both, symfony v4 and v5.

This change allows composer-based installations to support both
versions (v5/v4) again, so extensions like EXT:solr (which requires
a package called solarium) to work with the dependency constraints
of TYPO3 Core.

Commands used:

  composer require --no-update --working-dir=typo3/sysext/core \
    "symfony/event-dispatcher-contracts:^1.1 || ^2.0" \
    "typo3/symfony-psr-event-dispatcher-adapter:^1.0 || ^2.0"

  composer require --no-update \
    "symfony/event-dispatcher-contracts:^1.1 || ^2.0" \
    "typo3/symfony-psr-event-dispatcher-adapter:^1.0 || ^2.0"

  composer update --lock

Resolves: #92229
Releases: master, 10.4
Change-Id: I4ff6c6589833cbfea3c4b327a8580fd5fbf45468
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65644
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Simon Gilli <typo3@gilbertsoft.org>
Tested-by: Benjamin Franzke <bfr@qbus.de>
Reviewed-by: Simon Gilli <typo3@gilbertsoft.org>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Reviewed-by: Benjamin Franzke <bfr@qbus.de>
---
 composer.json                                 |  3 +-
 composer.lock                                 | 41 ++++++++++++++++-
 .../Adapter/SymfonyEventDispatcher.php        | 45 -------------------
 typo3/sysext/core/Classes/ServiceProvider.php |  7 +--
 typo3/sysext/core/Configuration/Services.yaml |  2 +-
 typo3/sysext/core/composer.json               |  3 +-
 6 files changed, 49 insertions(+), 52 deletions(-)
 delete mode 100644 typo3/sysext/core/Classes/Adapter/SymfonyEventDispatcher.php

diff --git a/composer.json b/composer.json
index 1a99ec48cc64..3e56a91240a4 100644
--- a/composer.json
+++ b/composer.json
@@ -58,7 +58,7 @@
 		"symfony/config": "^4.4 || ^5.0",
 		"symfony/console": "^4.4 || ^5.0",
 		"symfony/dependency-injection": "^4.4 || ^5.0",
-		"symfony/event-dispatcher-contracts": "^2.0",
+		"symfony/event-dispatcher-contracts": "^1.1 || ^2.0",
 		"symfony/expression-language": "^4.4 || ^5.0",
 		"symfony/finder": "^4.4 || ^5.0",
 		"symfony/http-foundation": "^4.4 || ^5.0",
@@ -76,6 +76,7 @@
 		"typo3/cms-cli": "^2.0",
 		"typo3/cms-composer-installers": "^2.0 || ^3.0",
 		"typo3/phar-stream-wrapper": "^3.1.3",
+		"typo3/symfony-psr-event-dispatcher-adapter": "^1.0 || ^2.0",
 		"typo3fluid/fluid": "^2.6.8 || ^3"
 	},
 	"require-dev": {
diff --git a/composer.lock b/composer.lock
index c627f6e0cf66..b8dbdcb75c1c 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "3f1c87b5cfbc1709b5c8741eff521bac",
+    "content-hash": "e2911ef42adea533e947f376bc311cc3",
     "packages": [
         {
             "name": "cogpowered/finediff",
@@ -3654,6 +3654,45 @@
             ],
             "time": "2019-10-18T11:57:16+00:00"
         },
+        {
+            "name": "typo3/symfony-psr-event-dispatcher-adapter",
+            "version": "v2.0.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/TYPO3/symfony-psr-event-dispatcher-adapter.git",
+                "reference": "6ea13090471966f2fb5de933e5cd5edd4f5d2327"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/TYPO3/symfony-psr-event-dispatcher-adapter/zipball/6ea13090471966f2fb5de933e5cd5edd4f5d2327",
+                "reference": "6ea13090471966f2fb5de933e5cd5edd4f5d2327",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.2",
+                "psr/event-dispatcher": "^1.0",
+                "symfony/event-dispatcher-contracts": "^2.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "TYPO3\\SymfonyPsrEventDispatcherAdapter\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "description": "Adapter to provide compatibility with the Symfony's event dispatcher interface in all versions with the PSR-14 specification.",
+            "homepage": "https://typo3.org/",
+            "keywords": [
+                "adapter",
+                "events",
+                "psr",
+                "psr-14"
+            ],
+            "time": "2020-09-09T20:23:21+00:00"
+        },
         {
             "name": "typo3fluid/fluid",
             "version": "2.6.9",
diff --git a/typo3/sysext/core/Classes/Adapter/SymfonyEventDispatcher.php b/typo3/sysext/core/Classes/Adapter/SymfonyEventDispatcher.php
deleted file mode 100644
index babe92a58194..000000000000
--- a/typo3/sysext/core/Classes/Adapter/SymfonyEventDispatcher.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-namespace TYPO3\CMS\Core\Adapter;
-
-use Psr\EventDispatcher\EventDispatcherInterface;
-use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
-
-/**
- * @internal
- */
-class SymfonyEventDispatcher implements SymfonyEventDispatcherInterface
-{
-    /**
-     * @var EventDispatcherInterface
-     */
-    private $eventDispatcher;
-
-    public function __construct(EventDispatcherInterface $eventDispatcher)
-    {
-        $this->eventDispatcher = $eventDispatcher;
-    }
-
-    /**
-     * @inheritdoc
-     */
-    public function dispatch(object $event, string $eventName = null): object
-    {
-        return $this->eventDispatcher->dispatch($event);
-    }
-}
diff --git a/typo3/sysext/core/Classes/ServiceProvider.php b/typo3/sysext/core/Classes/ServiceProvider.php
index 338dbf3e9f9e..b2083eb43f3c 100644
--- a/typo3/sysext/core/Classes/ServiceProvider.php
+++ b/typo3/sysext/core/Classes/ServiceProvider.php
@@ -23,6 +23,7 @@ use Psr\EventDispatcher\EventDispatcherInterface;
 use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
 use TYPO3\CMS\Core\Core\Environment;
 use TYPO3\CMS\Core\Package\AbstractServiceProvider;
+use TYPO3\SymfonyPsrEventDispatcherAdapter\EventDispatcherAdapter as SymfonyEventDispatcher;
 
 /**
  * @internal
@@ -37,7 +38,7 @@ class ServiceProvider extends AbstractServiceProvider
     public function getFactories(): array
     {
         return [
-            Adapter\SymfonyEventDispatcher::class => [ static::class, 'getSymfonyEventDispatcher' ],
+            SymfonyEventDispatcher::class => [ static::class, 'getSymfonyEventDispatcher' ],
             Cache\CacheManager::class => [ static::class, 'getCacheManager' ],
             Charset\CharsetConverter::class => [ static::class, 'getCharsetConverter' ],
             Configuration\SiteConfiguration::class => [ static::class, 'getSiteConfiguration' ],
@@ -85,7 +86,7 @@ class ServiceProvider extends AbstractServiceProvider
 
     public static function getSymfonyEventDispatcher(ContainerInterface $container): SymfonyEventDispatcherInterface
     {
-        return self::new($container, Adapter\SymfonyEventDispatcher::class, [
+        return self::new($container, SymfonyEventDispatcher::class, [
             $container->get(EventDispatcherInterface::class)
         ]);
     }
@@ -213,7 +214,7 @@ class ServiceProvider extends AbstractServiceProvider
     public static function getMailTransportFactory(ContainerInterface $container): Mail\TransportFactory
     {
         return self::new($container, Mail\TransportFactory::class, [
-            $container->get(Adapter\SymfonyEventDispatcher::class),
+            $container->get(SymfonyEventDispatcher::class),
             $container->get(Log\LogManager::class)
         ]);
     }
diff --git a/typo3/sysext/core/Configuration/Services.yaml b/typo3/sysext/core/Configuration/Services.yaml
index 4a207e0c4521..cdfde9acf42c 100644
--- a/typo3/sysext/core/Configuration/Services.yaml
+++ b/typo3/sysext/core/Configuration/Services.yaml
@@ -212,7 +212,7 @@ services:
     alias: GuzzleHttp\Client
     public: true
   Symfony\Contracts\EventDispatcher\EventDispatcherInterface:
-    alias: TYPO3\CMS\Core\Adapter\SymfonyEventDispatcher
+    alias: TYPO3\SymfonyPsrEventDispatcherAdapter\EventDispatcherAdapter
     public: true
 
   # External dependencies
diff --git a/typo3/sysext/core/composer.json b/typo3/sysext/core/composer.json
index 2c0a382650e9..323d524db6ca 100644
--- a/typo3/sysext/core/composer.json
+++ b/typo3/sysext/core/composer.json
@@ -45,7 +45,7 @@
 		"symfony/config": "^4.4 || ^5.0",
 		"symfony/console": "^4.4 || ^5.0",
 		"symfony/dependency-injection": "^4.4 || ^5.0",
-		"symfony/event-dispatcher-contracts": "^2.0",
+		"symfony/event-dispatcher-contracts": "^1.1 || ^2.0",
 		"symfony/expression-language": "^4.4 || ^5.0",
 		"symfony/finder": "^4.4 || ^5.0",
 		"symfony/http-foundation": "^4.4 || ^5.0",
@@ -60,6 +60,7 @@
 		"typo3/cms-cli": "^2.0",
 		"typo3/cms-composer-installers": "^2.0 || ^3.0",
 		"typo3/phar-stream-wrapper": "^3.1.3",
+		"typo3/symfony-psr-event-dispatcher-adapter": "^1.0 || ^2.0",
 		"typo3fluid/fluid": "^2.6.8 || ^3"
 	},
 	"require-dev": {
-- 
GitLab