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-message: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
README
A PSR-compliant toolkit for building high-performance HTTP and WebSocket servers using Swoole with dependency management.
Features
- PSR-7 HTTP message interfaces
- PSR-15 middleware support
- PSR-11 container integration
- Named routes support via League Router
- WebSocket support
- Clean architecture structure
Installation
Install the package via Composer and require the PSR-7 implementation of your choice:
composer require imefisto/swoole-psr-kit composer require http-interop/http-factory-guzzle
Basic Usage
Lets setup a basic example using our provided Example controller.
Create a config.php
file with your server configuration:
return [ // some configuration ]
Create a dependencies.php
file with your container definitions:
use Http\Factory\Guzzle\ResponseFactory; use Http\Factory\Guzzle\StreamFactory; use Http\Factory\Guzzle\UploadedFileFactory; use Http\Factory\Guzzle\UriFactory; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UriFactoryInterface; return [ ResponseFactoryInterface::class => \DI\get(ResponseFactory::class), StreamFactoryInterface::class => \DI\get(StreamFactory::class), UploadedFileFactoryInterface::class => \DI\get(UploadedFileFactory::class), UriFactoryInterface::class => \DI\get(UriFactory::class), ];
Create a routes.php
file with your routes:
use Imefisto\SwooleKit\Presentation\Controller\Example; return [ ['GET', '/example', Example::class, 'getExample'], ];
Create a server.php
file with your server implementation:
use Imefisto\SwooleKit\Infrastructure\DependencyInjection\ContainerFactory; use Imefisto\SwooleKit\Infrastructure\Routing\Router; use Imefisto\SwooleKit\Infrastructure\Swoole\Server; require __DIR__ . '/vendor/autoload.php'; $config = include '/path/to/config.php'; $dependencies = '/path/to/dependencies.php'; $routes = include '/path/to/routes.php'; $container = ContainerFactory::create($config, $dependencies, $routes); $server = $container->get(Server::class); $server->run();
Documentation
For detailed documentation, please see the /docs directory.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.