Skip to content

Commit 9e4662a

Browse files
committed
dirhandler improvements
1 parent 777e9ed commit 9e4662a

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/FileSystem/DirHandler.php

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,37 @@
3131
* @package doganoo\PHPUtil\FileSystem
3232
*/
3333
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+
}
3465

3566
/**
3667
* finds a file in the given dir
@@ -39,7 +70,7 @@ class DirHandler {
3970
* @param $fileName
4071
* @return string
4172
*/
42-
public function findFile(string $dirName, string $fileName) {
73+
private function _findFile(string $dirName, string $fileName) {
4374
$dirs = glob($dirName . '*');
4475
$file = "";
4576
foreach ($dirs as $d) {
@@ -57,7 +88,7 @@ public function findFile(string $dirName, string $fileName) {
5788
return $dirName . "/" . $pathInfo["basename"];
5889
}
5990
} else if (is_dir($d)) {
60-
$tmp = $this->findFile($d . "/", $fileName);
91+
$tmp = $this->_findFile($d . "/", $fileName);
6192
if ($tmp != "") {
6293
$file = $tmp;
6394
}
@@ -66,6 +97,33 @@ public function findFile(string $dirName, string $fileName) {
6697
return $file;
6798
}
6899

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+
69127
/**
70128
* lists every item in a given dir
71129
*
@@ -74,16 +132,15 @@ public function findFile(string $dirName, string $fileName) {
74132
* @param string $path
75133
* @return array
76134
*/
77-
function list(string $path): array {
135+
function _list(string $path): array {
78136
$result = [];
79137
$scan = glob($path . '/*');
80138
foreach ($scan as $item) {
81139
if (is_dir($item)) {
82-
$result[basename($item)] = $this->list($item);
140+
$result[basename($item)] = $this->_list($item);
83141
} else {
84142
$result[] = basename($item);
85143
}
86-
87144
}
88145
return $result;
89146
}

0 commit comments

Comments
 (0)