Giter Site home page Giter Site logo

filesystem's Introduction

Filesystem

Build Status Code Climate

ReactPHP's evented asynchronous, non-blocking filesystem access library.

Table of Contents

  1. Introduction
  2. Adapters
  3. Examples
  4. Install
  5. License

Introduction

react/filesystem is a package to power your application with asynchronous, non-blocking filesystem access. Asynchronous access is enabled by various adapters described below.

Adapters

  • ChildProcessAdapter - Adapter using child processes to perform IO actions (default adapter if no extensions are installed)
  • EioAdapter - Adapter using ext-eio

Examples

Adding examples here over time.

Creating filesystem object

<?php

$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);

File object

<?php

$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);

$file = $filesystem->file(__FILE__); // Returns a \React\Filesystem\Node\FileInterface compatible object

Reading files

$filesystem->getContents('test.txt')->then(function($contents) {
});

Which is a convenience method for:

$filesystem->file('test.txt')->open('r')->then(function($stream) {
    return \React\Stream\BufferedSink::createPromise($stream);
})->then(function($contents) {
    // ...
});

Which in it's turn is a convenience method for:

$filesystem->file('test.txt')->open('r')->then(function ($stream) use ($node) {
    $buffer = '';
    $deferred = new \React\Promise\Deferred();
    $stream->on('data', function ($data) use (&$buffer) {
        $buffer += $data;
    });
    $stream->on('end', function ($data) use ($stream, $deferred, &$buffer) {
        $stream->close();
        $deferred->resolve(&$buffer);
    });
    return $deferred->promise();
});

Writing files

Open a file for writing (w flag) and write abcde to test.txt and close it. Create it (c flag) when it doesn't exists and truncate it (t flag) when it does.

$filesystem->file('test.txt')->open('cwt')->then(function ($stream) {
    $stream->write('a');
    $stream->write('b');
    $stream->write('c');
    $stream->write('d');
    $stream->end('e');
});

Directory object

<?php

$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);

$dir = $filesystem->dir(__DIR__); // Returns a \React\Filesystem\Node\DirectoryInterface compatible object

List contents

$filesystem->dir(__DIR__)->ls()->then(function (array $list) {
   foreach ($list as $node) {
       echo $node->getPath(), PHP_EOL;
   }
});

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require react/filesystem:^0.1.1

License

React/Filesystem is released under the MIT license.

filesystem's People

Contributors

wyrihaximus avatar jsor avatar seregazhuk avatar clue avatar carusogabriel avatar localheinz avatar cboden avatar e3betht avatar dmitrijnezh avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.