1
|
|
|
<?php |
2
|
|
|
namespace phootwork\file; |
3
|
|
|
|
4
|
|
|
class FileDescriptor { |
5
|
|
|
|
6
|
|
|
use FileOperationTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Creates a new FileDescriptor from SplFileInfo |
10
|
|
|
* |
11
|
|
|
* @param \SplFileInfo $fileInfo |
12
|
|
|
* @return FileDescriptor |
13
|
|
|
*/ |
14
|
|
|
public static function fromFileInfo(\SplFileInfo $fileInfo) { |
15
|
|
|
return new self($fileInfo->getPathname()); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function __construct($filename) { |
19
|
|
|
$this->init($filename); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Tells whether this is a regular file |
24
|
|
|
* |
25
|
|
|
* @return boolean Returns TRUE if the filename exists and is a regular file, FALSE otherwise. |
26
|
|
|
*/ |
27
|
|
|
public function isFile() { |
28
|
|
|
return is_file($this->pathname); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tells whether the filename is a '.' or '..' |
33
|
|
|
* |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
|
|
public function isDot() { |
37
|
|
|
return $this->getFilename() == '.' || $this->getFilename() == '..'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Tells whether this is a directory |
42
|
|
|
* |
43
|
|
|
* @return boolean Returns TRUE if the filename exists and is a directory, FALSE otherwise. |
44
|
|
|
*/ |
45
|
|
|
public function isDir() { |
46
|
|
|
return is_dir($this->pathname); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Converts this file descriptor into a file object |
51
|
|
|
* |
52
|
|
|
* @return File |
53
|
|
|
*/ |
54
|
|
|
public function toFile() { |
55
|
|
|
return new File($this->pathname); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Converts this file descriptor into a directory object |
60
|
|
|
* |
61
|
|
|
* @return Directory |
62
|
|
|
*/ |
63
|
|
|
public function toDirectory() { |
64
|
|
|
return new Directory($this->pathname); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Deletes the file |
69
|
|
|
*/ |
70
|
|
|
public function delete() { |
71
|
|
|
if ($this->isDir()) { |
72
|
|
|
$this->toDirectory()->delete(); |
73
|
|
|
} else { |
74
|
|
|
$this->toFile()->delete(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* String representation of this file as pathname |
80
|
|
|
*/ |
81
|
|
|
public function __toString() { |
82
|
|
|
return $this->pathname; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|