brandoriented/swoole-server-bundle
Install the Bundle:
composer require brandoriented/swoole-server-bundle
Ensure BrandOriented\SwooleServerBundle\SwooleServerBundle is registered in config/bundles.php.
Configure the Server:
Override default settings in config/packages/brandoriented_swoole_server.yaml:
host: 0.0.0.0
port: 8080
options:
log_file: "%kernel.logs_dir%/swoole.log"
document_root: "%kernel.project_dir%/public"
First Use Case: Start the server with:
php bin/console swoole:server:start
Verify it’s running by accessing http://localhost:8080 or checking logs in var/log/swoole.log.
Hybrid Development:
Task Workers:
Swoole\Coroutine or Swoole\AsyncTask.use BrandOriented\SwooleServerBundle\Task\TaskManager;
class NotificationService {
public function __construct(private TaskManager $taskManager) {}
public function sendAsyncEmail(string $email) {
$this->taskManager->dispatch('send_email', [$email]);
}
}
Static File Handling:
enable_static_handler to let Swoole serve static assets (CSS/JS/images) directly, reducing PHP-FPM load.document_root to point to your public/ directory.Event Listeners:
onStart, onRequest) via Symfony’s event dispatcher:
# config/packages/brandoriented_swoole_server.yaml
events:
on_start: App\EventListener\SwooleStartListener
Load Balancing:
worker_num and reactor_num to optimize CPU/memory usage for high-traffic apps.options:
worker_num: 4
reactor_num: 2
Daemonization Conflicts:
daemonize: true, ensure pid_file is writable (e.g., /var/run/swoole_server.pid). Use chmod 777 temporarily for testing.var/log/swoole.log for permission errors or use daemonize: false in development.Static Handler Caveats:
enable_static_handler bypasses Symfony’s middleware (e.g., security, caching). Exclude sensitive routes from static handling.dispatch_mode: 1 (fixed) to prioritize PHP routes over static files.Task Worker Isolation:
Port Reuse:
enable_port_reuse: true helps avoid "Address already in use" errors on restart. Test in staging before production.SSL Configuration:
ssl_cert_file/ssl_key_file, ensure paths are absolute and files are readable. Test with:
openssl s_client -connect localhost:8080 -servername yourdomain.com
Log Levels:
options:
log_level: 7 # DEBUG (1-7: ERROR to DEBUG)
Coroutine Debugging:
Swoole\Coroutine::getuid() to trace coroutine IDs in logs:
\Swoole\Coroutine::debug();
Process Monitoring:
php bin/console swoole:server:ps
php bin/console swoole:server:kill <worker_id>
Custom Middleware:
BrandOriented\SwooleServerBundle\Middleware\AbstractMiddleware to add logic before/after requests.Protocol Support:
BrandOriented\SwooleServerBundle\Protocol\ProtocolInterface.Configuration Validation:
BrandOriented\SwooleServerBundle\DependencyInjection\Configuration to add custom validation for options.Metrics Integration:
$server->stats(); // Access Swoole server metrics
```markdown
---
**Note**: The package is minimally documented (0 stars, no active maintenance). Validate all `options` in a staging environment before production use. Consider forking to add missing features (e.g., WebSocket support, Docker integration).
How can I help you explore Laravel packages today?