scaleplan/php-inotify

将php的inotify模块封装成对象,并使用事件分发器来调度它们。

v1.1.0 2019-09-16 19:40 UTC

This package is not auto-updated.

Last update: 2024-10-03 04:21:32 UTC


README

Build Status Scrutinizer Code Quality Code Coverage

原因

在需要扫描目录以查找新文件或文件修改的情况下,你可能会创建一些脚本并实现拉取机制。这对文件较少的小系统来说很好,但对于大型系统来说效率不够高。
这就是我们为什么得到了inotify机制,它可以生成文件|目录更改事件,如创建、删除、更改等,我们可以监听这些事件。更多信息请参阅php手册。 这里

安装

composer require krowinski/php-inotify

安装php的inotify扩展

要监听事件,我们需要一个名为inotify的php扩展。在大多数情况下,您只需使用pecl安装即可

例如,在debian|ubuntu上安装php 7.1

apt-get update
apt-get install php-pear make php7.1-dev
pecl install inotify 
echo "extension=inotify.so" >>  /etc/php/7.1/cli/php.ini

示例

您可以在example.php中找到示例,以及您可以监听的事件InotifyEventCodeEnum.php 事件实现Arrayable、JsonSerializable和__toString。

Array
(
    [id] => 1
    [eventCode] => 256
    [eventDescription] => ON_CREATE - File or directory created in watched directory
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 32
    [eventDescription] => ON_OPEN - File was opened
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 4
    [eventDescription] => ON_ATTRIB - Metadata changed (e.g. permissions, mtime, etc.)
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 8
    [eventDescription] => ON_CLOSE_WRITE - File opened for writing was closed
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 512
    [eventDescription] => ON_DELETE - File or directory deleted in watched directory
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610456
)

位置

[id] => watch descriptor
[eventCode] => bit mask of events
[eventDescription] => human readable event description (can be UNKNOWN if not found in InotifyEventCodeEnum)
[uniqueId] => is a unique id to connect related events (e.g. IN_MOVE_FROM and IN_MOVE_TO)
[fileName] => name of a file (e.g. if a file was modified in a watched directory)
[pathName] => watched resource you give in configuration
[customName] => custom resource name for external parsing like "form-upload-dir" etc
[pathWithFile] => helper that contact pathName and fileName
[timestamp] => ...