From d086131630556a080d8947bac5792e7d16015e1d Mon Sep 17 00:00:00 2001
From: Alejandro Celaya <alejandro@alejandrocelaya.com>
Date: Fri, 19 Jul 2019 19:54:39 +0200
Subject: [PATCH] Moved all event-dispatching stuff to its own module

---
 composer.json                                 | 10 ++++---
 config/autoload/event_dispatcher.global.php   | 20 --------------
 config/config.php                             |  4 +--
 module/Common/functions/functions.php         |  7 -----
 .../config/event_dispatcher.config.php        | 27 +++++++++++++++++++
 .../config/task_runner.config.php             |  6 ++---
 .../EventDispatcher/functions/functions.php   | 11 ++++++++
 .../src/Async}/Task.php                       |  2 +-
 .../src/Async}/TaskRunner.php                 |  2 +-
 .../src/Async}/TaskRunnerDelegator.php        |  2 +-
 .../src/Async}/TaskRunnerFactory.php          |  2 +-
 module/EventDispatcher/src/ConfigProvider.php | 15 +++++++++++
 .../src/Listener}/AsyncEventListener.php      |  3 ++-
 .../src/Listener}/ListenerProviderFactory.php |  4 +--
 .../Listener}/ListenerProviderFactoryTest.php |  4 +--
 phpunit.xml.dist                              |  4 +--
 16 files changed, 77 insertions(+), 46 deletions(-)
 delete mode 100644 config/autoload/event_dispatcher.global.php
 create mode 100644 module/EventDispatcher/config/event_dispatcher.config.php
 rename module/{Common => EventDispatcher}/config/task_runner.config.php (56%)
 create mode 100644 module/EventDispatcher/functions/functions.php
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Async}/Task.php (93%)
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Async}/TaskRunner.php (96%)
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Async}/TaskRunnerDelegator.php (94%)
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Async}/TaskRunnerFactory.php (89%)
 create mode 100644 module/EventDispatcher/src/ConfigProvider.php
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Listener}/AsyncEventListener.php (84%)
 rename module/{Common/src/EventDispatcher => EventDispatcher/src/Listener}/ListenerProviderFactory.php (93%)
 rename module/{Common/test/EventDispatcher => EventDispatcher/test/Listener}/ListenerProviderFactoryTest.php (95%)

diff --git a/composer.json b/composer.json
index ba72716d..e6e20989 100644
--- a/composer.json
+++ b/composer.json
@@ -53,6 +53,7 @@
     "require-dev": {
         "devster/ubench": "^2.0",
         "doctrine/data-fixtures": "^1.3",
+        "eaglewu/swoole-ide-helper": "dev-master",
         "filp/whoops": "^2.0",
         "infection/infection": "^0.12.2",
         "phpstan/phpstan": "^0.11.2",
@@ -70,10 +71,12 @@
             "Shlinkio\\Shlink\\CLI\\": "module/CLI/src",
             "Shlinkio\\Shlink\\Rest\\": "module/Rest/src",
             "Shlinkio\\Shlink\\Core\\": "module/Core/src",
-            "Shlinkio\\Shlink\\Common\\": "module/Common/src"
+            "Shlinkio\\Shlink\\Common\\": "module/Common/src",
+            "Shlinkio\\Shlink\\EventDispatcher\\": "module/EventDispatcher/src"
         },
         "files": [
-            "module/Common/functions/functions.php"
+            "module/Common/functions/functions.php",
+            "module/EventDispatcher/functions/functions.php"
         ]
     },
     "autoload-dev": {
@@ -88,7 +91,8 @@
             "ShlinkioTest\\Shlink\\Common\\": [
                 "module/Common/test",
                 "module/Common/test-db"
-            ]
+            ],
+            "ShlinkioTest\\Shlink\\EventDispatcher\\": "module/EventDispatcher/test"
         }
     },
     "scripts": {
diff --git a/config/autoload/event_dispatcher.global.php b/config/autoload/event_dispatcher.global.php
deleted file mode 100644
index 53964152..00000000
--- a/config/autoload/event_dispatcher.global.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace Shlinkio\Shlink;
-
-use Phly\EventDispatcher as Phly;
-use Psr\EventDispatcher as Psr;
-
-return [
-
-    'dependencies' => [
-        'factories' => [
-            Psr\ListenerProviderInterface::class => Common\EventDispatcher\ListenerProviderFactory::class,
-        ],
-        'aliases' => [
-            Psr\EventDispatcherInterface::class => Phly\EventDispatcher::class,
-        ],
-    ],
-
-];
diff --git a/config/config.php b/config/config.php
index e10d3c1e..f09bb9f6 100644
--- a/config/config.php
+++ b/config/config.php
@@ -4,9 +4,9 @@ declare(strict_types=1);
 namespace Shlinkio\Shlink;
 
 use Acelaya\ExpressiveErrorHandler;
-use Phly\EventDispatcher;
 use Zend\ConfigAggregator;
 use Zend\Expressive;
+
 use function Shlinkio\Shlink\Common\env;
 
 return (new ConfigAggregator\ConfigAggregator([
@@ -16,11 +16,11 @@ return (new ConfigAggregator\ConfigAggregator([
     Expressive\Plates\ConfigProvider::class,
     Expressive\Swoole\ConfigProvider::class,
     ExpressiveErrorHandler\ConfigProvider::class,
-    EventDispatcher\ConfigProvider::class,
     Common\ConfigProvider::class,
     Core\ConfigProvider::class,
     CLI\ConfigProvider::class,
     Rest\ConfigProvider::class,
+    EventDispatcher\ConfigProvider::class,
     new ConfigAggregator\PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
     new ConfigAggregator\ZendConfigProvider('config/params/{generated_config.php,*.config.{php,json}}'),
     env('APP_ENV') === 'test'
diff --git a/module/Common/functions/functions.php b/module/Common/functions/functions.php
index 17856af2..485df607 100644
--- a/module/Common/functions/functions.php
+++ b/module/Common/functions/functions.php
@@ -3,8 +3,6 @@ declare(strict_types=1);
 
 namespace Shlinkio\Shlink\Common;
 
-use Swoole\Http\Server as HttpServer;
-
 use const JSON_ERROR_NONE;
 
 use function getenv;
@@ -61,8 +59,3 @@ function json_decode(string $json, int $depth = 512, int $options = 0): array
 
     return $data;
 }
-
-function asyncListener(HttpServer $server, string $regularListenerName): EventDispatcher\AsyncEventListener
-{
-    return new EventDispatcher\AsyncEventListener($server, $regularListenerName);
-}
diff --git a/module/EventDispatcher/config/event_dispatcher.config.php b/module/EventDispatcher/config/event_dispatcher.config.php
new file mode 100644
index 00000000..9930d9e3
--- /dev/null
+++ b/module/EventDispatcher/config/event_dispatcher.config.php
@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace Shlinkio\Shlink\EventDispatcher;
+
+use Phly\EventDispatcher as Phly;
+use Psr\EventDispatcher as Psr;
+use Shlinkio\Shlink\Common;
+
+return [
+
+    'events' => [
+        'regular' => [],
+        'async' => [],
+    ],
+
+    'dependencies' => [
+        'factories' => [
+            Phly\EventDispatcher::class => Phly\EventDispatcherFactory::class,
+            Psr\ListenerProviderInterface::class => Listener\ListenerProviderFactory::class,
+        ],
+        'aliases' => [
+            Psr\EventDispatcherInterface::class => Phly\EventDispatcher::class,
+        ],
+    ],
+
+];
diff --git a/module/Common/config/task_runner.config.php b/module/EventDispatcher/config/task_runner.config.php
similarity index 56%
rename from module/Common/config/task_runner.config.php
rename to module/EventDispatcher/config/task_runner.config.php
index 8f0e688e..a0a23db5 100644
--- a/module/Common/config/task_runner.config.php
+++ b/module/EventDispatcher/config/task_runner.config.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common;
+namespace Shlinkio\Shlink\EventDispatcher;
 
 use Swoole\Http\Server as HttpServer;
 
@@ -9,11 +9,11 @@ return [
 
     'dependencies' => [
         'factories' => [
-            EventDispatcher\TaskRunner::class => EventDispatcher\TaskRunnerFactory::class,
+            Async\TaskRunner::class => Async\TaskRunnerFactory::class,
         ],
         'delegators' => [
             HttpServer::class => [
-                EventDispatcher\TaskRunnerDelegator::class,
+                Async\TaskRunnerDelegator::class,
             ],
         ],
     ],
diff --git a/module/EventDispatcher/functions/functions.php b/module/EventDispatcher/functions/functions.php
new file mode 100644
index 00000000..a1c93231
--- /dev/null
+++ b/module/EventDispatcher/functions/functions.php
@@ -0,0 +1,11 @@
+<?php
+declare(strict_types=1);
+
+namespace Shlinkio\Shlink\EventDispatcher;
+
+use Swoole\Http\Server as HttpServer;
+
+function asyncListener(HttpServer $server, string $regularListenerName): Listener\AsyncEventListener
+{
+    return new Listener\AsyncEventListener($server, $regularListenerName);
+}
diff --git a/module/Common/src/EventDispatcher/Task.php b/module/EventDispatcher/src/Async/Task.php
similarity index 93%
rename from module/Common/src/EventDispatcher/Task.php
rename to module/EventDispatcher/src/Async/Task.php
index f12abb55..7a44faa2 100644
--- a/module/Common/src/EventDispatcher/Task.php
+++ b/module/EventDispatcher/src/Async/Task.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Async;
 
 use Psr\Container\ContainerInterface;
 
diff --git a/module/Common/src/EventDispatcher/TaskRunner.php b/module/EventDispatcher/src/Async/TaskRunner.php
similarity index 96%
rename from module/Common/src/EventDispatcher/TaskRunner.php
rename to module/EventDispatcher/src/Async/TaskRunner.php
index 7329307e..32dd7e80 100644
--- a/module/Common/src/EventDispatcher/TaskRunner.php
+++ b/module/EventDispatcher/src/Async/TaskRunner.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Async;
 
 use Psr\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
diff --git a/module/Common/src/EventDispatcher/TaskRunnerDelegator.php b/module/EventDispatcher/src/Async/TaskRunnerDelegator.php
similarity index 94%
rename from module/Common/src/EventDispatcher/TaskRunnerDelegator.php
rename to module/EventDispatcher/src/Async/TaskRunnerDelegator.php
index bb7f138c..887313df 100644
--- a/module/Common/src/EventDispatcher/TaskRunnerDelegator.php
+++ b/module/EventDispatcher/src/Async/TaskRunnerDelegator.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Async;
 
 use Interop\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
diff --git a/module/Common/src/EventDispatcher/TaskRunnerFactory.php b/module/EventDispatcher/src/Async/TaskRunnerFactory.php
similarity index 89%
rename from module/Common/src/EventDispatcher/TaskRunnerFactory.php
rename to module/EventDispatcher/src/Async/TaskRunnerFactory.php
index b24eebdb..b50ee791 100644
--- a/module/Common/src/EventDispatcher/TaskRunnerFactory.php
+++ b/module/EventDispatcher/src/Async/TaskRunnerFactory.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Async;
 
 use Interop\Container\ContainerInterface;
 use Psr\Log\LoggerInterface;
diff --git a/module/EventDispatcher/src/ConfigProvider.php b/module/EventDispatcher/src/ConfigProvider.php
new file mode 100644
index 00000000..70b6ab2e
--- /dev/null
+++ b/module/EventDispatcher/src/ConfigProvider.php
@@ -0,0 +1,15 @@
+<?php
+declare(strict_types=1);
+
+namespace Shlinkio\Shlink\EventDispatcher;
+
+use Zend\Config\Factory;
+use Zend\Stdlib\Glob;
+
+class ConfigProvider
+{
+    public function __invoke()
+    {
+        return Factory::fromFiles(Glob::glob(__DIR__ . '/../config/{,*.}config.php', Glob::GLOB_BRACE));
+    }
+}
diff --git a/module/Common/src/EventDispatcher/AsyncEventListener.php b/module/EventDispatcher/src/Listener/AsyncEventListener.php
similarity index 84%
rename from module/Common/src/EventDispatcher/AsyncEventListener.php
rename to module/EventDispatcher/src/Listener/AsyncEventListener.php
index 0b65d083..28132ae8 100644
--- a/module/Common/src/EventDispatcher/AsyncEventListener.php
+++ b/module/EventDispatcher/src/Listener/AsyncEventListener.php
@@ -1,8 +1,9 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Listener;
 
+use Shlinkio\Shlink\EventDispatcher\Async\Task;
 use Swoole\Http\Server as HttpServer;
 
 class AsyncEventListener
diff --git a/module/Common/src/EventDispatcher/ListenerProviderFactory.php b/module/EventDispatcher/src/Listener/ListenerProviderFactory.php
similarity index 93%
rename from module/Common/src/EventDispatcher/ListenerProviderFactory.php
rename to module/EventDispatcher/src/Listener/ListenerProviderFactory.php
index 3d9de084..7ad48508 100644
--- a/module/Common/src/EventDispatcher/ListenerProviderFactory.php
+++ b/module/EventDispatcher/src/Listener/ListenerProviderFactory.php
@@ -1,7 +1,7 @@
 <?php
 declare(strict_types=1);
 
-namespace Shlinkio\Shlink\Common\EventDispatcher;
+namespace Shlinkio\Shlink\EventDispatcher\Listener;
 
 use Interop\Container\ContainerInterface;
 use Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider;
@@ -9,7 +9,7 @@ use Swoole\Http\Server as HttpServer;
 use Zend\ServiceManager\Factory\FactoryInterface;
 
 use function Phly\EventDispatcher\lazyListener;
-use function Shlinkio\Shlink\Common\asyncListener;
+use function Shlinkio\Shlink\EventDispatcher\asyncListener;
 
 class ListenerProviderFactory implements FactoryInterface
 {
diff --git a/module/Common/test/EventDispatcher/ListenerProviderFactoryTest.php b/module/EventDispatcher/test/Listener/ListenerProviderFactoryTest.php
similarity index 95%
rename from module/Common/test/EventDispatcher/ListenerProviderFactoryTest.php
rename to module/EventDispatcher/test/Listener/ListenerProviderFactoryTest.php
index 9834c4cd..165bac37 100644
--- a/module/Common/test/EventDispatcher/ListenerProviderFactoryTest.php
+++ b/module/EventDispatcher/test/Listener/ListenerProviderFactoryTest.php
@@ -1,13 +1,13 @@
 <?php
 declare(strict_types=1);
 
-namespace ShlinkioTest\Shlink\Common\EventDispatcher;
+namespace ShlinkioTest\Shlink\EventDispatcher\Listener;
 
 use Interop\Container\ContainerInterface;
 use Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider;
 use PHPUnit\Framework\TestCase;
 use ReflectionObject;
-use Shlinkio\Shlink\Common\EventDispatcher\ListenerProviderFactory;
+use Shlinkio\Shlink\EventDispatcher\Listener\ListenerProviderFactory;
 
 use function Phly\EventDispatcher\lazyListener;
 
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 8220a06e..259a51ae 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -18,8 +18,8 @@
         <testsuite name="CLI">
             <directory>./module/CLI/test</directory>
         </testsuite>
-        <testsuite name="Installer">
-            <directory>./module/Installer/test</directory>
+        <testsuite name="EventDispatcher">
+            <directory>./module/EventDispatcher/test</directory>
         </testsuite>
     </testsuites>