Search code examples
phpsymfony4file-watcher

How can you use the ResourceWatcher bundle from YoSymfony?


I am trying to make a file watcher where, when you add, update or delete a file, you can see the files updates in a database. I'm using the framework Symfony4 and a bundle from it called ResourceWatcher from YoSymfony. This bundle uses the Finder bundle from Symfony to find files in the directories specified and then, the watcher compares the cache and the new file to see if there are any changes. When I use a method with the watcher which returns a path array, when I try to see the array, it returns null. How am I suppose to use these methods and their returns?

I put the var_dump everywhere to see that the problem comes from the findChanges()->getUpdatedFiles() and getNewFiles();

//OLD CODE

    $finder = new Finder();
    $finder->files()
        ->name('*.csv')
        ->in('%kernel.root_dir%/../src/data/');

    //watcher
    $hashContent = new Crc32ContentHash();
    $resourceCache = new ResourceCachePhpFile('cache-key.php');
    $watcher = new ResourceWatcher($resourceCache, $finder, $hashContent);
    $watcher->initialize();

    if($watcher->findChanges()->hasChanges()){
        if($watcher->findChanges()->getNewFiles() === null){
            $paths = $watcher->findChanges()->getUpdatedFiles();
        }
        else{
            $paths = $watcher->findChanges()->getNewFiles();
        }

        $propertyAccessor = PropertyAccess::createPropertyAccessor();
        var_dump($propertyAccessor->getValue($paths, '[first_name]'));
        die(); 
    }

I'd like to be able to see the paths, convert them into string and use that into my other method to make the data appear in my database.

In my var_dump, I get NULL in terminal.

EDIT:[first_name] is in my csv-file, you can dump $paths directly.

//NEW CODE

    $finder = new Finder();
    $finder->files()
        ->name('*.csv')
        ->in('%kernel.root_dir%/../src/data/');

    //watcher
    $hashContent = new Crc32ContentHash();
    $resourceCache = new ResourceCachePhpFile('cache-key.php');
    $watcher = new ResourceWatcher($resourceCache, $finder, $hashContent);
    $watcher->initialize();

    $changes = $watcher->findChanges();
    
    if(!empty($changes->getUpdatedFiles())){
        $updatedFilesPath = $changes->getUpdatedFiles();
        $pathString = implode($updatedFilesPath);
        $reader = Reader::createFromPath($pathString);
    }
    elseif(!empty($changes->getNewFiles())){
        $newFilesPath = $changes->getNewFiles();
        $pathString = implode($newFilesPath);
        $reader = Reader::createFromPath($pathString);
    }
    else{
        return;
    }

    $results = $reader->fetchAssoc();

Solution

  • So it looks like that as soon as you use the method findChanges()->hasChanges(), it tells the watcher that there is some changes but then it resets and there's no changes anymore in the watcher so it's pointless to use $paths = $watcher->findChanges()->getUpdatedFiles(); since it will always return nothing because of the reset. I had to make a variable with the changes inside so that I could re-use the changes further down.

    Details in code...