你如何使用 YoSymfony 的 ResourceWatcher 包?

我正在尝试制作一个文件观察器,当您添加、更新或删除文件时,您可以在数据库中看到文件更新。我正在使用框架 Symfony4 和来自 YoSymfony 的一个名为 ResourceWatcher 的包。该包使用来自 Symfony 的 Finder 包来查找指定目录中的文件,然后观察器比较缓存和新文件以查看是否有任何更改。当我使用返回路径数组的观察者的方法时,当我尝试查看该数组时,它返回 null。我应该如何使用这些方法及其回报?


我把var_dump到处都放了,看问题出在findChanges()->getUpdatedFiles()和getNewFiles();


//旧代码


    $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(); 

    }

我希望能够看到路径,将它们转换为字符串并将其用于我的其他方法以使数据出现在我的数据库中。


在我的 var_dump 中,我在终端中得到 NULL。


编辑:[first_name] 在我的 csv 文件中,您可以直接转储 $paths。


//新代码


    $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);

    }



阿晨1998
浏览 128回答 1
1回答

动漫人物

因此,一旦您使用 findChanges()->hasChanges() 方法,它就会告诉观察者有一些更改,但随后它会重置并且观察者中不再有任何更改,因此使用 $paths = 毫无意义$watcher->findChanges()->getUpdatedFiles(); 因为它总是因为重置而不会返回任何内容。我必须创建一个包含内部更改的变量,以便我可以进一步重复使用这些更改。
打开App,查看更多内容
随时随地看视频慕课网APP