linaphp/resource-watcher

使用Symfony Finder的简单资源监视器

v0.1.2 2024-04-07 16:24 UTC

This package is auto-updated.

Last update: 2024-09-07 17:08:21 UTC


README

这是一个用于获取文件系统变更的简单资源监视器。

** 这是对Yosymfony原始包的分支。 **

Build Status Latest Stable Version

安装

使用Composer安装此包

composer require linaphp/resource-watcher

如何使用?

此包使用Symfony Finder来设置发现文件变更的准则。

use Symfony\Component\Finder\Finder;
use LinaPhp\ResourceWatcher\Crc32ContentHash;
use LinaPhp\ResourceWatcher\ResourceWatcher;
use LinaPhp\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"

发现变更

每次调用类ResourceWatcherfindChanges()方法时,它返回一个类型为ResourceWatcherResult的对象,其中包含有关在文件系统中产生的所有变更的信息。`ResourceWatcherResult`类具有以下方法

  • getNewFiles():返回包含新资源路径的数组。
  • getDeletedFiles():返回包含已删除资源路径的数组。
  • getUpdatedFiles():返回包含更新资源路径的数组。
  • hasChanges():您的资源有变更吗?。

哈希替代方案

包中包含两个哈希类

  • LinaPhp\ResourceWatcher\Crc32ContentHash,用于哈希文件内容
  • LinaPhp\ResourceWatcher\Crc32MetaDataHash,用于哈希文件名及其最后修改时间戳

重建缓存

要重建资源缓存,请使用类ResourceWatcherrebuild()方法。

与资源缓存使用相对路径

由于ResourceWatcher的方法enableRelativePathWithCache,可以使用资源缓存与相对路径。

$watcher = new ResourceWatcher($resourceCache, $finder, $hashContent);
$watcher->enableRelativePathWithCache()

将具有相对路径。

单元测试

您可以使用以下命令运行单元测试

$ composer test