public function sync(FilesystemManager $manager, $filesystemName, IdentityInterface $identity = null, $syncPath = null) { //root folder sync if (empty($syncPath)) { $this->ensureRootFolder($filesystemName, $identity); $syncPath = ''; } $parentItem = $this->fetchByPath($filesystemName, $syncPath); if (!$parentItem) { throw new \Exception("sync: DB item for path '{$syncPath}' doesn't exist"); } $parentId = $parentItem['id']; $filesystem = $manager->get($filesystemName); $filesystemPaths = $filesystem->listPaths($syncPath); $dbFiles = $this->fetchAll(['filesystem' => $filesystemName, 'parent_id' => $parentId]); $toDeleteEntityIds = []; $dbPaths = []; foreach ($dbFiles as $dbFile) { $dbPaths[] = $dbFile['filesystem_path']; if ($dbFile['filesystem_path'] && !in_array($dbFile['filesystem_path'], $filesystemPaths)) { $toDeleteEntityIds[] = $dbFile['id']; } } $pathsToCreate = []; $dirPaths = []; foreach ($filesystemPaths as $path) { if ($filesystem->get($path) instanceof Directory) { $dirPaths[] = $path; } if (!in_array($path, $dbPaths)) { $pathsToCreate[] = $path; } } $deletedEntities = []; foreach ($toDeleteEntityIds as $id) { //$deletedEntities[] = $this->fetch($id); //$this->delete($id); $deletedEntities[] = $this->update($id, ['filesystem_error' => 'NOT_EXISTS']); } $createdEntities = []; foreach ($pathsToCreate as $path) { $createdEntities[] = $this->createFileEntityFromPath($manager, $filesystemName, $path, $identity); } $ret = ['created' => $createdEntities, 'deleted' => $deletedEntities]; foreach ($dirPaths as $dirPath) { $ret = array_merge($this->sync($manager, $filesystemName, $identity, $dirPath), $ret); } return $ret; }
/** * Create service * * @param ServiceLocatorInterface $serviceLocator * @return mixed */ public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator->get('Config'); if (empty($config['file-manager']) || empty($config['file-manager']['filesystem-manager'])) { throw new \Exception("\$config['file-manager']['filesystem-manager'] config needs to be set"); } $conf = $config['file-manager']['filesystem-manager']; //@todo refactor to lazy creation $ins = new FilesystemManager(); foreach ($conf['config'] as $handle => $options) { $filesystem = self::createFilesystem($options); $ins->setService($handle, $filesystem); } return $ins; }