M4W Framework

Create an application

Shell
composer create-project mini4work/framework my-app
cd my-app
composer require mini4work/webserver

Application service

Save this class in app/Services/RequestInfo.php. Its constructor asks the framework container for the current request.

PHP
<?php

namespace App\Services;

use M4W\WebServer\Http\Request;

final class RequestInfo
{
    public function __construct(private Request $request) {}

    public function describe(): array
    {
        return ['method' => $this->request->method, 'uri' => $this->request->uri];
    }
}

Write a handler

Save this as server.php next to m4w. Each request gets its own container. Register the request before resolving the service.

PHP
<?php

use App\Services\RequestInfo;
use M4W\Core\Framework;
use M4W\WebServer\Cli\ServerCommand;
use M4W\WebServer\Http\CallbackRequestHandler;
use M4W\WebServer\Http\Request;
use M4W\WebServer\Http\Response;

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

$handler = new CallbackRequestHandler(static function (Request $request): Response {
    $app = new Framework();
    $app->singleton(Request::class, $request);
    return Response::json($app->make(RequestInfo::class)->describe());
});

exit((new ServerCommand)->run($argv, $handler));

Start the server

Save this as m4w.json next to server.php.

JSON
{
  "listen": { "port": 9000 }
}
Shell
php server.php m4w.json

In a second terminal, send a request to the loopback listener.

Shell
curl -i 'http://127.0.0.1:9000/inspect?q=hello'
JSON
{"method": "GET", "uri": "/inspect?q=hello"}