innmind/framework

Http/Cli framework

2.3.1 2024-10-26 13:08 UTC

This package is auto-updated.

Last update: 2024-11-26 13:21:21 UTC


README

Build Status codecov Type Coverage

Minimalist HTTP/CLI framework that accomodate to simple applications to complex ones via middlewares.

The framework configuration is immutable and use a declarative approach.

Important: to correctly use this library you must validate your code with vimeo/psalm

Installation

composer require innmind/framework

Usage

Take a look at the documentation for a more in-depth understanding of the framework.

Http

The first step is to create the index file that will be exposed via a webserver (for example public/index.php). Then you need to specify the routes you want to handle.

Note

if you don't configure any route it will respond with 404 Not Found with an empty body.

<?php
declare(strict_types = 1);

require 'path/to/composer/autoload.php';

use Innmind\Framework\{
    Main\Http,
    Application,
};
use Innmind\Router\Route\Variables;
use Innmind\Http\{
    ServerRequest,
    Response,
    Response\StatusCode,
};
use Innmind\Filesystem\File\Content;

new class extends Http {
    protected function configure(Application $app): Application
    {
        return $app
            ->route('GET /', static fn(ServerRequest $request) => Response::of(
                StatusCode::ok,
                $request->protocolVersion(),
                null,
                Content::ofString('Hello world!'),
            ))
            ->route('GET /{name}', static fn(ServerRequest $request, Variables $variables) => Response::of(
                StatusCode::ok,
                $request->protocolVersion(),
                null,
                Content::ofString("Hello {$variables->get('name')}!"),
            ));
    }
};

You can run this script via cd public && php -S localhost:8080. If you open your web browser it will display Hello world! and if you go to /John it will display Hello John!.

Cli

The entrypoint of your cli tools will look something like this.

Note

by default if you don't configure any command it will always display Hello world.

<?php
declare(strict_types = 1);

require 'path/to/composer/autoload.php';

use Innmind\Framework\{
    Main\Cli,
    Application,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\TimeContinuum\{
    Clock,
    Earth\Format\ISO8601,
};
use Innmind\DI\Container;
use Innmind\CLI\{
    Console,
    Command,
};
use Innmind\Immutable\Str;

new class extends Cli {
    protected function configure(Application $app): Application
    {
        return $app->command(
            static fn(Container $container, OperatingSystem $os) => new class($os->clock()) implements Command {
                public function __construct(
                    private Clock $clock,
                ) {
                }

                public function __invoke(Console $console): Console
                {
                    $today = $this->clock->now()->format(new ISO8601);

                    return $console->output(Str::of("We are the: $today\n"));
                }

                public function usage(): string
                {
                    return 'today';
                }
            },
        );
    }
};

We can execute our script via php filename.php (or php filename.php today) and it would output something like We are the: 2022-12-30T14:04:50+00:00.