innmind / framework
Http/Cli framework
Requires
- php: ~8.2
- innmind/cli: ^3.1
- innmind/di: ~2.1
- innmind/filesystem: ~7.0
- innmind/http-server: ~4.0
- innmind/immutable: ~5.2
- innmind/operating-system: ~4.1|~5.0
- innmind/router: ~4.1
- innmind/url: ^4.1
- ramsey/uuid: ^4.7
Requires (Dev)
- innmind/async-http-server: ~2.0|~3.0
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- vimeo/psalm: ~5.6
Suggests
- innmind/async-http-server: To run a local asynchronous http server
- innmind/black-box: For property based testing
Provides
Conflicts
- innmind/async-http-server: <2.0|~4.0
- innmind/black-box: <5.0|~6.0
README
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
.