Skip to content

Commit

Permalink
Merge pull request #59 from acelaya-forks/feature/remove-swoole
Browse files Browse the repository at this point in the history
Drop support for openswoole
  • Loading branch information
acelaya authored Feb 17, 2024
2 parents b1e915a + 1d05094 commit e3b2403
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 344 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ on:
jobs:
ci:
uses: shlinkio/github-actions/.github/workflows/php-lib-ci.yml@main
with:
extra-extensions: openswoole-22.1.2
secrets:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* *Nothing*

### Fixed
* *Nothing*
* Remove support for openswoole


## [3.1.0] - 2023-11-25
Expand Down
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM composer:2

ENV OPENSWOOLE_VERSION 22.1.2

RUN apk add --no-cache openssl openssl-dev linux-headers
RUN apk add --no-cache libpng-dev libpng libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
RUN docker-php-ext-install gd sockets

RUN apk add --no-cache --virtual .phpize-deps ${PHPIZE_DEPS} unixodbc-dev && \
pecl install openswoole-${OPENSWOOLE_VERSION} pcov && \
docker-php-ext-enable openswoole pcov && \
pecl install pcov && \
docker-php-ext-enable pcov && \
apk del .phpize-deps
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Shlink Event Dispatcher

This library simplifies registering async and regular [PSR-14](https://www.php-fig.org/psr/psr-14/) event listeners while using [RoadRunner](https://roadrunner.dev/) or [openswoole](https://openswoole.com/) via [`mezzio/mezzio-swoole`](https://docs.mezzio.dev/mezzio-swoole/).
This library simplifies registering async and regular [PSR-14](https://www.php-fig.org/psr/psr-14/) event listeners while using [RoadRunner](https://roadrunner.dev/).

Async ones are executed using RoadRunner jobs or openswoole's task system. This library takes care of the boilerplate of registering events as async tasks/jobs, and you just interact with plain PSR-14 listeners and events.
Async ones are executed using RoadRunner jobs. This library takes care of the boilerplate of registering events as async tasks/jobs, and you just interact with plain PSR-14 listeners and events.

Most of the elements it provides require a [PSR-11](https://www.php-fig.org/psr/psr-11/) container, and it's easy to integrate on [mezzio](https://github.com/mezzio/mezzio) applications thanks to the `ConfigProvider` it includes.

Expand All @@ -25,13 +25,12 @@ Install this library using composer:

This module allows to register both regular and asynchronous event listeners on a PSR-14 EventDispatcher.

Regular listeners are executed on the same process, blocking the dispatching of the HTTP request, while asynchronous listeners are delegated to a RoadRunner job or openswoole background task, making the request to resolve immediately.
Regular listeners are executed on the same process, blocking the dispatching of the HTTP request, while asynchronous listeners are delegated to a RoadRunner job, making the request to resolve immediately.

If neither RoadRunner nor openswoole are found, async listeners are ignored by default, but you can choose to make them to be registered as regular listeners instead.
If RoadRunner is not found, async listeners are ignored by default, but you can choose to make them to be registered as regular listeners instead.

> **Note**
> * In order to be able to integrate with RoadRunner jobs, you need to install `spiral/roadrunner-jobs`.
> * In order to be able to integrate with openswoole tasks, you need to install `mezzio/mezzio-swoole`.
> In order to be able to integrate with RoadRunner jobs, you need to install `spiral/roadrunner-jobs`.
In order to register listeners you have to use a configuration like this:

Expand Down Expand Up @@ -67,7 +66,7 @@ return [
The `events` config entry has these blocks.

* `regular`: A list of events with all the listeners tha should be dispatched synchronously for each one of them.
* `async`: A list of events with all the listeners that should be executed as RoadRunner jobs or openswoole tasks for each one of them.
* `async`: A list of events with all the listeners that should be executed as RoadRunner jobs for each one of them.

In both cases, listeners are identified by their service name, making the services to be lazily resolved only when their corresponding event gets dispatched.

Expand Down
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "shlinkio/shlink-event-dispatcher",
"type": "library",
"homepage": "https://shlink.io",
"description": "Simplifies registering async and regular PSR-14 event listeners while using RoadRunner or openswoole.",
"description": "Simplifies registering async and regular PSR-14 event listeners while using RoadRunner.",
"license": "MIT",
"authors": [
{
Expand All @@ -21,8 +21,6 @@
},
"require-dev": {
"infection/infection": "^0.27.9",
"mezzio/mezzio-swoole": "^4.9",
"openswoole/ide-helper": "~22.1.5",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.5",
Expand All @@ -32,7 +30,6 @@
"symfony/var-dumper": "^7.0"
},
"suggest": {
"mezzio/mezzio-swoole": "To be able to run async event dispatchers as swoole tasks",
"spiral/roadrunner-jobs": "To be able to run async event dispatchers as roadrunner job tasks"
},
"autoload": {
Expand Down
14 changes: 0 additions & 14 deletions config/event-dispatcher.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Shlinkio\Shlink\EventDispatcher;

use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Mezzio\Swoole\Event\SwooleListenerProvider;
use Psr\EventDispatcher as Psr;
use Psr\Log\LoggerInterface;

return [

Expand All @@ -18,24 +15,13 @@

'dependencies' => [
'factories' => [
Swoole\TaskFinishListener::class => ConfigAbstractFactory::class,
Dispatcher\SyncEventDispatcherFactory::SYNC_DISPATCHER => Dispatcher\SyncEventDispatcherFactory::class,
Dispatcher\EventDispatcherAggregate::class => Dispatcher\EventDispatcherAggregateFactory::class,
],

'aliases' => [
Psr\EventDispatcherInterface::class => Dispatcher\EventDispatcherAggregate::class,
],

'delegators' => [
SwooleListenerProvider::class => [
Swoole\SwooleListenersProviderDelegator::class,
],
],
],

ConfigAbstractFactory::class => [
Swoole\TaskFinishListener::class => [LoggerInterface::class],
],

];
30 changes: 0 additions & 30 deletions config/swoole.config.php

This file was deleted.

19 changes: 1 addition & 18 deletions src/Dispatcher/EventDispatcherAggregateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,17 @@

namespace Shlinkio\Shlink\EventDispatcher\Dispatcher;

use Mezzio\Swoole\Event\EventDispatcherInterface as SwooleEventDispatcherInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\EventDispatcher\RoadRunner\RoadRunnerEventDispatcherFactory;

class EventDispatcherAggregateFactory
{
public function __invoke(ContainerInterface $container): EventDispatcherAggregate
{
// TODO For Shlink 4.0.0
// $asyncDispatcher = $container->get(RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER)
$asyncDispatcher = $this->resolveAsyncDispatcher($container);
$asyncDispatcher = $container->get(RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER);
$regularDispatcher = $container->get(SyncEventDispatcherFactory::SYNC_DISPATCHER);
$eventsConfig = $container->get('config')['events'] ?? [];

return new EventDispatcherAggregate($asyncDispatcher, $regularDispatcher, $eventsConfig);
}

/**
* @deprecated Use $container->get(RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER) instead
*/
private function resolveAsyncDispatcher(ContainerInterface $container): EventDispatcherInterface
{
if (! $container->has(SwooleEventDispatcherInterface::class)) {
// This dispatcher will actually not register anything if RoadRunner is not available
return $container->get(RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER);
}

return $container->get(SwooleEventDispatcherInterface::class);
}
}
41 changes: 0 additions & 41 deletions src/Swoole/SwooleListenersProviderDelegator.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Swoole/TaskFinishListener.php

This file was deleted.

24 changes: 1 addition & 23 deletions test/Dispatcher/EventDispatcherAggregateFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ShlinkioTest\Shlink\EventDispatcher\Dispatcher;

use League\Event\EventDispatcher;
use Mezzio\Swoole\Event\EventDispatcherInterface as SwooleEventDispatcherInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -30,11 +29,8 @@ public function setUp(): void
}

#[Test, DataProvider('provideConfigs')]
public function createsAsyncDispatcherWhenNotFoundInContainer(array $config): void
public function fetchesAsyncDispatcherFromContainer(array $config): void
{
$this->container->expects($this->once())->method('has')->with(
SwooleEventDispatcherInterface::class,
)->willReturn(false);
$this->container->expects($this->exactly(3))->method('get')->willReturnMap([
[RoadRunnerEventDispatcherFactory::ROAD_RUNNER_DISPATCHER, $this->ed],
[SyncEventDispatcherFactory::SYNC_DISPATCHER, $this->ed],
Expand All @@ -44,24 +40,6 @@ public function createsAsyncDispatcherWhenNotFoundInContainer(array $config): vo
($this->factory)($this->container);
}

/**
* @test
* @dataProvider provideConfigs
*/
public function fetchesAsyncDispatcherFromContainerWhenFound(array $config): void
{
$this->container->expects($this->once())->method('has')->with(
SwooleEventDispatcherInterface::class,
)->willReturn(true);
$this->container->expects($this->exactly(3))->method('get')->willReturnMap([
[SwooleEventDispatcherInterface::class, $this->ed],
[SyncEventDispatcherFactory::SYNC_DISPATCHER, $this->ed],
['config', $config],
]);

($this->factory)($this->container);
}

public static function provideConfigs(): iterable
{
yield 'empty config' => [[]];
Expand Down
5 changes: 2 additions & 3 deletions test/Dispatcher/SyncEventDispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Shlinkio\Shlink\EventDispatcher\Dispatcher\SyncEventDispatcherFactory;
use Shlinkio\Shlink\EventDispatcher\Listener\EnabledListenerCheckerInterface;
use stdClass;
use Symfony\Contracts\EventDispatcher\Event;

class SyncEventDispatcherFactoryTest extends TestCase
{
Expand Down Expand Up @@ -89,7 +88,7 @@ static function (ListenerProviderInterface $provider): void {
],
],
'async' => [
Event::class => [
EventDispatcher::class => [
'foo',
'bar',
'baz',
Expand All @@ -98,7 +97,7 @@ static function (ListenerProviderInterface $provider): void {
]],
static function (ListenerProviderInterface $provider): void {
Assert::assertCount(2, [...$provider->getListenersForEvent(new stdClass())]);
Assert::assertEmpty([...$provider->getListenersForEvent(new Event())]);
Assert::assertEmpty([...$provider->getListenersForEvent(new EventDispatcher())]);
},
];
}
Expand Down
6 changes: 3 additions & 3 deletions test/RoadRunner/RoadRunnerEventDispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ShlinkioTest\Shlink\EventDispatcher\RoadRunner;

use League\Event\EventDispatcher;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -15,7 +16,6 @@
use Spiral\RoadRunner\Jobs\Jobs;
use Spiral\RoadRunner\Jobs\JobsInterface;
use stdClass;
use Symfony\Contracts\EventDispatcher\Event;

use function putenv;
use function sprintf;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function shouldRegisterListener(string $event, string $listener, bool $is
$listenerProvider = $this->getPrivateProp($dispatcher, 'listenerProvider');

Assert::assertCount(0, [...$listenerProvider->getListenersForEvent(new stdClass())]);
Assert::assertCount(1, [...$listenerProvider->getListenersForEvent(new Event())]);
Assert::assertCount(1, [...$listenerProvider->getListenersForEvent(new EventDispatcher())]);
}

private function getPrivateProp(object $object, string $propName): mixed
Expand All @@ -89,7 +89,7 @@ private function container(?EnabledListenerCheckerInterface $listenerChecker = n
'events' => [
'async' => [
stdClass::class => ['bar', 'baz'],
Event::class => ['foo'],
EventDispatcher::class => ['foo'],
],
],
]],
Expand Down
Loading

0 comments on commit e3b2403

Please sign in to comment.