PHPackages                             talesoft/tale-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. [HTTP &amp; Networking](/categories/http)
4. /
5. talesoft/tale-stream

ActiveLibrary[HTTP &amp; Networking](/categories/http)

talesoft/tale-stream
====================

A basic PSR-7 and PSR-17 compatible stream utility library

0.6.0(6y ago)34.5k2MITPHPPHP &gt;=7.4.0CI failing

Since Jan 22Pushed 6y ago4 watchersCompare

[ Source](https://github.com/Talesoft/tale-stream)[ Packagist](https://packagist.org/packages/talesoft/tale-stream)[ Docs](http://docs.talesoft.codes/php/tale/stream)[ RSS](/packages/talesoft-tale-stream/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (8)Dependencies (4)Versions (9)Used By (2)

[![Packagist](https://camo.githubusercontent.com/3a6d600ed478d84f3540e8f37ccf7cf752b51fa1ed172f80c7020e1df633bcf4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74616c65736f66742f74616c652d73747265616d2e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/talesoft/tale-stream)[![License](https://camo.githubusercontent.com/3f6abb3a7cd18b4cdf41186b911f3ec69d9490017f7697e0c6253b90dfa4afec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f54616c65736f66742f74616c652d73747265616d2e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/Talesoft/tale-stream/blob/master/LICENSE.md)[![CI](https://camo.githubusercontent.com/716b724885d36e7080b0a4b39bd00d739ca038b2dc5ed5bbb7e8c85e33e881e9/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f54616c65736f66742f74616c652d73747265616d2e7376673f7374796c653d666f722d7468652d6261646765)](https://travis-ci.org/Talesoft/tale-stream)[![Coverage](https://camo.githubusercontent.com/43a7a42748aba465668b0465b0aa49b7536b345f9afdc99807d1364f185ed6cb/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f636f7665726167652f54616c65736f66742f74616c652d73747265616d2e7376673f7374796c653d666f722d7468652d6261646765)](https://codeclimate.com/github/Talesoft/tale-stream)

Tale Stream
===========

[](#tale-stream)

What is Tale Stream?
--------------------

[](#what-is-tale-stream)

This is a direct implementation of the PSR-7 `Psr\Http\Message\StreamInterface` and the PSR-17 `Psr\Http\Message\StreamFactoryInterface`. It doesn't add any extra methods, only a few utility stream factories and some useful iterators for streams.

It acts as a stable streaming library core for full-fledged streaming libraries like socket implementations or filesystem abstractions.

Installation
------------

[](#installation)

```
composer req talesoft/tale-stream
```

Usage
-----

[](#usage)

Check out the [Functions File](https://github.com/Talesoft/tale-stream/blob/master/src/functions.php)to see all things this library does.

### Stream all the things

[](#stream-all-the-things)

The heart is the `Tale\Stream` class you can use with the `Tale\stream`function. You can pass any resource of type `stream` and have a PSR-7 compatible stream for any purpose.

```
use function Tale\stream;

$stream = stream(fopen('/some/file', 'rb+'));

if ($stream->isReadable()) {
    $contents = $stream->read(100);
}

if ($stream->isWritable()) {
    $stream->write('Some Content');
}

$stream->close();
```

### Create a Stream factory for DI containers

[](#create-a-stream-factory-for-di-containers)

```
use Psr\Http\Message\StreamFactoryInterface;
use Tale\StreamFactory;

$container->add(StreamFactory::class);

// ...

$streamFactory = $container->get(StreamFactoryInterface::class);

$stream = $streamFactory->createStreamFromFile('./some-file.txt', 'rb');

echo $stream; // ""
```

### File Streams

[](#file-streams)

The filestream works analogous to the `fopen($filename, $mode, $useIncludePath, $context)` function.

```
use function Tale\stream_file;

$stream = stream_file('./some-file.txt', 'rb');

echo $stream->getContents(); // ""
```

### Memory Streams

[](#memory-streams)

Memory streams only reside in memory and are gone when the execution ended. This is very useful to create streams on the fly wherever you need one. They are always readable and writable.

```
use function Tale\stream_memory;

$stream = stream_memory('Some Content!');

echo $stream->getContents(); // "Some Content!"
```

### Temporary Streams

[](#temporary-streams)

Temporary streams work like memory streams, but at some point they will start swapping data into a file to save memory.

```
use function Tale\stream_memory;

$stream = stream_temp('Some Content!', 1024);
// Stream will start swapping after 1024 bytes
```

#### Input Stream

[](#input-stream)

The input stream is a readable file stream on `php://input`. In most cases, this is the raw HTTP request body and useful for APIs that work with structured data formats.

```
use function Tale\stream_input;

$stream = stream_input();

$data = json_decode($stream->getContents());
echo $data['firstName'];
```

#### Output Stream

[](#output-stream)

The output stream is a writable file stream on `php://output`. This is mostly the output content that is sent to the browser.

```
use function Tale\stream_output;

$stream = stream_output();

$stream->write('This will be sent to the browser');
```

#### STDIN Stream

[](#stdin-stream)

The standard input stream is a readable file stream on `php://stdin`. This is e.g. content from a piped command.

```
use function Tale\stream_stdin;

$stream = stream_stdin();

echo $stream->getContents(); // ""
```

#### STDERR Stream

[](#stderr-stream)

The standard error stream is a writable file stream on `php://stderr`. This is mostly used for output of errors in console commands.

```
use function Tale\stream_stderr;

$stream = stream_stderr();

$stream->write('Error: Something bad happened');
```

#### STDOUT Stream

[](#stdout-stream)

The standard output stream is a writable file stream on `php://stdout`. This is mostly console command output.

```
use function Tale\stream_stdout;

$stream = stream_stdout();

$stream->write("Working...please wait...\n");
```

### Null Stream

[](#null-stream)

The null-stream does nothing. It only implements the interfaces. Useful to avoid defensive null checks on optional dependencies, to suppress things and for testing.

```
use function Tale\stream_null;

$stream = stream_null();

echo $stream->read(100); // Will always return an empty string
```

### Read streams with iterators

[](#read-streams-with-iterators)

The ReadIterator will read a stream chunk-by-chunk (default chunk size is 1024). Notice, all the iterators here work with any PSR-7 stream, not only `Tale\Stream` instances.

```
use function Tale\stream_memory;
use function Tale\stream_read;

$stream = stream_memory('abcdefg');

$iterator = stream_read($stream, 2); //Chunk size of 2

$chunks = iterator_to_array($iterator);
// You could alternatively iterate the iterator with foreach

dump($chunks); // ['ab', 'cd', 'ef', 'g']
```

### Iterate lines of a stream

[](#iterate-lines-of-a-stream)

The LineIterator works differently to `fgets()`. While the chunk size on `fgets()` limits the length a line can has, the LineIterator will just wait for the actual end of the line and doesn't care about the chunk size.

```
use function Tale\stream_memory;
use function Tale\stream_get_lines;

$stream = stream_memory("Line 1\nLine 2\nLine 3");

$lines = stream_get_lines($stream);

dump(iterator_to_array($lines)); // ["Line 1", "Line 2", "Line 3"]
```

#### Split streams by delimiters

[](#split-streams-by-delimiters)

The SplitIterator is the reason why the LineIterator can work like it does. It can split streams by any delimiter of any length and doesn't care about the chunk size of the internal reader.

```
use function Tale\stream_memory;
use function Tale\stream_split;

$stream = stream_memory('a,b,c,d');

$items = stream_split($stream, ',');

dump(iterator_to_array($items)); // ['a', 'b', 'c', 'd']
```

#### Write to streams with iterators

[](#write-to-streams-with-iterators)

The WriteIterator is a utility to write iterables to streams easily.

```
use function Tale\stream_memory;
use function Tale\stream_write;

function generateLines()
{
    yield "Line 1\n";
    yield "Line 2\n";
    yield "Line 3\n";
}

$stream = stream_memory();

$writer = stream_write($stream, generateLines());
$writtenBytes = $writer->writeAll();

// or use stream_write_all()
$writtenBytes = stream_write_all($stream, generateLines());

// You could also iterate the write to leave place for other actions
// e.g. in async environments
```

### Pipe streams

[](#pipe-streams)

The ReadIterator and WriteIterator combined provide a solid way to pipe streams efficiently.

```
use function Tale\stream_memory;
use function Tale\stream_pipe;

$inputStream = stream_memory('Some content');
$outputStream = stream_memory();

$writer = stream_pipe($inputStream, $outputStream);
$writer->writeAll();

// or use stream_pipe_all()
$writtenBytes = stream_pipe_all($inputStream, $outputStream);
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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

Every ~207 days

Recently: every ~132 days

Total

8

Last Release

2356d ago

PHP version history (3 changes)0.1PHP &gt;=5.5.0

0.2.0PHP &gt;=7.1.0

0.6.0PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/75a80e0830e63c723808d021d3a1648a2643db60f4ac2e40842da05f227f956b?d=identicon)[TorbenKoehn](/maintainers/TorbenKoehn)

---

Top Contributors

[![TorbenKoehn](https://avatars.githubusercontent.com/u/1403556?v=4)](https://github.com/TorbenKoehn "TorbenKoehn (41 commits)")

---

Tags

phppsr-7streampsr-7streampsr-17stream-wrappermemory-streamtemp-stream

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/talesoft-tale-stream/health.svg)

```
[![Health](https://phpackages.com/badges/talesoft-tale-stream/health.svg)](https://phpackages.com/packages/talesoft-tale-stream)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

7.9k1.1B3.8k](/packages/guzzlehttp-psr7)[symfony/psr-http-message-bridge

PSR HTTP message bridge

1.3k312.3M932](/packages/symfony-psr-http-message-bridge)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28146.3k](/packages/phpro-http-tools)[laminas/laminas-stratigility

PSR-7 middleware foundation for building and dispatching middleware pipelines

577.0M95](/packages/laminas-laminas-stratigility)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28545.4k3](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

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