31
31
* @package doganoo\PHPUtil\FileSystem
32
32
*/
33
33
class DirHandler {
34
+ private $ path = null ;
35
+
36
+ /**
37
+ * DirHandler constructor.
38
+ * @param string $path
39
+ */
40
+ public function __construct (string $ path ) {
41
+ $ this ->setPath ($ path );
42
+ }
43
+
44
+ /**
45
+ * @return string
46
+ */
47
+ public function getPath (): string {
48
+ return $ this ->path ;
49
+ }
50
+
51
+ /**
52
+ * @param string $path
53
+ */
54
+ public function setPath (string $ path ) {
55
+ $ this ->path = $ path ;
56
+ }
57
+
58
+ /**
59
+ * @param string $fileName
60
+ * @return string
61
+ */
62
+ public function findFile (string $ fileName ) {
63
+ return $ this ->_findFile ($ this ->path , $ fileName );
64
+ }
34
65
35
66
/**
36
67
* finds a file in the given dir
@@ -39,7 +70,7 @@ class DirHandler {
39
70
* @param $fileName
40
71
* @return string
41
72
*/
42
- public function findFile (string $ dirName , string $ fileName ) {
73
+ private function _findFile (string $ dirName , string $ fileName ) {
43
74
$ dirs = glob ($ dirName . '* ' );
44
75
$ file = "" ;
45
76
foreach ($ dirs as $ d ) {
@@ -57,7 +88,7 @@ public function findFile(string $dirName, string $fileName) {
57
88
return $ dirName . "/ " . $ pathInfo ["basename " ];
58
89
}
59
90
} else if (is_dir ($ d )) {
60
- $ tmp = $ this ->findFile ($ d . "/ " , $ fileName );
91
+ $ tmp = $ this ->_findFile ($ d . "/ " , $ fileName );
61
92
if ($ tmp != "" ) {
62
93
$ file = $ tmp ;
63
94
}
@@ -66,6 +97,33 @@ public function findFile(string $dirName, string $fileName) {
66
97
return $ file ;
67
98
}
68
99
100
+ /**
101
+ * whether the dir is readable
102
+ *
103
+ * @return bool
104
+ */
105
+ public function isReadable (): bool {
106
+ return \is_dir ($ this ->path ) && \is_readable ($ this ->path );
107
+ }
108
+
109
+ /**
110
+ * whether the dir is writable
111
+ *
112
+ * @return bool
113
+ */
114
+ public function isWritable (): bool {
115
+ return \is_dir ($ this ->path ) && \is_writable ($ this ->path );
116
+ }
117
+
118
+ /**
119
+ * lists every item in a given dir
120
+ *
121
+ * @return array
122
+ */
123
+ public function list (): array {
124
+ return $ this ->_list ($ this ->path );
125
+ }
126
+
69
127
/**
70
128
* lists every item in a given dir
71
129
*
@@ -74,16 +132,15 @@ public function findFile(string $dirName, string $fileName) {
74
132
* @param string $path
75
133
* @return array
76
134
*/
77
- function list (string $ path ): array {
135
+ function _list (string $ path ): array {
78
136
$ result = [];
79
137
$ scan = glob ($ path . '/* ' );
80
138
foreach ($ scan as $ item ) {
81
139
if (is_dir ($ item )) {
82
- $ result [basename ($ item )] = $ this ->list ($ item );
140
+ $ result [basename ($ item )] = $ this ->_list ($ item );
83
141
} else {
84
142
$ result [] = basename ($ item );
85
143
}
86
-
87
144
}
88
145
return $ result ;
89
146
}
0 commit comments