我正在嘗試制作一個(gè)文件觀察器,當(dāng)您添加、更新或刪除文件時(shí),您可以在數(shù)據(jù)庫中看到文件更新。我正在使用框架 Symfony4 和來自 YoSymfony 的一個(gè)名為 ResourceWatcher 的包。該包使用來自 Symfony 的 Finder 包來查找指定目錄中的文件,然后觀察器比較緩存和新文件以查看是否有任何更改。當(dāng)我使用返回路徑數(shù)組的觀察者的方法時(shí),當(dāng)我嘗試查看該數(shù)組時(shí),它返回 null。我應(yīng)該如何使用這些方法及其回報(bào)?我把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(); }我希望能夠看到路徑,將它們轉(zhuǎn)換為字符串并將其用于我的其他方法以使數(shù)據(jù)出現(xiàn)在我的數(shù)據(jù)庫中。在我的 var_dump 中,我在終端中得到 NULL。編輯:[first_name] 在我的 csv 文件中,您可以直接轉(zhuǎn)儲(chǔ) $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); }
1 回答

動(dòng)漫人物
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
因此,一旦您使用 findChanges()->hasChanges() 方法,它就會(huì)告訴觀察者有一些更改,但隨后它會(huì)重置并且觀察者中不再有任何更改,因此使用 $paths = 毫無意義$watcher->findChanges()->getUpdatedFiles(); 因?yàn)樗偸且驗(yàn)橹刂枚粫?huì)返回任何內(nèi)容。我必須創(chuàng)建一個(gè)包含內(nèi)部更改的變量,以便我可以進(jìn)一步重復(fù)使用這些更改。
- 1 回答
- 0 關(guān)注
- 173 瀏覽
添加回答
舉報(bào)
0/150
提交
取消