Convert between Symfony HttpFoundation Request / Response and Swoole Request / Response classes with swoole-http-message-bridge

Today I was implementing an HTTP Server using Swoole. At its core, the code looks just like the Swoole HTTP Server example:

$http = new \Swoole\HTTP\Server("", 9501);

$http->on('request', function (\Swoole\HTTP\Request $request, \Swoole\HTTP\Response $response) {
    // …


In that same project I was also looking to use a package that evolves around symfony/http-foundation which:

  1. Takes in a \Symfony\Component\HttpFoundation\Request instance
  2. Processes the $request to a \Symfony\Component\HttpFoundation\Response
  3. Returns the $response

Instead of adjusting the processor to work with the \Swoole\HTTP\Request and \Swoole\HTTP\Response classes, I looked into converting the \Swoole\HTTP classes to their \Symfony\Component\HttpFoundation counterparts. That way the processor itself would remain untouched, and only some translation before and after would be done.


My solution lay into the indragunawan/swoole-http-message-bridge package that provides just that. However, the package hadn’t been updated in two years and it was showing: It didn’t support symfony/http-foundation version 5. Above that builds were failing.

Luckily for me those errors were easily fixable, and the author was kind enough to merge my two Pull Requests (#3 and #4) in only a few hours time 🙂


use Indragunawan\SwooleHttpMessageBridge\Symfony\Request as RequestFactory;
use Indragunawan\SwooleHttpMessageBridge\Symfony\Response as ResponseWriter;

$http = new \Swoole\HTTP\Server("", 9501);

$http->on('request', function (\Swoole\HTTP\Request $request, \Swoole\HTTP\Response $response) {
    // Convert the \Swoole\HTTP\Request instance into a \Symfony\Component\HttpFoundation\Request instance
    $sfRequest = RequestFactory::createFromSwooleRequest($request);

    // Process \Symfony\Component\HttpFoundation\Request, yielding a \Symfony\Component\HttpFoundation\Response
    $sfResponse = …;

    // Convert the \Symfony\Component\HttpFoundation\Response instance into a \Swoole\HTTP\Response instance and write it out to the client 
    ResponseWriter::writeSwooleResponse($response, $sfResponse);


Installation per Composer:

composer require indragunawan/swoole-http-message-bridge:^0.1.0

There’s a few more changes and additions that could be done to this package, but I’ll leave it at that for the time being 😉


Did this help you out? Like what you see?
Thank me with a coffee.

I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!

BuymeaCoffee (€3)

To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.