Re-internalize CallableEventDispatcher.

This commit is contained in:
Buster Neece 2022-11-13 16:55:20 -06:00
parent c69ed7570e
commit 422b926fee
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
9 changed files with 166 additions and 82 deletions

View File

@ -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",

68
composer.lock generated
View File

@ -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,

View File

@ -1,11 +1,11 @@
<?php
use App\CallableEventDispatcherInterface;
use App\Environment;
use App\Event;
use App\Middleware;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
return function (CallableEventDispatcherInterface $dispatcher) {
return static function (CallableEventDispatcherInterface $dispatcher) {
$dispatcher->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(

View File

@ -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

View File

@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace App;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use function is_array;
use function is_string;
final class CallableEventDispatcher extends EventDispatcher implements CallableEventDispatcherInterface
{
public function __construct(
private readonly ContainerInterface $di
) {
parent::__construct();
}
/**
* @param array|class-string $className
*/
public function addServiceSubscriber(array|string $className): void
{
if (is_array($className)) {
foreach ($className as $service) {
$this->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);
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
interface CallableEventDispatcherInterface extends EventDispatcherInterface
{
/**
* @param array|class-string $className
*/
public function addServiceSubscriber(array|string $className): void;
/**
* @param array|class-string $className
*/
public function removeServiceSubscriber(array|string $className): void;
public function addCallableListener(
string $eventName,
string $className,
?string $method = '__invoke',
int $priority = 0
): void;
public function removeCallableListener(
string $eventName,
string $className,
?string $method = '__invoke'
): void;
}

View File

@ -4,13 +4,13 @@ declare(strict_types=1);
namespace App\Console\Command\MessageQueue;
use App\CallableEventDispatcherInterface;
use App\Console\Command\CommandAbstract;
use App\Doctrine\Messenger\ClearEntityManagerSubscriber;
use App\Environment;
use App\MessageQueue\LogWorkerExceptionSubscriber;
use App\MessageQueue\QueueManagerInterface;
use App\MessageQueue\ResetArrayCacheMiddleware;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace App\Controller\Api\Frontend\Dashboard;
use App\CallableEventDispatcherInterface;
use App\Event;
use App\Http\Response;
use App\Http\ServerRequest;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
final class NotificationsAction

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\Finder\Finder;