/** * @see sfValidatorUrl */ protected function doClean($value) { $value = parent::doClean($value); if ($value !== dmOs::sanitizeDirName($value)) { throw new sfValidatorError($this, 'invalid', array('value' => $value)); } return $value; }
/** * Change folder name * * @param string $name */ public function rename($name) { if ($name === $this->name) { return $this; } if ($this->getNode()->isRoot()) { throw new dmException('The root folder cannot be renamed'); } if (!$this->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($this->fullPath))); } if (dmOs::sanitizeDirName($name) !== $name) { throw new dmException(sprintf('The target folder: "%s" contains incorrect characters.', $name)); } if ($this->getNode()->getParent()->hasSubFolder($name)) { throw new dmException(sprintf('The parent folder already contains a folder named "%s".', $name)); } $oldName = $this->getName(); $oldRelPath = $this->get('rel_path'); $newRelPath = $this->getNode()->getParent()->get('rel_path') . '/' . $name; $fs = $this->getService('filesystem'); $oldFullPath = $this->getFullPath(); $newFullPath = dmOs::join($this->getNode()->getParent()->getFullPath(), $name); if (!rename($oldFullPath, $newFullPath)) { throw new dmException('Can not move %s to %s', dmProject::unRootify($oldFullPath), dmProject::unRootify($newFullPath)); } $this->set('rel_path', $newRelPath); $this->clearCache()->save(); //update descendants if ($descendants = $this->getNode()->getDescendants()) { foreach ($descendants as $folder) { $folder->set('rel_path', dmString::str_replace_once($oldRelPath, $newRelPath, $folder->get('rel_path'))); $folder->save(); } } return $this; }
/** * Sanitize dirs name (move dirs) */ protected function sanitizeDir($dir) { if (basename($dir) != dmOs::sanitizeDirName(basename($dir))) { $renamedDir = dmOs::join(dirname($dir), dmOs::sanitizeDirName(basename($dir))); while (is_dir($renamedDir)) { $renamedDir = dmOs::randomizeDirName($renamedDir); } rename($dir, $renamedDir); $dir = $renamedDir; } return $dir; }
<?php require_once dirname(__FILE__) . '/helper/dmUnitTestHelper.php'; $helper = new dmUnitTestHelper(); $helper->boot(); $t = new lime_test(5); $t->is(dmOs::join("home"), "/home", 'dmOs::join /home'); $t->is(dmOs::join(sfConfig::get("sf_root_dir"), "/cache/"), sfConfig::get("sf_root_dir") . "/cache", 'dmOs::join sfConfig::get("sf_root_dir"), "/cache/"'); $t->is(dmOs::join("home", "user/dir", "file.ext"), "/home/user/dir/file.ext", 'dmOs::join "home", "user/dir", "file.ext"'); $t->is(dmOs::join("//home///user//dir/file.ext///"), "/home/user/dir/file.ext", 'dmOs::join "//home///user//dir/file.ext///"'); $t->is(dmOs::sanitizeDirName('A strange DIR name é & / \\ ( $'), $expected = 'A-strange-DIR-name-e', 'dmOs::sanitizeDirName() : ' . $expected);