Giter VIP home page Giter VIP logo

resource-watcher's Introduction

A simple resource watcher for getting changes of your filesystem.

Build Status Latest Stable Version

Installation

Use Composer to install this package:

composer require yosymfony/resource-watcher

How to use?

This package uses Symfony Finder to set the criteria to discover file changes.

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"

Finding changes

Every time the method findChanges() of the class ResourceWatcher is invoked, it returns an object type ResourceWatcherResult with information about all the changes producced in the filesystem. The ResourceWatcherResult class has the following methods:

  • getNewFiles(): Returns an array with the paths of the new resources.
  • getDeteledFiles(): Returns an array with the paths of deleted resources.
  • getUpdatedFiles(): Returns an array with the paths of the updated resources.
  • hasChanges(): Are they changes in your resources?.

Hashing alternatives

Two hashing classes are included in the package:

  • Yosymfony\ResourceWatcher\Crc32ContentHash, which hashes the content of the file
  • Yosymfony\ResourceWatcher\Crc32MetaDataHash, which hashes the filename and its last modified timestamp

Rebuild cache

To rebuild the resource cache uses rebuild() method of the class ResourceWatcher.

Relative paths with the resource cache

Using relative paths with the resource cache is possible thanks to the ResourceWatcher's method enableRelativePathWithCache:

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

The ResourceWatcherResult object will has relative paths.

Unit tests

You can run the unit tests with the following command:

$ composer test

resource-watcher's People

Contributors

arnaudligny avatar erikgaal avatar prudloff-insite avatar seregazhuk avatar yosymfony avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

resource-watcher's Issues

Compatibility with Symfony 6

This library can not be used with Symfony 6:

yosymfony/resource-watcher v3.0.0 requires symfony/finder ^2.7|^3.0|^4.0|^5.0 -> found symfony/finder[v2.7.0, ..., v2.8.52, v3.0.0, ..., v3.4.47, v4.0.0, ..., v4.4.44, v5.0.0, ..., v5.4.27] but the package is fixed to v6.3.5 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.

[Bug] Faulty detection logic if both file creation and deletion happens between detection calls

Hi!

The logic behind change detection is flawed.
It becomes an issue if both file deletion and creation happens between two 'findChanges' call.
(Theoretically it can be prevented by polling for changes fast enough, but I can't rely on it, since many file operations can happen even in a few milliseconds, and polling can be expensive.)

Problematic source part:

if (count($finderFileHashes) > count($cacheFileHashes)) {
foreach ($finderFileHashes as $file => $hash) {
$this->processFileFromFilesystem($file, $hash);
}
} else {
foreach ($cacheFileHashes as $file => $hash) {
$this->processFileFromCache($file, $hash);
}
}

Based just on the file count, it is not possible to detect all 3 cases (addition, deletion, updates) at once.
The code should compare differences from the perspective of both of them.
- Cache compared to actual files
- Actual files compared to cache

The library wrongly assumes that if:
"Current count of files" > "file count in cache", then only these two could have happened:

  • Any/some file was updated
  • New file has been added

AND IF

"Current count of files" <= "file count in cache", then only these two could have happened:

  • Any/some file was updated
  • Some files were deleted

Examples:

1. Library initialized
2. Files in folder: ['a.txt', 'b.txt', 'c.txt']
3. Delete: 'c.txt'
4. Add: 'd.txt'
5. Detect changes

In this case, the library will report that 'c.txt' has been deleted, but won't report that 'd.txt' have been added.

Another similar example:

1. Library initialized
2. Files in folder: ['a.txt', 'b.txt', 'c.txt']
3. Delete: 'c.txt'
4. Add: 'd.txt'
5. Add: 'e.txt'
6. Detect changes

Now the library will report that 'd.txt' and 'e.txt' have been added, but won't report that 'c.txt' have been deleted.

I'm attaching a test script with the example scenario.

index.zip

Cpu usage goes higher and higher

I'm using your excellent package to power my phpunit-watcher tool.

Unfortunately when leaving the watcher running for a couple of minutes, the cpu usage begins to ramp up. It starts very early on, there's about 1% more cpu usage each 2 or 3 seconds.

This is the code that's being looped. If I comment that out, the cpu stays at a stable level.

Do you know what's the cause of the ramp up in cpu usage and how this can be fixed?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.