DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Path Class
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
class Path implements IteratorAggregate{
const SEPARATOR = DIRECTORY_SEPARATOR;
const PATH_SEPARATOR = PATH_SEPARATOR;
protected $path;
public function __construct($isURL = false){
$this->path = $this->join(func_get_args());
}
public static function create(){
return new self(func_get_args());
}
public static function setCurrentFolder($n){
chdir($n);
return new Path($n);
}
public static function getCurrentFolder(){
return new Path(getcwd());
}
public function deleteFolder($recursive = false){
$empty = array($this->path);
if($recursive)
for($s = self::SEPARATOR, $stack = array($this->path); $d = array_pop($stack);){
if(is_dir($d) && !($h = dir($d)))
throw new Exception('Error while removing folder');
while(false !== $name = $h->read()){
if($name == '.' || $name == '..')
continue;
if(is_dir($path = $h->path . $s . $name))
array_push($stack, $path) && array_push($empty, $path);
elseif(!unlink($path))
throw new Exception('Error while removing folder');
}
$h->close();
}
for($i = count($empty); $i--;)
if(!rmdir($empty[$i]))
throw new Exception('Error while removing folder');
return new Path($this);
}
public function getIterator(){
return new RecursiveDirectoryIterator($this->path);
}
public function createFolder($recursive = false, $permissions){
for($d = self::SEPARATOR, $l = count($dir = preg_split('/[' . preg_quote('/\\' . $d, '/') . ']/', $dir)), $i = $l; $i-- && (!strlen($dir[$i]) || !is_dir(implode($d, array_slice($dir, 0, $i + 1)))););
while(++$i < $l)
if(!is_dir($path = implode($d, array_slice($dir, 0, $i + 1))) && (!mkdir($path, $permissions) || !chmod($path, $permissions)))
throw new Exception('Error while creating folder');
return new Path($this);
}
public function createTemporaryFile($permissions){
return tmpfile();
}
public function getTemporaryFilename($prefix = ''){
return new Path(tempnam($this->path, $prefix));
}
public function exists(){
return file_exists($this->path);
}
public function copy($destiny){
copy($this->path, $destiny);
return new Path($destiny);
}
public function delete($checkExists = true){
if(!$checkExists || $this->exists())
unlink($this->path);
return new Path($this);
}
public function rename($destiny){
rename($this->path, $destiny);
return new Path($destiny);
}
public function setModificationDate($date){
$date > ($a = $this->getAccessDate()) && $a = $date;
return touch($this->path, $date, $a);
}
public function getModificationDate(){
return filemtime($this->path);
}
public function getAccessDate(){
return fileatime($this->path);
}
public function setAccessDate($date){
$date < ($m = $this->getModificationDate()) && $m = $date;
return touch($this->path, $m, $date);
}
public function setPermissions($v){
return chmod($this->path, $v);
}
public function getPermissions(){
return fileperms($this->path);
}
public function isFolder(){
return is_dir($this->path);
}
public function isFile(){
return is_file($this->path);
}
public function isReadable(){
return is_readable($this->path);
}
public function isWritable(){
return is_writable($this->path);
}
public function getSize(){
return +sprintf("%u", filesize($this->path));
}
public function getRealPath(){
return new Path(realpath($this->path));
}
public function getFilename(){
return new Path(basename($this->path));
}
public function getFolder(){
return new Path(dirname($this->path));
}
public function getExtension(){
return pathinfo($this->path, PATHINFO_EXTENSION);
}
public function getPath(){
return $this->path;
}
public function setPath($path){
$this->path = $path;
return $this;
}
public static function join($a, $b = null){
$c = array(__CLASS__, __METHOD__);
if(count(func_get_args()) < 2)
return array_reduce($a, $c);
is_array($a) && $a = array_reduce($a, $c);
is_array($b) && $b = array_reduce($b, $c);
return ($a !== null ? trim($a, self::SEPARATOR) . self::SEPARATOR : '') . trim($b, self::SEPARATOR);
}
public function __toString(){
return $this->path;
}
}





