imefisto / swoole-psr-kit
A PSR-compliant toolkit for building Swoole HTTP/WebSocket servers with dependency management
Requires
- php: ^8.1
- imefisto/psr-swoole-native: ^2
- league/route: ^6
- php-di/php-di: ^7.0
- psr/container: ^2.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^3.0
Requires (Dev)
- nyholm/psr7: ^1.8
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-14 17:05:51 UTC
README
A PHP library providing clean interfaces and handlers for Swoole server implementation, with support for HTTP, WebSocket, and Worker management.
⚠️ Alpha Release: This project is currently in alpha stage (v2.x-alpha). The API may change significantly between versions. Version 2.x contains breaking changes from 1.x.
Version Information
- Current:
2.0.0-alpha.4(Alpha - Breaking changes from 1.x) - Previous:
1.1.0(Legacy - Not recommended for new projects)
Migration from 1.x
If you're upgrading from version 1.x, please note there are breaking changes in 2.x. See CHANGELOG.md for detailed migration information.
Requirements
- PHP 8.3+
- Swoole 5.1+
Installation
For new projects (recommended):
composer require imefisto/swoole-psr-kit:^2.0@alpha
For existing 1.x projects:
composer require imefisto/swoole-psr-kit:^1.1
Features
- HTTP request handling with PSR-7 compliance
- WebSocket server support with clean event handlers
- Worker management interface
- Middleware support
- Extensible handler system
Basic Usage
See the folder examples/.
Testing
TestHttpKernel dispatches PSR-7 requests through your routes without starting a server, so HTTP tests don't need the Swoole extension:
use Imefisto\SwooleKit\DependencyInjection\ContainerFactory; use Imefisto\SwooleKit\Routing\Router; use Imefisto\SwooleKit\Testing\TestHttpKernel; use Nyholm\Psr7\ServerRequest; use Psr\Http\Message\ResponseFactoryInterface; $container = ContainerFactory::create($config, $dependencies, $routes); $kernel = new TestHttpKernel( $container->get(Router::class), $container->get(ResponseFactoryInterface::class) ); $response = $kernel->handle(new ServerRequest('GET', '/hello'));
RequestBuilder cuts down request setup. It's immutable, so a base builder can be shared between tests:
use Imefisto\SwooleKit\Testing\RequestBuilder; use Nyholm\Psr7\Factory\Psr17Factory; $psr17Factory = new Psr17Factory(); $builder = (new RequestBuilder($psr17Factory, $psr17Factory)) ->withHeader('Authorization', 'Bearer token'); $response = $kernel->handle( $builder->post('/users?notify=1')->withJson(['name' => 'Ada'])->build() );
withJson() sets the body and Content-Type but not the parsed body; register JsonBodyParserMiddleware as you would on the server.
Routing errors (404, 405) and HttpExceptions become responses, as they do on the server. Any other exception is rethrown so your test shows the real failure.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
The MIT License (MIT). Please see License File for more information.