doudouxuqh198138 2019-05-22 15:43
浏览 74
已采纳

如何使用YoSymfony的ResourceWatcher包?

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 wich 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();
  • 写回答

1条回答 默认 最新

  • duanjiaopi8218 2019-05-23 14:14
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集