innmind/framework 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

innmind/framework

Composer 安装命令:

composer require innmind/framework

包简介

Http/Cli framework

README 文档

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,
    Http\Route,
};
use Innmind\DI\Service;
use Innmind\Http\{
    ServerRequest,
    Response,
    Response\StatusCode,
};
use Innmind\Filesystem\File\Content;
use Innmind\Immutable\Attempt;

enum Services implements Service
{
    case hello;
}

new class extends Http {
    protected function configure(Application $app): Application
    {
        return $app
            ->service(Services::hello, static fn() => static fn(
                ServerRequest $request,
                ?string $name = null,
            ) => Attempt::result(Response::of(
                StatusCode::ok,
                $request->protocolVersion(),
                null,
                Content::ofString(\sprintf(
                    'Hello %s!',
                    $name ?? 'world',
                )),
            )))
            ->route(Route::get(
                '/',
                Services::hello,
            ))
            ->route(Route::get(
                '/{name}',
                Services::hello,
            ));
    }
};

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\Time\{
    Clock,
    Format,
};
use Innmind\DI\Container;
use Innmind\CLI\{
    Console,
    Command,
    Command\Usage,
};
use Innmind\Immutable\{
    Attempt,
    Str,
};

enum Services implements Service
{
    case clock;
}

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

                    public function __invoke(Console $console): Attempt
                    {
                        $today = $this->clock->now()->format(Format::iso8601());

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

                    public function usage(): Usage
                    {
                        return Usage::of('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.

统计信息

  • 总下载量: 2.79k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固