Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial release #2

Merged
merged 15 commits into from
Nov 17, 2024
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1', '8.2', '8.3']

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: composer:v2

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run tests
run: vendor/bin/phpunit

- name: Static Analysis
run: vendor/bin/phpstan analyse

coding-standards:
name: Coding Standards
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none
tools: composer:v2, php-cs-fixer

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Check coding standards
run: php-cs-fixer fix --dry-run --diff
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/vendor/
/composer.lock
/.phpunit.cache/
/.phpunit.result.cache
/.php-cs-cache
/phpstan.cache
.idea/
.vscode/
*.swp
*.swo
.DS_Store
.php-cs-fixer.cache
Empty file removed .gitkeep
Empty file.
23 changes: 23 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/examples',
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'no_extra_blank_lines' => true,
'single_quote' => true,
'trailing_comma_in_multiline' => true,
])
->setFinder($finder);
29 changes: 29 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2024, ArchiPro
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# PSR-14 Async Event Dispatcher (Experimental)

[![CI](https://github.com/archiprocode/revolt-event-dispatcher/actions/workflows/ci.yml/badge.svg)](https://github.com/archiprocode/revolt-event-dispatcher/actions/workflows/ci.yml)

A PSR-14 compatible event dispatcher implementation using Revolt and AMPHP for asynchronous event handling.

## Features

- Full PSR-14 compatibility
- Asynchronous event dispatching using Revolt's event loop
- Support for stoppable and non-stoppable events
- Fire-and-forget event dispatching
- Type-safe event handling

## Installation

```bash
composer require archipro/revolt-event-dispatcher
```
47 changes: 47 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "archipro/revolt-event-dispatcher",
"description": "PSR-14 Event Dispatcher implementation using Revolt and AMPHP",
"authors": [
{
"name": "ArchiPro",
"email": "[email protected]"
}
],
"type": "library",
"require": {
"php": "^8.1",
"psr/event-dispatcher": "^1.0",
"revolt/event-loop": "^1.0",
"amphp/amp": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^1.10",
"friendsofphp/php-cs-fixer": "^3.14"
},
"autoload": {
"psr-4": {
"ArchiPro\\EventDispatcher\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ArchiPro\\EventDispatcher\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit",
"analyse": "phpstan analyse",
"cs-check": "php-cs-fixer fix --dry-run --diff",
"cs-fix": "php-cs-fixer fix",
"check": [
"@cs-check",
"@analyse",
"@test"
]
},
"license": "BSD-3-Clause",
"provide": {
"psr/event-dispatcher-implementation": "1.0"
}
}
62 changes: 62 additions & 0 deletions examples/basic-usage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use Amp\TimeoutCancellation;
use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\Event\AbstractStoppableEvent;
use ArchiPro\EventDispatcher\ListenerProvider;
use Revolt\EventLoop;

// Create a custom event
class UserCreatedEvent extends AbstractStoppableEvent
{
public function __construct(
public readonly string $userId,
public readonly string $email
) {
}
}

// Create the listener provider and register listeners
$listenerProvider = new ListenerProvider();

$listenerProvider->addListener(UserCreatedEvent::class, function (UserCreatedEvent $event) {
// Simulate async operation
EventLoop::delay(
1,
function () use ($event) {
echo "Sending welcome email to {$event->email}\n";
}
);
});

$listenerProvider->addListener(UserCreatedEvent::class, function (UserCreatedEvent $event) {
// Simulate async operation
EventLoop::delay(
0.5,
function () use ($event) {
echo "Logging user creation: {$event->userId}\n";
}
);
});

// Create the event dispatcher
$dispatcher = new AsyncEventDispatcher($listenerProvider);

// Dispatch an event
$event = new UserCreatedEvent('123', '[email protected]');
$dispatcher->dispatch($event);

// Run the event loop to process all events
EventLoop::run();

// Wait for the event to finish right away
$event = new UserCreatedEvent('456', '[email protected]');
$future = $dispatcher->dispatch($event);
$updatedEvent = $future->await();

// Make an event cancellable
$event = new UserCreatedEvent('789', '[email protected]');
$future = $dispatcher->dispatch($event, new TimeoutCancellation(30));
EventLoop::run();
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
level: 8
paths:
- src
- tests
- examples
tmpDir: phpstan.cache
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="false"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
Loading
Loading