我正在尝试制作一个文件观察器,当您添加、更新或删除文件时,您可以在数据库中看到文件更新。我正在使用框架 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);
}
动漫人物