/** * {@inheritdoc} * * @todo the call to setMessage will override custom message if one is set */ public function validate() { $path = Path::normalize($this->value); $test = Path::test($path, $this->bitmask); if ($test === true) { $this->value = $path; return true; } else { $this->setMessage($test); return false; } }
/** * Set the root temp directory (overrides use of the system temp directory) * * @param string|null $path * @return void * @throws InvalidArgumentException If the provided path isn't usable */ public static function setDir($path) { if ($path === null) { self::$dir = null; return; } if (!is_string($path)) { throw new InvalidArgumentException("invalid value provided for 'path'; " . "expecting an absolute directory path as string"); } else { if (($test = Path::test($path, Dir::READABLE_WRITABLE)) !== true) { throw new InvalidArgumentException("invalid value provided for 'path'; {$test}"); } } self::$dir = $path; }
/** * Combine prepare, chmod, and rename into a single step * * @param string $from An absolute path to the file before moving * @param string $to An absolute path to the file after moving * @param octal $fperm An octal to pass to chmod * @param octal $dperm New directory permissions * @return boolean|string All operations were successful * @return boolean:true All operations were successful * @return string Error message indicating which operation failed */ public static function rename($from, $to, $fperm = 0664, $dperm = 0775) { if (($test = Path::test($from, self::READABLE_WRITABLE)) !== true) { return $test; } else { if (($test = self::prepare($to, $dperm)) !== true) { return $test; } else { if (@rename($from, $to) === false) { return "failed to move '{$from}' to '{$to}'"; } else { if ($fperm !== null && @chmod($to, $fperm) === false) { return "failed to set permissions for '{$from}' to '{$fperm}'"; } } } } return true; }
/** * Recursively remove a directory * * @param string $dir An absolute path to a directory * @return boolean|string * @return boolean:true The directory was removed * @return string An error message describing the failure */ public static function remove($dir) { if (($test = Path::test($dir, Dir::READABLE_WRITABLE)) !== true) { return "failed to remove directory '{$dir}'; {$test}"; } $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . DIRECTORY_SEPARATOR . $file; $isDir = is_dir($path); $result = $isDir ? self::remove($path) : @unlink($path); if ($result !== true) { return $isDir ? $result : "failed to remove file '{$path}'"; } } return @rmdir($dir) !== true ? "failed to remove directory '{$dir}'" : true; }