/**
 *
 * @param object $task
 * @param array $args
 */
function run_sfassetlibrary_synchronize($task, $args, $options)
{
    if (!count($args)) {
        sfAssetsLibraryTools::log('Usage: php symfony sfassetlibrary-synchronize [app] [dirname] --notVerbose --removeOrphanAssets --removeOrphanFolders');
        return;
    }
    $app = $args[0];
    if (!is_dir(sfConfig::get('sf_app_dir') . DIRECTORY_SEPARATOR . $app)) {
        throw new Exception('The app "' . $app . '" does not exist.');
    }
    if (!isset($args[1])) {
        throw new Exception('You must define a sychronization folder');
    }
    $base_folder = $args[1];
    $verbose = array_key_exists('notVerbose', $options) ? false : true;
    $removeOrphanAssets = array_key_exists('removeOrphanAssets', $options) ? true : false;
    $removeOrphanFolders = array_key_exists('removeOrphanFolders', $options) ? true : false;
    // define constants
    define('SF_ROOT_DIR', sfConfig::get('sf_root_dir'));
    define('SF_APP', $app);
    define('SF_ENVIRONMENT', 'dev');
    define('SF_DEBUG', true);
    // get configuration
    require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';
    // initialize database manager
    $databaseManager = new sfDatabaseManager();
    $databaseManager->initialize();
    $con = Propel::getConnection();
    // synchronize
    sfAssetsLibraryTools::log(sprintf("Comparing files from %s with assets stored in the database...", $base_folder), 'green');
    $rootFolder = sfAssetFolderPeer::getRoot();
    try {
        $rootFolder->synchronizeWith($base_folder, $verbose, $removeOrphanAssets, $removeOrphanFolders);
    } catch (sfAssetException $e) {
        throw new sfException(strtr($e->getMessage(), $e->getMessageParams()));
    }
    echo pakeColor::colorize("Synchronization complete\n", 'INFO');
}
Ejemplo n.º 2
0
 /**
  * Overrrides the parent synchronizeWith() method to add a fix when calling
  * insertAsLastChildOf($this->reload()) because $this->reload() doesn't return
  * $this.
  *
  * @see parent::synchronizeWith()
  */
 public function synchronizeWith($baseFolder, $verbose = true, $removeOrphanAssets = false, $removeOrphanFolders = false)
 {
     if (!is_dir($baseFolder)) {
         throw new sfAssetException(sprintf('%s is not a directory', $baseFolder));
     }
     $files = sfFinder::type('file')->maxdepth(0)->ignore_version_control()->in($baseFolder);
     $assets = $this->getAssetsWithFilenames();
     foreach ($files as $file) {
         if (!array_key_exists(basename($file), $assets)) {
             // File exists, asset does not exist: create asset
             $sfAsset = new sfAsset();
             $sfAsset->setFolderId($this->getId());
             $sfAsset->create($file, false);
             $sfAsset->save();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Importing file %s", $file), 'green');
             }
         } else {
             // File exists, asset exists: do nothing
             unset($assets[basename($file)]);
         }
     }
     foreach ($assets as $name => $asset) {
         if ($removeOrphanAssets) {
             // File does not exist, asset exists: delete asset
             $asset->delete();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Deleting asset %s", $asset->getUrl()), 'yellow');
             }
         } else {
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Warning: No file for asset %s", $asset->getUrl()), 'red');
             }
         }
     }
     $dirs = sfFinder::type('dir')->maxdepth(0)->discard(sfConfig::get('app_sfAssetsLibrary_thumbnail_dir', 'thumbnail'))->ignore_version_control()->in($baseFolder);
     $folders = $this->getSubfoldersWithFolderNames();
     foreach ($dirs as $dir) {
         list(, $name) = sfAssetsLibraryTools::splitPath($dir);
         if (!array_key_exists($name, $folders)) {
             $this->reload();
             // dir exists in filesystem, not in database: create folder in database
             $sfAssetFolder = new sfAssetFolder();
             $sfAssetFolder->insertAsLastChildOf($this);
             $sfAssetFolder->setName($name);
             $sfAssetFolder->save();
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Importing directory %s", $dir), 'green');
             }
         } else {
             // dir exists in filesystem and database: look inside
             $sfAssetFolder = $folders[$name];
             unset($folders[$name]);
         }
         $sfAssetFolder->synchronizeWith($dir, $verbose, $removeOrphanAssets, $removeOrphanFolders);
     }
     foreach ($folders as $name => $folder) {
         if ($removeOrphanFolders) {
             $folder->delete(null, true);
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Deleting folder %s", $folder->getRelativePath()), 'yellow');
             }
         } else {
             if ($verbose) {
                 sfAssetsLibraryTools::log(sprintf("Warning: No directory for folder %s", $folder->getRelativePath()), 'red');
             }
         }
     }
 }