Use shared CallableEventManager library.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-08-04 21:24:27 -05:00
parent 8718bdd794
commit b5fc9ad601
No known key found for this signature in database
GPG Key ID: 6D9E12FF03411F4E
25 changed files with 119 additions and 210 deletions

View File

@ -20,6 +20,7 @@
"azuracast/azuraforms": "dev-main",
"azuracast/flysystem-v2-extensions": "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.9",

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": "aaed255d6b73758458225fdff1a226fb",
"content-hash": "c268283686625c65ab291d1d1628c62d",
"packages": [
{
"name": "aws/aws-sdk-php",
@ -318,6 +318,71 @@
],
"time": "2021-06-01T06:46:10+00:00"
},
{
"name": "azuracast/slim-callable-eventdispatcher",
"version": "dev-main",
"source": {
"type": "git",
"url": "https://github.com/AzuraCast/slim-callable-eventdispatcher.git",
"reference": "0c1a17c81573fbdbcb65b04aea5ddf1743889e91"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/AzuraCast/slim-callable-eventdispatcher/zipball/0c1a17c81573fbdbcb65b04aea5ddf1743889e91",
"reference": "0c1a17c81573fbdbcb65b04aea5ddf1743889e91",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=8.0",
"slim/slim": "^4",
"symfony/event-dispatcher": "^5"
},
"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": "2021-08-05T00:22:23+00:00"
},
{
"name": "bacon/bacon-qr-code",
"version": "2.0.4",
@ -13600,6 +13665,7 @@
"azuracast/azuraforms": 20,
"azuracast/flysystem-v2-extensions": 20,
"azuracast/nowplaying": 20,
"azuracast/slim-callable-eventdispatcher": 20,
"james-heinrich/getid3": 20,
"lstrojny/fxmlrpc": 20,
"rlanvin/php-ip": 20,

View File

@ -4,8 +4,9 @@ use App\Console\Command;
use App\Environment;
use App\Event;
use App\Middleware;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
return function (App\EventDispatcher $dispatcher) {
return function (CallableEventDispatcherInterface $dispatcher) {
$dispatcher->addListener(
Event\BuildConsoleCommands::class,
function (Event\BuildConsoleCommands $event) use ($dispatcher) {

View File

@ -70,7 +70,7 @@ return [
App\Doctrine\Event\StationRequiresRestart $eventRequiresRestart,
App\Doctrine\Event\AuditLog $eventAuditLog,
App\Doctrine\Event\SetExplicitChangeTracking $eventChangeTracking,
App\EventDispatcher $dispatcher
Psr\EventDispatcher\EventDispatcherInterface $dispatcher
) {
$connectionOptions = array_merge(
$environment->getDatabaseSettings(),
@ -244,7 +244,7 @@ return [
// Console
App\Console\Application::class => static function (
DI\Container $di,
App\EventDispatcher $dispatcher,
Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface $dispatcher,
App\Version $version,
Environment $environment
) {
@ -263,8 +263,11 @@ return [
},
// Event Dispatcher
App\EventDispatcher::class => static function (Slim\App $app, App\Plugins $plugins) {
$dispatcher = new App\EventDispatcher($app->getCallableResolver());
Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface::class => static function (
Slim\App $app,
App\Plugins $plugins
) {
$dispatcher = new Azura\SlimCallableEventDispatcher\SlimCallableEventDispatcher($app->getCallableResolver());
// Register application default events.
if (file_exists(__DIR__ . '/events.php')) {
@ -276,7 +279,9 @@ return [
return $dispatcher;
},
Psr\EventDispatcher\EventDispatcherInterface::class => DI\get(App\EventDispatcher::class),
Psr\EventDispatcher\EventDispatcherInterface::class => DI\get(
Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface::class
),
// Monolog Logger
Monolog\Logger::class => static function (Environment $environment) {
@ -425,7 +430,7 @@ return [
// Mail functionality
Symfony\Component\Mailer\Transport\TransportInterface::class => static function (
App\Entity\Repository\SettingsRepository $settingsRepo,
App\EventDispatcher $eventDispatcher,
Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface $eventDispatcher,
Monolog\Logger $logger
) {
$settings = $settingsRepo->readSettings();
@ -472,7 +477,7 @@ return [
Symfony\Component\Mailer\Mailer::class => static function (
Symfony\Component\Mailer\Transport\TransportInterface $transport,
Symfony\Component\Messenger\MessageBus $messageBus,
App\EventDispatcher $eventDispatcher
Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface $eventDispatcher
) {
return new Symfony\Component\Mailer\Mailer(
$transport,

View File

@ -6,6 +6,8 @@ parameters:
paths:
- src
- bin
- web
bootstrapFiles:
- ./util/phpstan.php

View File

@ -7,6 +7,7 @@ namespace App;
use App\Entity;
use App\Http\ServerRequest;
use App\Traits\RequestAwareTrait;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use function in_array;
@ -46,7 +47,7 @@ class Acl
public function __construct(
protected Entity\Repository\RolePermissionRepository $permissionRepo,
protected EventDispatcher $dispatcher
protected EventDispatcherInterface $dispatcher
) {
$this->reload();
}

View File

@ -17,6 +17,7 @@ use Invoker\ParameterResolver\Container\TypeHintContainerResolver;
use Invoker\ParameterResolver\DefaultValueResolver;
use Invoker\ParameterResolver\ResolverChain;
use Monolog\Registry;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Slim\App;
use Slim\Factory\ServerRequestCreatorFactory;
@ -98,7 +99,7 @@ class AppFactory
$routeCollector->setCacheFile($environment->getTempDirectory() . '/app_routes.cache.php');
}
$eventDispatcher = $container->get(EventDispatcher::class);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new Event\BuildRoutes($app));
return $app;

View File

@ -7,10 +7,10 @@ namespace App\Console\Command\MessageQueue;
use App\Console\Command\CommandAbstract;
use App\Doctrine\Messenger\ClearEntityManagerSubscriber;
use App\Environment;
use App\EventDispatcher;
use App\MessageQueue\LogWorkerExceptionSubscriber;
use App\MessageQueue\QueueManagerInterface;
use App\MessageQueue\ResetArrayCacheMiddleware;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\EventListener\StopWorkerOnTimeLimitListener;
use Symfony\Component\Messenger\MessageBus;
@ -21,7 +21,7 @@ class ProcessCommand extends CommandAbstract
{
public function __invoke(
MessageBus $messageBus,
EventDispatcher $eventDispatcher,
CallableEventDispatcherInterface $eventDispatcher,
QueueManagerInterface $queueManager,
LoggerInterface $logger,
Environment $environment,

View File

@ -5,9 +5,9 @@ declare(strict_types=1);
namespace App\Controller\Api\Frontend\Dashboard;
use App\Event;
use App\EventDispatcher;
use App\Http\Response;
use App\Http\ServerRequest;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
class NotificationsAction
@ -15,7 +15,7 @@ class NotificationsAction
public function __invoke(
ServerRequest $request,
Response $response,
EventDispatcher $eventDispatcher
CallableEventDispatcherInterface $eventDispatcher
): ResponseInterface {
$event = new Event\GetNotifications($request);
$eventDispatcher->dispatch($event);

View File

@ -6,7 +6,6 @@ namespace App\Controller\Frontend;
use App\Acl;
use App\Entity;
use App\EventDispatcher;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Service\Avatar;
@ -19,7 +18,6 @@ class DashboardAction
ServerRequest $request,
Response $response,
EntityManagerInterface $em,
EventDispatcher $eventDispatcher,
Avatar $avatar,
Entity\ApiGenerator\NowPlayingApiGenerator $npApiGenerator,
Entity\Repository\SettingsRepository $settingsRepo

View File

@ -1,48 +0,0 @@
<?php
/**
* Deferred Callable
* Used from:
* Slim Framework (https://slimframework.com)
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace App;
use Slim\Interfaces\CallableResolverInterface;
class DeferredCallable
{
/**
* @var callable|string
*/
protected $callable;
protected ?CallableResolverInterface $callableResolver;
/**
* @param callable|string $callable
* @param CallableResolverInterface|null $resolver
*/
public function __construct(callable|string $callable, ?CallableResolverInterface $resolver = null)
{
$this->callable = $callable;
$this->callableResolver = $resolver;
}
/**
* @param mixed ...$args
*/
public function __invoke(...$args): mixed
{
/** @var callable $callable */
$callable = $this->callable;
if ($this->callableResolver) {
$callable = $this->callableResolver->resolve($callable);
}
return $callable(...$args);
}
}

View File

@ -1,120 +0,0 @@
<?php
declare(strict_types=1);
namespace App;
use Slim\Interfaces\CallableResolverInterface;
use function is_array;
use function is_string;
class EventDispatcher extends \Symfony\Component\EventDispatcher\EventDispatcher
{
public function __construct(
protected CallableResolverInterface $callableResolver
) {
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)
);
}
protected function getCallable(
string $className,
?string $method = '__invoke'
): DeferredCallable {
return new DeferredCallable($className . ':' . $method, $this->callableResolver);
}
}

View File

@ -8,11 +8,11 @@ use App\Entity;
use App\Environment;
use App\Event\Media\ReadMetadata;
use App\Event\Media\WriteMetadata;
use App\EventDispatcher;
use App\Exception\CannotProcessMediaException;
use App\Utilities\File;
use App\Utilities\Json;
use GuzzleHttp\Client;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
@ -20,7 +20,7 @@ use Symfony\Component\Process\Process;
class MetadataManager implements EventSubscriberInterface
{
public function __construct(
protected EventDispatcher $eventDispatcher,
protected EventDispatcherInterface $eventDispatcher,
protected Client $httpClient,
protected Environment $environment
) {

View File

@ -8,10 +8,10 @@ namespace App\Media;
use App\Entity;
use App\Event\Media\GetAlbumArt;
use App\EventDispatcher;
use App\Version;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Throwable;
@ -24,7 +24,7 @@ class RemoteAlbumArt
protected LoggerInterface $logger,
protected CacheInterface $cache,
protected Entity\Repository\SettingsRepository $settingsRepo,
protected EventDispatcher $eventDispatcher,
protected EventDispatcherInterface $eventDispatcher,
protected Client $httpClient
) {
}

View File

@ -6,8 +6,8 @@ namespace App\Middleware\Module;
use App\Entity\Repository\SettingsRepository;
use App\Event;
use App\EventDispatcher;
use App\Http\ServerRequest;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Interfaces\RouteInterface;
@ -19,7 +19,7 @@ use Slim\Routing\RouteContext;
class Admin
{
public function __construct(
protected EventDispatcher $dispatcher,
protected EventDispatcherInterface $dispatcher,
protected SettingsRepository $settingsRepo
) {
}

View File

@ -6,8 +6,8 @@ namespace App\Middleware\Module;
use App\Entity\Repository\SettingsRepository;
use App\Event;
use App\EventDispatcher;
use App\Http\ServerRequest;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Interfaces\RouteInterface;
@ -19,7 +19,7 @@ use Slim\Routing\RouteContext;
class Stations
{
public function __construct(
protected EventDispatcher $dispatcher,
protected EventDispatcherInterface $dispatcher,
protected SettingsRepository $settingsRepo
) {
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App;
use Azura\SlimCallableEventDispatcher\CallableEventDispatcherInterface;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Symfony\Component\Finder\Finder;
@ -68,9 +69,9 @@ class Plugins
/**
* Register custom events that the plugin overrides with the Event Dispatcher.
*
* @param EventDispatcher $dispatcher
* @param CallableEventDispatcherInterface $dispatcher
*/
public function registerEvents(EventDispatcher $dispatcher): void
public function registerEvents(CallableEventDispatcherInterface $dispatcher): void
{
foreach ($this->plugins as $plugin) {
$plugin_path = $plugin['path'];

View File

@ -6,12 +6,12 @@ namespace App\Radio;
use App\Entity;
use App\Environment;
use App\EventDispatcher;
use App\Exception\Supervisor\AlreadyRunningException;
use App\Exception\Supervisor\BadNameException;
use App\Exception\Supervisor\NotRunningException;
use App\Exception\SupervisorException;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Supervisor\Exception\Fault;
use Supervisor\Exception\SupervisorException as SupervisorLibException;
@ -24,7 +24,7 @@ abstract class AbstractAdapter
protected Environment $environment,
protected EntityManagerInterface $em,
protected Supervisor $supervisor,
protected EventDispatcher $dispatcher,
protected EventDispatcherInterface $dispatcher,
protected LoggerInterface $logger
) {
}

View File

@ -9,13 +9,13 @@ use App\Entity;
use App\Environment;
use App\Event\Radio\AnnotateNextSong;
use App\Event\Radio\BuildQueue;
use App\EventDispatcher;
use App\LockFactory;
use App\Radio\AutoDJ\Scheduler;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use Psr\EventDispatcher\EventDispatcherInterface;
class AutoDJ
{
@ -23,7 +23,7 @@ class AutoDJ
protected ReloadableEntityManagerInterface $em,
protected Entity\Repository\SongHistoryRepository $songHistoryRepo,
protected Entity\Repository\StationQueueRepository $queueRepo,
protected EventDispatcher $dispatcher,
protected EventDispatcherInterface $dispatcher,
protected Logger $logger,
protected Scheduler $scheduler,
protected Environment $environment,

View File

@ -7,9 +7,9 @@ namespace App\Radio\Backend;
use App\Entity;
use App\Environment;
use App\Event\Radio\WriteLiquidsoapConfiguration;
use App\EventDispatcher;
use App\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use Supervisor\Supervisor;
@ -21,7 +21,7 @@ class Liquidsoap extends AbstractBackend
Environment $environment,
EntityManagerInterface $em,
Supervisor $supervisor,
EventDispatcher $dispatcher,
EventDispatcherInterface $dispatcher,
LoggerInterface $logger
) {
parent::__construct($environment, $em, $supervisor, $dispatcher, $logger);

View File

@ -6,7 +6,6 @@ namespace App\Radio\Frontend;
use App\Entity;
use App\Environment;
use App\EventDispatcher;
use App\Http\Router;
use App\Radio\AbstractAdapter;
use App\Xml\Reader;
@ -19,6 +18,7 @@ use NowPlaying\AdapterFactory;
use NowPlaying\Result\Result;
use PhpIP\IP;
use PhpIP\IPBlock;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use Supervisor\Supervisor;
@ -34,7 +34,7 @@ abstract class AbstractFrontend extends AbstractAdapter
Environment $environment,
EntityManagerInterface $em,
Supervisor $supervisor,
EventDispatcher $dispatcher,
EventDispatcherInterface $dispatcher,
LoggerInterface $logger
) {
parent::__construct($environment, $em, $supervisor, $dispatcher, $logger);

View File

@ -7,7 +7,6 @@ namespace App\Sync;
use App\Entity\Repository\SettingsRepository;
use App\Environment;
use App\Event\GetSyncTasks;
use App\EventDispatcher;
use App\LockFactory;
use App\Message;
use Doctrine\ORM\EntityManagerInterface;
@ -15,6 +14,7 @@ use Exception;
use InvalidArgumentException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LogLevel;
/**
@ -27,7 +27,7 @@ class Runner
protected Environment $environment,
protected Logger $logger,
protected LockFactory $lockFactory,
protected EventDispatcher $eventDispatcher,
protected EventDispatcherInterface $eventDispatcher,
protected EntityManagerInterface $em
) {
}

View File

@ -7,9 +7,9 @@ namespace App\Sync\Task;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity;
use App\Event\Radio\AnnotateNextSong;
use App\EventDispatcher;
use App\Radio\Adapters;
use App\Radio\Backend\Liquidsoap;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
class CheckRequests extends AbstractTask
@ -17,7 +17,7 @@ class CheckRequests extends AbstractTask
public function __construct(
protected Entity\Repository\StationRequestRepository $requestRepo,
protected Adapters $adapters,
protected EventDispatcher $dispatcher,
protected EventDispatcherInterface $dispatcher,
ReloadableEntityManagerInterface $em,
LoggerInterface $logger
) {

View File

@ -8,7 +8,6 @@ use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity;
use App\Environment;
use App\Event\Radio\GenerateRawNowPlaying;
use App\EventDispatcher;
use App\Http\RouterInterface;
use App\LockFactory;
use App\Message;
@ -18,6 +17,7 @@ use DeepCopy\DeepCopy;
use Exception;
use Monolog\Logger;
use NowPlaying\Result\Result;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -30,7 +30,7 @@ class NowPlayingTask extends AbstractTask implements EventSubscriberInterface
protected Adapters $adapters,
protected AutoDJ $autodj,
protected CacheInterface $cache,
protected EventDispatcher $eventDispatcher,
protected EventDispatcherInterface $eventDispatcher,
protected MessageBus $messageBus,
protected LockFactory $lockFactory,
protected RouterInterface $router,

View File

@ -10,6 +10,7 @@ use App\Traits\RequestAwareTrait;
use Doctrine\Inflector\InflectorFactory;
use League\Plates\Engine;
use League\Plates\Template\Data;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;
@ -21,7 +22,7 @@ class View extends Engine
public function __construct(
Environment $environment,
EventDispatcher $dispatcher,
EventDispatcherInterface $dispatcher,
Version $version,
RouterInterface $router,
protected Assets $assets