prewk/file-chainer

Chainable file stream writer with insert support

0.3.0 2014-12-14 11:06 UTC

This package is auto-updated.

Last update: 2024-11-29 04:19:49 UTC


README

What is it?

A small wrapper for some php file streaming functions with support for inserting data into streams without overwriting. (Also: chainable.)

Installation

Add to composer:

require: {
    "prewk/file-chainer": "dev-master"
}

And run composer install.

Usage

Two new file stream methods:

  • finsert inserts a string at the current file stream position
  • finsertcsv inserts a csv (like fputcsv) line at the current file stream position

Inserting & chaining

Prewk\FileChainer::make()
    ->fopen("/foo/bar.txt", "w+")
    ->fwrite("foo")
    ->rewind()
    ->finsert("bar")
    ->fclose();

echo file_get_contents("/foo/bar.txt");
// Output: barfoo
// The handle's file pointer = 3

Inserting with static method

$handle = fopen("/foo/bar.txt", "w+");
fwrite($handle, "foo");
rewind($handle);

// Statically, using the default "temporary file stream" method
Prewk\FileChainer\Inserters\File::finsert($handle, "bar");

fclose($handle);

echo file_get_contents("/foo/bar.txt");
// Output: barfoo
// The handle's file pointer = 3