PHPackages                             reactphp-x/stream-to-stream - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. reactphp-x/stream-to-stream

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

reactphp-x/stream-to-stream
===========================

v1.0.0(1y ago)00MITPHP

Since May 7Pushed 1y agoCompare

[ Source](https://github.com/reactphp-x/stream-to-stream)[ Packagist](https://packagist.org/packages/reactphp-x/stream-to-stream)[ RSS](/packages/reactphp-x-stream-to-stream/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

ReactPHP Stream to Stream
=========================

[](#reactphp-stream-to-stream)

一个用于处理 ReactPHP 流之间数据转换的库。这个库允许你轻松地在两个流之间建立双向连接，并在数据传输过程中进行转换。

特性
--

[](#特性)

- 支持双向流数据传输
- 支持数据转换函数
- 错误处理和事件传播
- 支持流的关闭和资源清理
- 支持 Promise 和异步操作

安装
--

[](#安装)

使用 Composer 安装:

```
composer require reactphp-x/stream-to-stream -vvv
```

基本用法
----

[](#基本用法)

```
use React\EventLoop\Loop;
use React\Stream\ThroughStream;
use ReactphpX\StreamToStream\StreamToStream;

$read1 = new ThroughStream();
$read2 = new ThroughStream();
$write1 = new ThroughStream();
$write2 = new ThroughStream();

// 创建流
$inputStream = new CompositeStream($read1, $write1);
$outputStream = new CompositeStream($read2, $write2);

// 创建 StreamToStream 实例
$streamToStream = StreamToStream::create();

// 设置数据转换函数
$inMapBuffer = function($data) {
    return "Received: " . $data;
};

// 设置输出数据转换函数
$outMapBuffer = function($data) {
    return "Echo: " . $data;
};

// 连接流并应用转换
$streamToStream->from($inputStream, $inMapBuffer)
              ->bridge($outputStream, $outMapBuffer);

// 写入数据
$inputStream->write("Hello World");
```

高级用法
----

[](#高级用法)

### Echo 服务器示例

[](#echo-服务器示例)

```
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use React\Stream\CompositeStream;
use ReactphpX\StreamToStream\StreamToStream;

$loop = Loop::get();

// 创建读写流
$readable = new ReadableResourceStream(STDIN, $loop);
$writable = new WritableResourceStream(STDOUT, $loop);

// 创建组合流
$inputStream = new CompositeStream($readable, $writable);
$outputStream = new CompositeStream($readable, $writable);

// 创建 StreamToStream 实例
$streamToStream = StreamToStream::create();

// 设置数据转换函数
$inMapBuffer = function($data) {
    return "Received: " . $data;
};

// 连接流
$streamToStream->from($inputStream, $inMapBuffer)
              ->bridge($outputStream);

// 设置错误处理
$inputStream->on('error', function($error) use ($loop) {
    echo "Stream error: " . $error->getMessage() . "\n";
    $loop->stop();
});

// 运行事件循环
$loop->run();
```

API 文档
------

[](#api-文档)

### StreamToStream 类

[](#streamtostream-类)

#### `create()`

[](#create)

创建一个新的 StreamToStream 实例。

#### `from($fromStream, $inMapBuffer = null)`

[](#fromfromstream-inmapbuffer--null)

设置源流和输入数据转换函数。

- `$fromStream`: 实现了 DuplexStreamInterface 的流对象
- `$inMapBuffer`: 可选的数据转换函数

#### `bridge($stream, $outMapBuffer = null)`

[](#bridgestream-outmapbuffer--null)

建立两个流之间的双向连接，并设置数据转换函数。

- `$stream`: 实现了 DuplexStreamInterface 的流对象或返回此类对象的 Promise
- `$outMapBuffer`: 可选的数据转换函数，用于从目标流返回到源流的数据转换

测试
--

[](#测试)

运行测试：

```
composer install
./vendor/bin/phpunit tests
```

测试覆盖了以下场景：

- 基本数据传输
- 数据转换功能
- 错误处理
- 流关闭处理

要求
--

[](#要求)

- PHP &gt;= 8.1
- ReactPHP Stream ^1.4
- ReactPHP Promise ^3.2

许可证
---

[](#许可证)

MIT

作者
--

[](#作者)

- wpjscc

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance49

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

376d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/76907477?v=4)[wpjscc](/maintainers/wpjscc)[@wpjscc](https://github.com/wpjscc)

---

Top Contributors

[![wpjscc](https://avatars.githubusercontent.com/u/76907477?v=4)](https://github.com/wpjscc "wpjscc (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/reactphp-x-stream-to-stream/health.svg)

```
[![Health](https://phpackages.com/badges/reactphp-x-stream-to-stream/health.svg)](https://phpackages.com/packages/reactphp-x-stream-to-stream)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.4k187.2M2.6k](/packages/composer-composer)[friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

13.5k234.7M20.6k](/packages/friendsofphp-php-cs-fixer)[react/react

ReactPHP: Event-driven, non-blocking I/O with PHP.

9.1k3.6M63](/packages/react-react)[reactivex/rxphp

Reactive extensions for php.

1.7k3.2M49](/packages/reactivex-rxphp)[react/child-process

Event-driven library for executing child processes with ReactPHP.

34076.1M136](/packages/react-child-process)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
