M4W Framework

Paglikha ng aplikasyon

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

Service ng application

I-save ang class na ito sa app/Services/RequestInfo.php. Hinihingi ng constructor nito sa container ng framework ang kasalukuyang 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];
    }
}

Pagsulat ng handler

I-save ang code bilang server.php sa parehong directory ng m4w. May sariling container ang bawat request. Irehistro ang request bago kunin ang service mula sa container.

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));

Pagsisimula ng server

I-save ang code bilang m4w.json sa parehong directory ng server.php.

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

Sa isang pangalawang terminal, magpadala ng isang kahilingan sa loopback listener.

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