/** * Prepare a file for writing * * @param string $path An absolute path to the file to write * @param octal $dirPerms Permissions for new directories * @return boolean|string * @return boolean:true The path is ready for writing * @return string An error message indicating where the prepare failed */ public static function prepare($path, $dirPerms = 0775) { if (file_exists($path)) { return Path::test($path, self::WRITABLE); } return Dir::prepare(dirname($path), $dirPerms); }
/** * {@inheritdoc} */ public function validate() { $path = Path::normalize($this->value); $test = $this->isDir === true ? Dir::isWritable($path) : File::isWritable($path); if ($test === true) { $this->value = $path; return true; } $this->message = $test; return false; }
/** * Find empty directories * * @param string $dir An absolute path to the directory to search * @param integer $options Iterator options * @return array<string> */ public static function emptyDirectories($dir, $options = 0) { $ret = []; $iterator = self::getIterator($dir, $options); foreach ($iterator as $file) { if ($iterator->isDot() === false && $file->isDir() && ($path = $file->getRealPath()) && !array_key_exists($path, $ret) && Dir::isEmpty($path)) { $ret[$path] = 1; } } return array_keys($ret); }
/** * {@inheritdoc} */ public function validate() { if (($test = Dir::isWritable($this->value)) !== true) { $this->message = $test; return false; } else { if (file_exists($this->value) && $this->mustBeEmpty === true && count(scandir($this->value)) !== 2) { $this->message = "'{$this->value}' must be an empty directory"; return false; } } return true; }
/** * @param string $path Absolute filesystem path to remove * @param boolean|null * @return boolean True if the path no longer exists, false if it does */ private static function removePath($path, $isDir = null) { if (file_exists($path)) { $isDir = $isDir === null ? is_dir($path) : $isDir; $result = $isDir === true ? Dir::remove($path) : @unlink($path); return $result === true; } return true; }
/** * Recursively copy a directory * * @param string $source The absolute directory path to copy from * @param string $dest The absolute directory path to copy to * @param octal $mode Permissions for newly created directories * @return boolean|string * @return boolean:true The copy was successful * @return string An error message describing the failure */ public static function copy($source, $dest, $mode = 0775) { if (($test = Dir::prepare($dest, $mode)) !== true) { return "failed to copy '{$source}' to '{$dest}'; {$test}"; } else { if (Dir::isEmpty($dest) === false) { return "failed to copy '{$source}'; '{$dest}' is not empty"; } } $iterator = Find::getIterator($source, Find::RECURSIVE); foreach ($iterator as $file) { $destPath = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); if ($file->isDir()) { mkdir($destPath, $mode); } else { copy($file, $destPath); } } return true; }