icicleio/filesystem

Asynchronous filesystem component for Icicle.

v0.1.0 2016-01-28 17:43 UTC

This package is auto-updated.

Last update: 2024-10-14 03:44:10 UTC


README

Asynchronous filesystem access that is always non-blocking, no extensions required.

This library is a component for Icicle, providing asynchronous filesystem functions and abstracting files as asynchronous streams. Like other Icicle components, this library uses Coroutines built from Awaitables and Generators to make writing asynchronous code more like writing synchronous code.

Build Status Coverage Status Semantic Version MIT License @icicleio on Twitter

Documentation and Support

Requirements
  • PHP 5.5+ for v0.1.x branch (current stable) and v1.x branch (mirrors current stable)
  • PHP 7 for v2.0 branch (under development) supporting generator delegation and return expressions
Installation

The recommended way to install is with the Composer package manager. (See the Composer installation guide for information on installing and using Composer.)

Run the following command to use this library in your project:

composer require icicleio/filesystem

You can also manually edit composer.json to add this library as a project requirement.

// composer.json
{
    "require": {
        "icicleio/filesystem": "^0.1"
    }
}
Suggested
  • eio extension: Uses libeio to provide asynchronous file access (v1.2.6+ required).

Example

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

use Icicle\Coroutine;
use Icicle\File;
use Icicle\Loop;

Coroutine\create(function () {
    $path = __DIR__ . '/test.txt';

    // Create and open the file for reading and writing.
    $file = (yield File\open($path, 'w+'));

    try {
        // Write data to file.
        $written = (yield $file->write('testing'));
        
        printf("Wrote %d bytes to file.\n", $written);
        
        // Seek to beginning of file.
        yield $file->seek(0);
        
        // Read data from file.
        $data = (yield $file->read());
    } finally {
        $file->close();
    }
    
    printf("Read data from file: %s\n", $data);
    
    // Remove file.
    yield File\unlink($path);
})->done();

Loop\run();