ahmadelsaeed / php8-resource-watcher
使用Symfony Finder的简单资源监视器
1.1.1
2022-08-17 19:52 UTC
Requires
- php: ^7.2|^8.0
- symfony/finder: ^2.7|^3.0|^4.0|^5.0|v6.1.3
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/filesystem: ^2.7|^3.0|^4.0|^5.0|v6.1.3
This package is auto-updated.
Last update: 2024-09-18 00:25:58 UTC
README
这是一个简单的文件系统更改资源监视器。
安装
使用Composer安装此包
composer require yosymfony/resource-watcher
如何使用?
此包使用Symfony Finder来设置发现文件更改的准则。
use Symfony\Component\Finder\Finder; use Yosymfony\ResourceWatcher\Crc32ContentHash; use Yosymfony\ResourceWatcher\ResourceWatcher; use Yosymfony\ResourceWatcher\ResourceCachePhpFile; $finder = new Finder(); $finder->files() ->name('*.md') ->in(__DIR__); $hashContent = new Crc32ContentHash(); $resourceCache = new ResourceCachePhpFile('/path-cache-file.php'); $watcher = new ResourceWatcher($resourceCache, $finder, $hashContent); $watcher->initialize(); // delete a file $result = $watcher->findChanges(); $result->getDeletedResources() // array of deleted filenames. e.g: "/home/yosymfony/README.md"
查找更改
每次调用类ResourceWatcher
的findChanges()
方法时,它都会返回一个类型为ResourceWatcherResult
的对象,其中包含关于文件系统中所有更改的信息。 ResourceWatcherResult
类有以下方法:
getNewFiles()
:返回一个包含新资源路径的数组。getDeletedFiles()
:返回一个包含已删除资源路径的数组。getUpdatedFiles()
:返回一个包含已更新资源路径的数组。hasChanges()
:您的资源有更改吗?
哈希替代方案
包中包含两个哈希类
Yosymfony\ResourceWatcher\Crc32ContentHash
,该类对文件内容进行哈希处理Yosymfony\ResourceWatcher\Crc32MetaDataHash
,该类对文件名及其最后修改时间戳进行哈希处理
重建缓存
要重建资源缓存,请使用类ResourceWatcher
的rebuild()
方法。
使用资源缓存进行相对路径
由于ResourceWatcher的方法enableRelativePathWithCache
,使用资源缓存中的相对路径是可能的
$watcher = new ResourceWatcher($resourceCache, $finder, $hashContent); $watcher->enableRelativePathWithCache()
ResourceWatcherResult
对象将具有相对路径。
单元测试
您可以使用以下命令运行单元测试
$ composer test