icicleio/filesystem

Icicle 的异步文件系统组件。

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

This package is auto-updated.

Last update: 2024-09-14 03:33:35 UTC


README

始终非阻塞的异步文件系统访问,无需扩展。

该库是 Icicle 的一个组件,提供异步文件系统函数,并将文件抽象为异步 。与其他 Icicle 组件类似,该库使用从 协程可等待对象生成器 构建的工具,使编写异步代码更像编写同步代码。

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

文档和支持

需求
  • PHP 5.5+ 用于 v0.1.x 分支(当前稳定版)和 v1.x 分支(镜像当前稳定版)
  • PHP 7 用于 v2.0 分支(开发中)支持生成器委托和返回表达式
安装

推荐的安装方法是使用 Composer 包管理器。(有关安装和使用 Composer 的信息,请参阅 Composer 安装指南。)

运行以下命令以在项目中使用此库

composer require icicleio/filesystem

您还可以手动编辑 composer.json 以将此库添加为项目依赖项。

// composer.json
{
    "require": {
        "icicleio/filesystem": "^0.1"
    }
}
建议
  • eio 扩展:使用 libeio 提供异步文件访问(需要 v1.2.6+)。

示例

#!/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();