krowinski / php-inotify
将php的inotify模块包装成对象,并使用事件分派器分发它们。
v2.1.0
2022-06-18 10:27 UTC
Requires
- php: >=7.4
- ext-inotify: *
- ext-json: *
- myclabs/php-enum: ^1.8
- symfony/event-dispatcher: ^4.0|^5.0|^6.0
- vistik/typed-collections: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-08-29 22:07:50 UTC
README
原因
在需要扫描目录以查找新文件或文件修改的情况下,你可能需要创建一些脚本并实现拉取机制。这对于文件较少的小系统来说很好,但对于大系统来说效率不高。
这就是为什么我们有了inotify机制,它可以生成文件|目录变化的诸如创建、删除、更改等事件,我们可以监听。更多信息请参阅php手册。 这里
安装
composer require krowinski/php-inotify
安装php的inotify扩展
为了监听事件,我们需要一个名为inotify的php扩展。在大多数情况下,您只需要使用pecl安装(例如,在 dockerfile 中)
示例
您可以在 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] => ...