Skip to content

Commit

Permalink
dirhandler improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Nov 24, 2018
1 parent 777e9ed commit 9e4662a
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions src/FileSystem/DirHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,37 @@
* @package doganoo\PHPUtil\FileSystem
*/
class DirHandler {
private $path = null;

/**
* DirHandler constructor.
* @param string $path
*/
public function __construct(string $path) {
$this->setPath($path);
}

/**
* @return string
*/
public function getPath(): string {
return $this->path;
}

/**
* @param string $path
*/
public function setPath(string $path) {
$this->path = $path;
}

/**
* @param string $fileName
* @return string
*/
public function findFile(string $fileName) {
return $this->_findFile($this->path, $fileName);
}

/**
* finds a file in the given dir
Expand All @@ -39,7 +70,7 @@ class DirHandler {
* @param $fileName
* @return string
*/
public function findFile(string $dirName, string $fileName) {
private function _findFile(string $dirName, string $fileName) {
$dirs = glob($dirName . '*');
$file = "";
foreach ($dirs as $d) {
Expand All @@ -57,7 +88,7 @@ public function findFile(string $dirName, string $fileName) {
return $dirName . "/" . $pathInfo["basename"];
}
} else if (is_dir($d)) {
$tmp = $this->findFile($d . "/", $fileName);
$tmp = $this->_findFile($d . "/", $fileName);
if ($tmp != "") {
$file = $tmp;
}
Expand All @@ -66,6 +97,33 @@ public function findFile(string $dirName, string $fileName) {
return $file;
}

/**
* whether the dir is readable
*
* @return bool
*/
public function isReadable(): bool {
return \is_dir($this->path) && \is_readable($this->path);
}

/**
* whether the dir is writable
*
* @return bool
*/
public function isWritable(): bool {
return \is_dir($this->path) && \is_writable($this->path);
}

/**
* lists every item in a given dir
*
* @return array
*/
public function list(): array {
return $this->_list($this->path);
}

/**
* lists every item in a given dir
*
Expand All @@ -74,16 +132,15 @@ public function findFile(string $dirName, string $fileName) {
* @param string $path
* @return array
*/
function list(string $path): array {
function _list(string $path): array {
$result = [];
$scan = glob($path . '/*');
foreach ($scan as $item) {
if (is_dir($item)) {
$result[basename($item)] = $this->list($item);
$result[basename($item)] = $this->_list($item);
} else {
$result[] = basename($item);
}

}
return $result;
}
Expand Down

0 comments on commit 9e4662a

Please sign in to comment.