mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 09:13:11 +08:00
Moved all event-dispatching stuff to its own module
This commit is contained in:
33
module/EventDispatcher/src/Async/Task.php
Normal file
33
module/EventDispatcher/src/Async/Task.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
use function get_class;
|
||||
use function sprintf;
|
||||
|
||||
class Task
|
||||
{
|
||||
/** @var string */
|
||||
private $regularListenerName;
|
||||
/** @var object */
|
||||
private $event;
|
||||
|
||||
public function __construct(string $regularListenerName, object $event)
|
||||
{
|
||||
$this->regularListenerName = $regularListenerName;
|
||||
$this->event = $event;
|
||||
}
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
($container->get($this->regularListenerName))($this->event);
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return sprintf('Listener -> "%s", Event -> "%s"', $this->regularListenerName, get_class($this->event));
|
||||
}
|
||||
}
|
||||
55
module/EventDispatcher/src/Async/TaskRunner.php
Normal file
55
module/EventDispatcher/src/Async/TaskRunner.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Http\Server as HttpServer;
|
||||
use Throwable;
|
||||
|
||||
use function get_class;
|
||||
use function gettype;
|
||||
use function is_object;
|
||||
|
||||
class TaskRunner
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
/** @var ContainerInterface */
|
||||
private $container;
|
||||
|
||||
public function __construct(LoggerInterface $logger, ContainerInterface $container)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
public function __invoke(HttpServer $server, int $taskId, int $fromId, $task): void
|
||||
{
|
||||
if (! $task instanceof Task) {
|
||||
$this->logger->error('Invalid task provided to task worker: {type}', [
|
||||
'type' => is_object($task) ? get_class($task) : gettype($task),
|
||||
]);
|
||||
$server->finish('');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logger->notice('Starting work on task {taskId}: {task}', [
|
||||
'taskId' => $taskId,
|
||||
'task' => $task->toString(),
|
||||
]);
|
||||
|
||||
try {
|
||||
$task($this->container);
|
||||
} catch (Throwable $e) {
|
||||
$this->logger->error('Error processing task {taskId}: {e}', [
|
||||
'taskId' => $taskId,
|
||||
'e' => $e,
|
||||
]);
|
||||
} finally {
|
||||
// Notify the server that processing of the task has finished:
|
||||
$server->finish('');
|
||||
}
|
||||
}
|
||||
}
|
||||
29
module/EventDispatcher/src/Async/TaskRunnerDelegator.php
Normal file
29
module/EventDispatcher/src/Async/TaskRunnerDelegator.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Http\Server as HttpServer;
|
||||
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
|
||||
|
||||
class TaskRunnerDelegator implements DelegatorFactoryInterface
|
||||
{
|
||||
public function __invoke(
|
||||
ContainerInterface $container,
|
||||
$name,
|
||||
callable $callback,
|
||||
array $options = null
|
||||
): HttpServer {
|
||||
$server = $callback();
|
||||
$logger = $container->get(LoggerInterface::class);
|
||||
|
||||
$server->on('task', $container->get(TaskRunner::class));
|
||||
$server->on('finish', function (HttpServer $server, int $taskId) use ($logger) {
|
||||
$logger->notice('Task #{taskId} has finished processing', ['taskId' => $taskId]);
|
||||
});
|
||||
|
||||
return $server;
|
||||
}
|
||||
}
|
||||
17
module/EventDispatcher/src/Async/TaskRunnerFactory.php
Normal file
17
module/EventDispatcher/src/Async/TaskRunnerFactory.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class TaskRunnerFactory implements FactoryInterface
|
||||
{
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): TaskRunner
|
||||
{
|
||||
$logger = $container->get(LoggerInterface::class);
|
||||
return new TaskRunner($logger, $container);
|
||||
}
|
||||
}
|
||||
15
module/EventDispatcher/src/ConfigProvider.php
Normal file
15
module/EventDispatcher/src/ConfigProvider.php
Normal file
@@ -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));
|
||||
}
|
||||
}
|
||||
26
module/EventDispatcher/src/Listener/AsyncEventListener.php
Normal file
26
module/EventDispatcher/src/Listener/AsyncEventListener.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Listener;
|
||||
|
||||
use Shlinkio\Shlink\EventDispatcher\Async\Task;
|
||||
use Swoole\Http\Server as HttpServer;
|
||||
|
||||
class AsyncEventListener
|
||||
{
|
||||
/** @var string */
|
||||
private $regularListenerName;
|
||||
/** @var HttpServer */
|
||||
private $server;
|
||||
|
||||
public function __construct(HttpServer $server, string $regularListenerName)
|
||||
{
|
||||
$this->regularListenerName = $regularListenerName;
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
public function __invoke(object $event): void
|
||||
{
|
||||
$this->server->task(new Task($this->regularListenerName, $event));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Listener;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Phly\EventDispatcher\ListenerProvider\AttachableListenerProvider;
|
||||
use Swoole\Http\Server as HttpServer;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
use function Phly\EventDispatcher\lazyListener;
|
||||
use function Shlinkio\Shlink\EventDispatcher\asyncListener;
|
||||
|
||||
class ListenerProviderFactory implements FactoryInterface
|
||||
{
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
{
|
||||
$config = $container->has('config') ? $container->get('config') : [];
|
||||
$events = $config['events'] ?? [];
|
||||
$provider = new AttachableListenerProvider();
|
||||
|
||||
$this->registerListeners($events['regular'] ?? [], $container, $provider);
|
||||
$this->registerListeners($events['async'] ?? [], $container, $provider, true);
|
||||
|
||||
return $provider;
|
||||
}
|
||||
|
||||
private function registerListeners(
|
||||
array $events,
|
||||
ContainerInterface $container,
|
||||
AttachableListenerProvider $provider,
|
||||
bool $isAsync = false
|
||||
): void {
|
||||
// Avoid registering async event listeners when the swoole server is not registered
|
||||
if ($isAsync && ! $container->has(HttpServer::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($events as $eventName => $listeners) {
|
||||
foreach ($listeners as $listenerName) {
|
||||
$eventListener = $isAsync
|
||||
? asyncListener($container->get(HttpServer::class), $listenerName)
|
||||
: lazyListener($container, $listenerName);
|
||||
|
||||
$provider->listen($eventName, $eventListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user