Code Monkey home page Code Monkey logo

filesystem's Introduction

Filesystem

Evented filesystem access utilizing EIO.

Build Status Code Climate

Table of Contents

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

Introduction

Filesystem WIP for EIO, keep in mind that this can be very unstable at times and is not stable by a long shot!

Adapters

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

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->open('test.txt')->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 (\SplObjectStorage $list) {
   foreach ($list as $node) {
       echo $node->getPath(), PHP_EOL;
   }
});

License

React/Filesystem is released under the MIT license.

filesystem's People

Contributors

cboden avatar e3betht avatar wyrihaximus avatar

Watchers

 avatar  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.