public function execute(DmMediaFolder $folder, $depth = 99) { if ($depth < 1) { return; } /* * Clear php filesystem cache * This will avoid some problems */ clearstatcache(); //$folder->refresh(true); list($dirs, $files) = $this->getDirContents($folder->getFullPath()); $medias = $folder->getDmMediasByFileName(); $children = $folder->getSubfoldersByName(); $dirty = false; /* * 1. Add new files to the medias */ foreach ($files as $file) { $fileBasename = basename($this->sanitizeFile($file)); if (!array_key_exists($fileBasename, $medias)) { // File exists, media does not exist: create media $this->mediaTable->create(array('dm_media_folder_id' => $folder->get('id'), 'file' => $fileBasename))->save(); $dirty = true; } else { // File exists, media exists: do nothing unset($medias[$fileBasename]); } } /* * 2. Remove medias which have no file */ foreach ($medias as $name => $media) { // File does not exist, media exists: delete media try { $media->delete(); } catch (Doctrine_Connection_Exception $e) { //A record needs this media, but the file has been removed :-/ } $dirty = true; } foreach ($dirs as $dir) { $dirName = basename($this->sanitizeDir($dir)); /* * Exists in fs, not in db */ if (!array_key_exists($dirName, $children)) { $subfolderRelPath = trim(dmOs::join($folder->get('rel_path'), $dirName), '/'); if ($child = $this->folderTable->findOneByRelPath($subfolderRelPath)) { // folder exists in db but is not linked to its parent $child->getNode()->moveAsLastChildOf($folder); $child->refresh(); $dirty = true; } else { // dir exists in filesystem, not in database: create folder in database $child = $this->folderTable->create(array('rel_path' => $subfolderRelPath)); $child->getNode()->insertAsLastChildOf($folder); $child->refresh(); $dirty = true; } } else { // dir exists in filesystem and database: do nothing $child = $children[$dirName]; unset($children[$dirName]); } $this->execute($child, $depth - 1); } /* * Not unsetted folders * don't exist in fs */ foreach ($children as $child) { try { $child->getNode()->delete(); } catch (Doctrine_Connection_Exception $e) { //A record needs a media in this folder, but the folder has been removed :-/ $this->filesystem->mkdir($child->getFullPath()); } $dirty = true; } if ($dirty) { $folder->clearCache()->refresh()->refreshRelated('Medias'); } }