2017 © Pedro Peláez
 

library safe-stream

Nette SafeStream: atomic and safe manipulation with files via native PHP functions.

image

nette/safe-stream

Nette SafeStream: atomic and safe manipulation with files via native PHP functions.

  • Friday, June 22, 2018
  • by david@grudl.com
  • Repository
  • 32 Watchers
  • 59 Stars
  • 1,262,015 Installations
  • PHP
  • 176 Dependents
  • 0 Suggesters
  • 12 Forks
  • 1 Open issues
  • 8 Versions
  • 5 % Grown

The README.md

Downloads this Month Tests Coverage Status Latest Stable Version License, (*1)

Introduction

SafeStream guarantees that every read and write to a file is isolated. This means that no thread will start reading a file that is not yet fully written, or multiple threads will not overwrite the same file., (*2)

Installation:, (*3)

composer require nette/safe-stream

What is it good for?

What are isolated operations actually good for? Let's start with a simple example that repeatedly writes to a file and then reads the same string from it:, (*4)

$s = str_repeat('Long String', 10000);

$counter = 1000;
while ($counter--) {
    file_put_contents('file', $s); // write it
    $readed = file_get_contents('file'); // read it
    if ($s !== $readed) { // check it
        echo 'strings are different!';
    }
}

It may seem that echo 'strings differ!' can never occur. The opposite is true. Try running this script in two browser tabs at the same time. The error will occur almost immediately., (*5)

One of the tabs will read the file at a time when the other hasn't had a chance to write it all, so the content will not be complete., (*6)

Therefore, the code is not safe if it is executed multiple times at the same time (i.e. in multiple threads). Which is not uncommon on the internet, often a server is responding to a large number of users at one time. So ensuring that your application works reliably even when executed in multiple threads (thread-safe) is very important. Otherwise, data will be lost and hard-to-detect errors will occur., (*7)

But as you can see, PHP's native file read and write functions are not isolated and atomic., (*8)

How to use SafeStream?

SafeStream creates a secure protocol to read and write files in isolation using standard PHP functions. All you need to do is to specify nette.safe:// before the file name:, (*9)

file_put_contents('nette.safe://file', $s);
$s = file_get_contents('nette.safe://file');

SafeStream ensures that at most one thread can write to the file at a time. The other threads are waiting in the queue. If no thread is writing, any number of threads can read the file in parallel., (*10)

All common PHP functions can be used with the protocol, for example:, (*11)

// 'r' means open read-only
$handle = fopen('nette.safe://file.txt', 'r');

$ini = parse_ini_file('nette.safe://translations.neon');

Documentation can be found on the website. If you like it, please make a donation now. Thank you!, (*12)

The Versions

22/06 2018

dev-master

9999999-dev https://nette.org

Nette SafeStream: atomic and safe manipulation with files via native PHP functions.

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

filesystem nette atomic safe

13/07 2017

v2.3.3

2.3.3.0 https://nette.org

Nette SafeStream: atomic and safe manipulation with files via native PHP functions.

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

filesystem nette atomic safe

19/03 2016

v2.3.2

2.3.2.0 https://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

06/02 2016

2.2.x-dev

2.2.9999999.9999999-dev https://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

11/07 2015

v2.2.1

2.2.1.0 http://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

11/07 2015

v2.3.1

2.3.1.0 http://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

27/01 2015

v2.3.0

2.3.0.0 http://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires

25/05 2014

v2.2.0

2.2.0.0 http://nette.org

Nette SafeStream: Atomic Operations

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0

The Requires

  • php >=5.3.1

 

The Development Requires