darkwood/flow

Flow

资助包维护!
matyo91

v1.2.2 2024-08-30 17:02 UTC

README

Flow

为什么?

Flow 概念旨在解决

  • 采用异步作为原生实现
  • 使用函数式编程构建你的代码
  • 可视地组装你的代码

安装

PHP 8.3 是使用 Flow 的最低版本。推荐通过 Composer 安装并执行

composer require darkwood/flow

用法

<?php

use Flow\Flow\Flow;
use Flow\Ip;

class D1 {
    public function __construct(public int $n1) {}
}

class D2 {
    public function __construct(public int $n2) {}
}

class D3 {
    public function __construct(public int $n3) {}
}

class D4 {
    public function __construct(public int $n4) {}
}

$flow = Flow::do(static function() {
    yield fn (D1 $data1) => new D2($data1->n1 += 1);
    yield fn (D2 $data2) => new D3($data2->n2 * 2);
    yield function(D3 $data3) {
        printf("my number %d\n", $ip->data->n3)); // display 'my number 10'

        return new D4($data3->n3);
    };
});

$ip = new Ip(new D1(4));
$flow($ip);
$flow->await();

示例

一个可工作的脚本位于捆绑的 examples 目录中

  • 运行 Flow : php examples/flow.php
  • 运行 Y-Combinator Flow : php examples/yflow.php
  • 启动服务器 : php examples/server.php
    启动客户端 : php examples/client.php

文档

https://darkwood-com.github.io/flow

许可证

Flow 在 MIT 许可证下发布。