All Projects → nette → Safe Stream

nette / Safe Stream

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

Projects that are alternatives of or similar to Safe Stream

Finder
🔍 Finder: find files and directories with an intuitive API.
Stars: ✭ 765 (+657.43%)
Mutual labels:  nette, nette-framework, filesystem
Mobile Detect
Extension for detecting mobile devices, managing mobile view types, redirect to mobile version for Nette Framework (2.4+)
Stars: ✭ 19 (-81.19%)
Mutual labels:  nette, nette-framework
Nette
👪 METAPACKAGE for Nette Framework components
Stars: ✭ 1,356 (+1242.57%)
Mutual labels:  nette, nette-framework
Tracy
😎 Tracy: the addictive tool to ease debugging PHP code for cool developers. Friendly design, logging, profiler, advanced features like debugging AJAX calls or CLI support. You will love it.
Stars: ✭ 1,345 (+1231.68%)
Mutual labels:  nette, nette-framework
Di
💎 Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 7 features.
Stars: ✭ 645 (+538.61%)
Mutual labels:  nette, nette-framework
Neon
🍸 Encodes and decodes NEON file format.
Stars: ✭ 674 (+567.33%)
Mutual labels:  nette, nette-framework
Guzzlette
🌀 Guzzle integration into Nette Framework (@nette)
Stars: ✭ 19 (-81.19%)
Mutual labels:  nette, nette-framework
Cookbook
🎶 Cookbook for Nette Framework (@nette) & Contributte (@contributte). Read it while its HOT!
Stars: ✭ 30 (-70.3%)
Mutual labels:  nette, nette-framework
Naja
Modern AJAX library for Nette Framework
Stars: ✭ 86 (-14.85%)
Mutual labels:  nette, nette-framework
Middlewares
💥 Middlewares / Relay / PSR-7 support to Nette Framework (@nette)
Stars: ✭ 13 (-87.13%)
Mutual labels:  nette, nette-framework
Latte
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.
Stars: ✭ 616 (+509.9%)
Mutual labels:  nette, nette-framework
Php Generator
🐘 Generates neat PHP code for you. Supports new PHP 8.0 features.
Stars: ✭ 1,264 (+1151.49%)
Mutual labels:  nette, nette-framework
Bootstrap
🅱 The simple way to configure and bootstrap your Nette application.
Stars: ✭ 524 (+418.81%)
Mutual labels:  nette, nette-framework
Robot Loader
🍀 RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.
Stars: ✭ 690 (+583.17%)
Mutual labels:  nette, nette-framework
Schema
📐 Validating data structures against a given Schema.
Stars: ✭ 359 (+255.45%)
Mutual labels:  nette, nette-framework
Mail
📧 Handy email creation and transfer library for PHP with both text and MIME-compliant support.
Stars: ✭ 288 (+185.15%)
Mutual labels:  nette, nette-framework
Forms
📝 Generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.
Stars: ✭ 272 (+169.31%)
Mutual labels:  nette, nette-framework
Tester
Tester: enjoyable unit testing in PHP with code coverage reporter. 🍏🍏🍎🍏
Stars: ✭ 281 (+178.22%)
Mutual labels:  nette, nette-framework
Flash Messages
Flash messages handler for Nette Framework (2.4+)
Stars: ✭ 8 (-92.08%)
Mutual labels:  nette, nette-framework
Utils
🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
Stars: ✭ 1,158 (+1046.53%)
Mutual labels:  nette, nette-framework

Nette SafeStream: Atomic Operations

Downloads this Month Tests Coverage Status Latest Stable Version License

Introduction

The Nette SafeStream protocol for file manipulation guarantees atomicity and isolation of every file operation.

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

Installation:

composer require nette/safe-stream

It requires PHP version 8.0.

What exactly are atomic operations good for? Let's start with a simple example that repeatedly writes the same string to a file and then reads it:

$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 the echo 'Strings are different!' command can't ever get executed. The opposite is true. Try to run this script in two browsers tabs simultaneously. The error occurs almost immediately.

One tab reads the file at the moment when the other has not yet finished writing it.

Therefore, the code is not safe when performed multiple at the same time (ie, in multiple threads). And that is nothing unusual on the Internet, where several people often connect to one website at the same time. Therefore, it's very important to ensure that your application can handle multiple threads at once - that it's thread-safe because native PHP functions are not. Otherwise, you can expect data loss and strange errors occurring.

The SafeStream offers solution: its secure protocol, through which we can atomically manipulate files through standard PHP functions. You just need prefix the filename with nette.safe://.

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

If we used the secure protocol in the first example, there would never be an error.

All known functions can be used with the protocol, for example:

$handle = fopen('nette.safe://file.txt', 'x');

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

How does it Work?

SafeStream guarantees:

  • Atomicity: The file is written either as a whole or not written at all.
  • Isolation: No one can start to read a file that is not yet fully written.

To ensure both it uses file locks and temporary files.

Writing is done to temporary files and they are renamed only after successful writing. If the write fails for any reason, such as a script error or insufficient disk space, it will be discarded and an incomplete file will not be created.

If you write to an existing file in a (append) mode, SafeStream will create a copy of it. Writing in this mode therefore has a higher overhead than writing in other modes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].