From 422b926fee561e0a6b8caea9fc9ab713bd4c707f Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Sun, 13 Nov 2022 16:55:20 -0600 Subject: [PATCH] Re-internalize CallableEventDispatcher. --- composer.json | 1 - composer.lock | 68 +--------- config/events.php | 10 +- config/services.php | 10 +- src/CallableEventDispatcher.php | 121 ++++++++++++++++++ src/CallableEventDispatcherInterface.php | 33 +++++ .../Command/MessageQueue/ProcessCommand.php | 2 +- .../Dashboard/NotificationsAction.php | 2 +- src/Plugins.php | 1 - 9 files changed, 166 insertions(+), 82 deletions(-) create mode 100644 src/CallableEventDispatcher.php create mode 100644 src/CallableEventDispatcherInterface.php diff --git a/composer.json b/composer.json index 07ee148b8..c108e9da7 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "azuracast/doctrine-batch-utilities": "dev-main", "azuracast/doctrine-entity-normalizer": "dev-main", "azuracast/nowplaying": "dev-main", - "azuracast/slim-callable-eventdispatcher": "dev-main", "bacon/bacon-qr-code": "^2.0", "beberlei/doctrineextensions": "^1.2", "brick/math": "^0.10", diff --git a/composer.lock b/composer.lock index 12a005910..44a0c7d51 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": "418f33f513ae89bfcf89c29142408445", + "content-hash": "5b92d9eab0dc7f1b2ca03aabeff2858c", "packages": [ { "name": "aws/aws-crt-php", @@ -334,71 +334,6 @@ ], "time": "2022-11-07T00:28:44+00:00" }, - { - "name": "azuracast/slim-callable-eventdispatcher", - "version": "dev-main", - "source": { - "type": "git", - "url": "https://github.com/AzuraCast/slim-callable-eventdispatcher.git", - "reference": "d8875710407a8af8bd66eb8a59d0a5c5e3448bee" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/AzuraCast/slim-callable-eventdispatcher/zipball/d8875710407a8af8bd66eb8a59d0a5c5e3448bee", - "reference": "d8875710407a8af8bd66eb8a59d0a5c5e3448bee", - "shasum": "" - }, - "require": { - "ext-json": "*", - "php": ">=8.0", - "slim/slim": "^4", - "symfony/event-dispatcher": "^5|^6" - }, - "require-dev": { - "php-parallel-lint/php-console-highlighter": "^0.5.0", - "php-parallel-lint/php-parallel-lint": "^1.3", - "phpstan/phpstan": "^0.12", - "roave/security-advisories": "dev-latest" - }, - "default-branch": true, - "type": "library", - "autoload": { - "psr-4": { - "Azura\\SlimCallableEventDispatcher\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Buster 'Silver Eagle' Neece", - "email": "buster@busterneece.com" - } - ], - "description": "Bridge code that integrates Slim's CallableResolver and Symfony's Event Dispatcher to allow resolvable event listeners.", - "homepage": "https://github.com/AzuraCast/slim-callable-eventdispatcher", - "support": { - "issues": "https://github.com/AzuraCast/slim-callable-eventdispatcher/issues", - "source": "https://github.com/AzuraCast/slim-callable-eventdispatcher/tree/main" - }, - "funding": [ - { - "url": "https://github.com/AzuraCast", - "type": "github" - }, - { - "url": "https://opencollective.com/azuracast", - "type": "open_collective" - }, - { - "url": "https://www.patreon.com/AzuraCast", - "type": "patreon" - } - ], - "time": "2022-01-20T07:54:58+00:00" - }, { "name": "bacon/bacon-qr-code", "version": "2.0.7", @@ -13899,7 +13834,6 @@ "azuracast/doctrine-batch-utilities": 20, "azuracast/doctrine-entity-normalizer": 20, "azuracast/nowplaying": 20, - "azuracast/slim-callable-eventdispatcher": 20, "lstrojny/fxmlrpc": 20, "php-di/php-di": 20, "rlanvin/php-ip": 20, diff --git a/config/events.php b/config/events.php index e25eb34e4..12abb12a6 100644 --- a/config/events.php +++ b/config/events.php @@ -1,11 +1,11 @@ addListener( Event\BuildConsoleCommands::class, function (Event\BuildConsoleCommands $event) use ($dispatcher) { @@ -177,14 +177,12 @@ return function (CallableEventDispatcherInterface $dispatcher) { $dispatcher->addCallableListener( Event\Media\GetAlbumArt::class, App\Media\AlbumArtHandler\LastFmAlbumArtHandler::class, - '__invoke', - 10 + priority: 10 ); $dispatcher->addCallableListener( Event\Media\GetAlbumArt::class, App\Media\AlbumArtHandler\MusicBrainzAlbumArtHandler::class, - '__invoke', - -10 + priority: -10 ); $dispatcher->addCallableListener( diff --git a/config/services.php b/config/services.php index 423bc65f1..cadd111d0 100644 --- a/config/services.php +++ b/config/services.php @@ -193,7 +193,7 @@ return [ // Console App\Console\Application::class => static function ( DI\Container $di, - Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface $dispatcher, + App\CallableEventDispatcherInterface $dispatcher, App\Version $version, Environment $environment ) { @@ -218,11 +218,11 @@ return [ }, // Event Dispatcher - Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface::class => static function ( - Slim\App $app, + App\CallableEventDispatcherInterface::class => static function ( + DI\Container $di, App\Plugins $plugins ) { - $dispatcher = new Azura\SlimCallableEventDispatcher\SlimCallableEventDispatcher($app->getCallableResolver()); + $dispatcher = new App\CallableEventDispatcher($di); // Register application default events. if (file_exists(__DIR__ . '/events.php')) { @@ -235,7 +235,7 @@ return [ return $dispatcher; }, Psr\EventDispatcher\EventDispatcherInterface::class => DI\get( - Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface::class + App\CallableEventDispatcherInterface::class ), // Monolog Logger diff --git a/src/CallableEventDispatcher.php b/src/CallableEventDispatcher.php new file mode 100644 index 000000000..94d56814b --- /dev/null +++ b/src/CallableEventDispatcher.php @@ -0,0 +1,121 @@ +addServiceSubscriber($service); + } + return; + } + + foreach ($className::getSubscribedEvents() as $eventName => $params) { + if (is_string($params)) { + $this->addCallableListener( + $eventName, + $className, + $params + ); + } elseif (is_string($params[0])) { + $this->addCallableListener( + $eventName, + $className, + $params[0], + $params[1] ?? 0 + ); + } else { + foreach ($params as $listener) { + $this->addCallableListener( + $eventName, + $className, + $listener[0], + $listener[1] ?? 0 + ); + } + } + } + } + + /** + * @param array|class-string $className + */ + public function removeServiceSubscriber(array|string $className): void + { + if (is_array($className)) { + foreach ($className as $service) { + $this->removeServiceSubscriber($service); + } + return; + } + + foreach ($className::getSubscribedEvents() as $eventName => $params) { + if (is_array($params) && is_array($params[0])) { + foreach ($params as $listener) { + $this->removeCallableListener( + $eventName, + $className, + $listener[0] + ); + } + } else { + $this->removeCallableListener( + $eventName, + $className, + is_string($params) ? $params : $params[0] + ); + } + } + } + + public function addCallableListener( + string $eventName, + string $className, + ?string $method = '__invoke', + int $priority = 0 + ): void { + $this->addListener( + $eventName, + $this->getCallable($className, $method), + $priority + ); + } + + public function removeCallableListener( + string $eventName, + string $className, + ?string $method = '__invoke' + ): void { + $this->removeListener( + $eventName, + $this->getCallable($className, $method) + ); + } + + private function getCallable( + string $className, + ?string $method = '__invoke' + ): \Closure { + return fn(...$args) => $this->di->get($className)->$method(...$args); + } +} diff --git a/src/CallableEventDispatcherInterface.php b/src/CallableEventDispatcherInterface.php new file mode 100644 index 000000000..71c9e060d --- /dev/null +++ b/src/CallableEventDispatcherInterface.php @@ -0,0 +1,33 @@ +