1
2 //+ Jonas Raoni Soares Silva
3 //@ http://jsfromhell.com
4 class Path implements IteratorAggregate{
5 const SEPARATOR = DIRECTORY_SEPARATOR;
6 const PATH_SEPARATOR = PATH_SEPARATOR;
7
8 protected $path;
9
10 public function __construct($isURL = false){
11 $this->path = $this->join(func_get_args());
12 }
13 public static function create(){
14 return new self(func_get_args());
15 }
16 public static function setCurrentFolder($n){
17 chdir($n);
18 return new Path($n);
19 }
20 public static function getCurrentFolder(){
21 return new Path(getcwd());
22 }
23 public function deleteFolder($recursive = false){
24 $empty = array($this->path);
25 if($recursive)
26 for($s = self::SEPARATOR, $stack = array($this->path); $d = array_pop($stack);){
27 if(is_dir($d) && !($h = dir($d)))
28 throw new Exception('Error while removing folder');
29 while(false !== $name = $h->read()){
30 if($name == '.' || $name == '..')
31 continue;
32 if(is_dir($path = $h->path . $s . $name))
33 array_push($stack, $path) && array_push($empty, $path);
34 elseif(!unlink($path))
35 throw new Exception('Error while removing folder');
36 }
37 $h->close();
38 }
39 for($i = count($empty); $i--;)
40 if(!rmdir($empty[$i]))
41 throw new Exception('Error while removing folder');
42 return new Path($this);
43 }
44 public function getIterator(){
45 return new RecursiveDirectoryIterator($this->path);
46 }
47 public function createFolder($recursive = false, $permissions){
48 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)))););
49 while(++$i < $l)
50 if(!is_dir($path = implode($d, array_slice($dir, 0, $i + 1))) && (!mkdir($path, $permissions) || !chmod($path, $permissions)))
51 throw new Exception('Error while creating folder');
52 return new Path($this);
53 }
54 public function createTemporaryFile($permissions){
55 return tmpfile();
56 }
57 public function getTemporaryFilename($prefix = ''){
58 return new Path(tempnam($this->path, $prefix));
59 }
60 public function exists(){
61 return file_exists($this->path);
62 }
63 public function copy($destiny){
64 copy($this->path, $destiny);
65 return new Path($destiny);
66 }
67 public function delete($checkExists = true){
68 if(!$checkExists || $this->exists())
69 unlink($this->path);
70 return new Path($this);
71 }
72 public function rename($destiny){
73 rename($this->path, $destiny);
74 return new Path($destiny);
75 }
76 public function setModificationDate($date){
77 $date > ($a = $this->getAccessDate()) && $a = $date;
78 return touch($this->path, $date, $a);
79 }
80 public function getModificationDate(){
81 return filemtime($this->path);
82 }
83 public function getAccessDate(){
84 return fileatime($this->path);
85 }
86 public function setAccessDate($date){
87 $date < ($m = $this->getModificationDate()) && $m = $date;
88 return touch($this->path, $m, $date);
89 }
90 public function setPermissions($v){
91 return chmod($this->path, $v);
92 }
93 public function getPermissions(){
94 return fileperms($this->path);
95 }
96 public function isFolder(){
97 return is_dir($this->path);
98 }
99 public function isFile(){
100 return is_file($this->path);
101 }
102 public function isReadable(){
103 return is_readable($this->path);
104 }
105 public function isWritable(){
106 return is_writable($this->path);
107 }
108 public function getSize(){
109 return +sprintf("%u", filesize($this->path));
110 }
111 public function getRealPath(){
112 return new Path(realpath($this->path));
113 }
114 public function getFilename(){
115 return new Path(basename($this->path));
116 }
117 public function getFolder(){
118 return new Path(dirname($this->path));
119 }
120 public function getExtension(){
121 return pathinfo($this->path, PATHINFO_EXTENSION);
122 }
123 public function getPath(){
124 return $this->path;
125 }
126 public function setPath($path){
127 $this->path = $path;
128 return $this;
129 }
130 public static function join($a, $b = null){
131 $c = array(__CLASS__, __METHOD__);
132 if(count(func_get_args()) < 2)
133 return array_reduce($a, $c);
134 is_array($a) && $a = array_reduce($a, $c);
135 is_array($b) && $b = array_reduce($b, $c);
136 return ($a !== null ? trim($a, self::SEPARATOR) . self::SEPARATOR : '') . trim($b, self::SEPARATOR);
137 }
138 public function __toString(){
139 return $this->path;
140 }
141 }