一些文件工具

v0.1.1 2021-02-15 16:49 UTC

This package is auto-updated.

Last update: 2024-09-09 21:00:09 UTC


README

使用此库来操作和交互文件系统。
注意。一些函数或类无法与Windows系统交互,因此请谨慎使用。

警告:OnFileChange类需要reactphp/event-loop才能工作,但在composer配置中未将其作为必需项添加,以避免强迫普通用户包含他们永远不会使用的库,因此在使用此类时,请务必在安装程序时包含它。

目录

OnFileChange

OnFileChange 是一个用于监控文件何时被修改并据此采取某些操作的辅助类。

请阅读以下说明

此类支持通过将 brunonatali/tools 包含到您的composer项目中来进行调试。

注意:OnFileChange 类需要 reactphp/event-loop 来工作,请手动将其包含到项目中!

性能:为了性能目的,请安装 inotify PECL 扩展并将 brunonatali/inotify 包含到您的项目中。

轮询示例

use BrunoNatali\File\OnFileChange;

use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();

$myFuncToCall = function () {
    echo "File changed!";
};

try {
    $onFileChange = new OnFileChange(
        '/my/path/to.file', 
        $loop, 
        $myFuncToCall,
        /**
         * You can pass an configuration array to force polling or configure polling time
         * but generally system will use polling if brunonatali/inotify was not found and
         * a 1 sec polling time as default
        */
        [
            'force_ppolling' => true,
            'polling_time' => 1.0 // check for file changes every 1 sec
        ]
    );
} catch ($e \Exception) {
    /**
     * Exception codes:
     * ERROR_FILE_NAME_ABSENT -> file name not provided 
     * ERROR_FILE_CALL_ABSENT -> no callable function
     * ERROR_FILE_LOOP_ABSENT -> unknown LoopInterface
     * ERROR_FILE_NOT_EXIST -> non existent file
    */
}

Inotify示例

通过在项目中输入以下命令安装 brunonatali/inotify

composer require brunonatali/inotify
// Inotify is automatically included if available
use BrunoNatali\File\OnFileChange;

use React\EventLoop\Factory as LoopFactory;

$loop = LoopFactory::create();

$myFuncToCall = function () {
    echo "File changed!";
};

try {
    $onFileChange = new OnFileChange('/my/path/to.file', $loop, $myFuncToCall);
} catch ($e \Exception) {
    /**
     * Exception codes:
     * ERROR_FILE_NAME_ABSENT -> file name not provided 
     * ERROR_FILE_CALL_ABSENT -> no callable function
     * ERROR_FILE_LOOP_ABSENT -> unknown LoopInterface
     * ERROR_FILE_NOT_EXIST -> non existent file
    */
}

OFC配置

配置以数组格式在 OnFileChange() 的第4个参数中传递,如下所示

$config = [
    'client_name' => 'FILE-X', // Desired app name when included and using debug mode []
    'auto_start' => true, // Tell if file monitoring will start ASAP as initialized. If false, start() is necessary
    'force_ppolling' => false, // Force polling mothod instead inotify
    'polling_time' => 1.0, // Check file change period when rining on polling method
    /**
     * Provide an Inotify constant to filter specific attr change
     *   NOTE 1. To specify more than one constant place each one in array (see below). 
     *   NOTE 2. 'specific_attr' has no effect when running on polling method
    */
    'specific_attr' => false // Pasing a single flag as simple like this => IN_MODIFY
];

// https://php.ac.cn/manual/en/inotify.constants.php
$inotifyFilters = [
    IN_ACCESS,
    IN_MODIFY 
];

OFC start()

用于开始监视文件更改。仅在停止或使用 'auto_start' => false 初始化时才有效。
此函数不返回任何内容。

OFC stop()

停止文件更改验证。此函数不返回任何内容。

OFC setPollingTime()

使用此方法配置轮询时间。

/**
 * Set file verification for every 10 sec.
 * Returns FALSE if is not configured to use polling method or can`t stops running timer
*/
$onFileChange->setPollingTime(10.0);

OFC static isFileChanged()

您可以通过调用 isFileChanged() 手动检查文件更改。此函数以静态方式提供,可以手动调用

$file = '/my/path/to.file';
$lastModifiedDate = null;

while (true) {
    if (OnFileChange::isFileChanged($file, $lastModifiedDate))
        echo "File checked manually & was changed!";
    sleep(5);
}

JsonFile

此类提供静态函数以易于交互,旨在易于操作/创建 JSON 文件。

readAsArray()

将整个 JSON 文件作为数组读取。此函数旨在简化使用原生的 PHP 函数 file_get_contents() 和 json_decode(),并添加一些验证。
简单调用

$jsonArray = \BrunoNatali\File\JsonFile\readAsArray('\my\path\to\file.json');

/**
 * You can add paths to search desired file.
 * Than file will be searched in every provided paths and than readed
*/
$anotherJsonArray = \BrunoNatali\File\JsonFile\readAsArray('file.json', '\my\path\one', '\my\path\two');
}

saveArray()

将提供的数组保存到 JSON 文件中,返回一个布尔成功结果。
您可以提供 PHP JSON 标志

$dataArray = [
    'simple-obj' => [
        1,2,3,4,5
    ]
];
/**
 * Comom use
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray);

/**
 * Overwrite destiny if exists
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray, true);

/**
 * Adding paths to search desired write file
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('file.json', $dataArray, true, '\my\path\one', '\my\path\two');

/**
 * Adding flags to PHP JSON function
*/
$jsonArray = \BrunoNatali\File\JsonFile\saveArray('\my\path\to\file.json', $dataArray, JSON_PRETTY_PRINT | JSON_HEX_TAG);

FileHandler

文件处理器被设计为一个流文件读取器,但尚未进行审查和文档化。

安装

推荐通过 Composer 安装此库。 对 Composer 不熟悉?

此项目遵循 SemVer。这将安装最新的支持版本

$ composer require brunonatali/file:^0.1

本项目旨在在Linux上运行,需要其他组件以及inotify PHP扩展,才能正常运行。请按照每个部分的说明操作,获取您所需的内容。
如果您发现任何错误,请报告。

许可证

MIT许可,请参阅许可证文件