Object-Oriented API for PHP streams

Overview

Streamer

Streamer is an Object-Oriented API for PHP streams.

Why should I use Streams?

A stream is a flow of bytes from one container to the other. You already use streams a lot in PHP, for instance each time you load a file into memory (file_get_contents()). You should explicitly use streams each time that:

  • You need to access data from a container, but you don't know the size of this container (e.g. reading from STDIN, or a web service using streaming)
  • You need to start processing data from a container before the whole transfer is finished (e.g. start zipping a file before it's all in memory)
  • You need to save time and memory

What is Streamer?

PHP has a very elaborate stream API ; unfortunately, it uses functions for most stream operations (except for wrappers - go figure). Streamer is a generic library focusing on offering an object-oriented API to streams, and only that.

Installation

Streamer is published on packagist.org, so you can add it to your composer.json file for an easy installation:

composer require fzaninotto/Streamer

or

{
    "require": {
        "fzaninotto/Streamer": "0.0.1"
    }
}

Example

<?php
use Streamer\Stream,
	Streamer\FileStream,
	Streamer\NetworkStream;

// basic usage
$stream = new Stream(fopen('smiley.png', 'r'));
$image = '';
while (!$stream->isEOF()) {
  $image .= $stream->read();
}

// pipe dreams!
$stream1 = new Stream(fopen('smiley.png', 'r'));
$stream2 = new Stream(fopen('tmp.png', 'w'));
// copy the contents from the first stream to the second one
$stream1->pipe($stream2);

// factory
$fileStream = FileStream::create('smiley.png', 'r');
print_r($fileStream);

$networkStream = NetworkStream::create('tcp://www.google.com:80');
print_r($networkStream);

Credits

Streamer is heavily inspired by other Stream class implementations:

Comments
  • Project status

    Project status

    As title, does @fzaninotto still consider/maintain this?

    It seems that this package has been inactive for the long time.

    And some issues should be concerned.

    opened by peter279k 1
  • Question regarding stream usage

    Question regarding stream usage

    Hi,

    I have a question regarding the stream. Basically I was playing with the streams to find a fix for https://github.com/auraphp/Aura.Http/issues/29 .

    This is the code I tried.

    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    $networkStream = Streamer\NetworkStream::create('https://api.github.com/orgs/auraphp/repos');
    print_r($networkStream);
    

    and running it got into exception.

    $ php test.php
    
    PHP Warning:  stream_socket_client() expects parameter 6 to be resource, null given in /var/www/github.com/harikt/streamer/vendor/fzaninotto/streamer/Streamer/NetworkStream.php on line 9
    PHP Fatal error:  Uncaught exception 'Streamer\Exception\InvalidArgumentException' with message 'A Stream object requires a stream resource as constructor argument' in /var/www/github.com/harikt/streamer/vendor/fzaninotto/streamer/Streamer/Stream.php:25
    Stack trace:
    #0 /var/www/github.com/harikt/streamer/vendor/fzaninotto/streamer/Streamer/NetworkStream.php(9): Streamer\Stream->__construct(false)
    #1 /var/www/github.com/harikt/streamer/test.php(4): Streamer\NetworkStream::create('https://api.git...')
    #2 {main}
      thrown in /var/www/github.com/harikt/streamer/vendor/fzaninotto/streamer/Streamer/Stream.php on line 25
    

    Any help to understand / fix this is appreciated.

    opened by harikt 0
  • FileStream::create , $context cannot be null

    FileStream::create , $context cannot be null

    // factory $fileStream = FileStream::create('smiley.png', 'r'); print_r($fileStream); // exception: fopen() expects parameter 4 to be resource, null given` , // my php versoin: 5.6.17

    opened by absszero 0
  • Auto-closing the stream in __destruct() leads to invalid resource, if Stream used inside a function

    Auto-closing the stream in __destruct() leads to invalid resource, if Stream used inside a function

    I'd experienced that if you using the Stream class inside a function the __destruct function will close the stream, after the function is finished, so the according file resource is not valid anymore.

    Here is a small exploit:

    function streamerTest($outputStream)
    {
        $stream = new \Streamer\Stream($outputStream);
        $stream->write('foo' . PHP_EOL);
    }
    
    $outputStream = tmpfile();
    
    var_dump($outputStream);
    var_dump(is_resource($outputStream));
    
    streamerTest($outputStream);
    
    echo "\n
    after function call
    \n";
    
    var_dump($outputStream);
    var_dump(is_resource($outputStream));
    

    The output is:

    resource(93) of type (stream)
    bool(true)
    
    after function call
    
    resource(93) of type (Unknown)
    bool(false)
    

    I guess that behavior is intentional and in general maybe not wrong at all. But in case, i want to reuse the resource i given to the Stream, that behavior messing with my code.

    My preference would that you make it optional and you can deactivate that behavior in the class constructor via an option or something. I can help you with that, if you want.

    opened by k00ni 3
  • Be more oo - in the documentation

    Be more oo - in the documentation

    I don't think this kind of approach is oo:

    new Stream(fopen('smiley.png', 'r'));
    

    it would be better this way:

    new FileStream('smiley.png');
    

    I see now, that you have FileStream class. Why don't you use it in the documentation?

    opened by inf3rno 1
Owner
Francois Zaninotto
Open-source software enthusiast, React.js, Node.js and PHP expert, Lean & Agile management practicioner, Leadership & motivation experimenter, CEO @marmelab
Francois Zaninotto
PHP implementation for reading and writing Apache Parquet files/streams

php-parquet This is the first parquet file format reader/writer implementation in PHP, based on the Thrift sources provided by the Apache Foundation.

null 17 Oct 25, 2022
Test case to reproduce a PHP segmentation fault involving Xdebug, streams, and dates

The code in this repository causes a segmentation fault when run with PHP 8.0.6 on Fedora 33. This issue was originally reported to the Xdebug project

Matthew Turland 0 Jul 31, 2021
Provides an object-oriented API to query in-memory collections in a SQL-style.

POQ - PHP Object Query Install composer require alexandre-daubois/poq 1.0.0-beta2 That's it, ready to go! ?? Usage Here is the set of data we're going

Alexandre Daubois 16 Nov 29, 2022
This packages enables the ability to serve file streams in a smart way

A blade component for easy image manipulation Want to serve private hosted images without the need to code your own logic ? Want to resize your images

Dieter Coopman 205 Dec 19, 2022
the examples of head first object oriented analysis & design - in PHP

Head First object oriented analysis & design in (PHP) after cloning the repository, you have to install the project's dependancies by running the foll

Muhammed ElFeqy 3 Oct 16, 2021
PHP FFmpeg - An Object Oriented library to convert video/audio files with FFmpeg / AVConv

PHP FFmpeg An Object Oriented library to convert video/audio files with FFmpeg / AVConv. Check another amazing repo: PHP FFMpeg extras, you will find

Alchemy 10 Dec 31, 2022
An object oriented wrapper around PHP's built-in server.

Statix Server Requirements PHP 8 minumum Installation composer require statix/server Basic Usage To get started, ensure the vendor autoload script is

Statix PHP 113 Dec 27, 2022
PHP Unoconv - An Object Oriented library which allow easy to use file conversion with Unoconv.

An Object Oriented library which allow easy to use file conversion with Unoconv. Install The recommended way to install PHP-Unoconv is thr

Alchemy 69 Dec 3, 2022
The XP Framework is an all-purpose, object oriented PHP framework.

XP Framework Core This is the XP Framework's development checkout. Installation If you just want to use the XP Framework, grab a release using compose

XP-Framework 17 Dec 14, 2022
An article about alternative solution for convert object into a JSON Object for your api.

Do we really need a serializer for our JSON API? The last years I did build a lot of JSON APIs but personally was never happy about the magic of using

Alexander Schranz 1 Feb 1, 2022
Strings Package provide a fluent, object-oriented interface for working with multibyte string

Strings Package provide a fluent, object-oriented interface for working with multibyte string, allowing you to chain multiple string operations together using a more readable syntax compared to traditional PHP strings functions.

Glowy PHP 14 Mar 12, 2022
Your alter ego object. Takes the best of object and array worlds.

Supporting Opensource formapro\values is an MIT-licensed open source project with its ongoing development made possible entirely by the support of com

FormaPro 31 Jun 25, 2021
A simple but extensible economy engine for PocketMine-MP, using label-oriented APIs.

Capital A simple but very extensible economy plugin for PocketMine-MP. How is Capital different from other economy plugins? Capital introduces a label

Jonathan Chan Kwan Yin 37 Dec 19, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
PHP library that helps to map any input into a strongly-typed value object structure.

Valinor • PHP object mapper with strong type support Valinor is a PHP library that helps to map any input into a strongly-typed value object structure

Team CuyZ 873 Jan 7, 2023
FUGIO: Automatic Exploit Generation for PHP Object Injection Vulnerabilities

FUGIO FUGIO is the first automatic exploit generation (AEG) tool for PHP object injection (POI) vulnerabilities. When exploiting a POI vulnerability,

KAIST Web Security and Privacy Lab 53 Dec 23, 2022
An object graph visualizer

print_o An object graph visualizer for PHP What is object graph ? Object-oriented applications contain complex webs of interrelated objects. Objects a

Akihito Koriyama 139 Jan 1, 2023
A data transfer object inspired by Rust's serde

Data Transfer Object Want to deserialize an object with data on the fly? Go for it by using the From trait. How is this package any different from spa

Randy Schütt 37 Dec 15, 2022
Allows reflection of object attributes, including inherited and non-public ones

sebastian/object-reflector Allows reflection of object attributes, including inherited and non-public ones. Installation You can add this library as a

Sebastian Bergmann 6k Jan 4, 2023