Skip to content

Commit

Permalink
remove dir
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Oct 10, 2019
1 parent 3886e1a commit ca4a5b1
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 32 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"autoload": {
"psr-4": {
"doganoo\\PHPUtil\\": "src/"
"doganoo\\PHPUtil\\": "src/",
"doganoo\\PHPUtil\\IntegrationTest\\": "test/Integration"
}
},
"require": {
Expand All @@ -22,6 +23,10 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "6.5"
"phpunit/phpunit": "^6"
},
"scripts": {
"unit-test": "./vendor/bin/phpunit test/Unit",
"integration-test": "php test/Integration/main.php"
}
}
20 changes: 11 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 60 additions & 20 deletions src/FileSystem/DirHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
Expand All @@ -25,12 +26,26 @@

namespace doganoo\PHPUtil\FileSystem;

use RecursiveDirectoryIterator;
use SplFileInfo;
use function fwrite;
use function is_dir;
use function is_readable;
use function is_writable;
use function mkdir;
use function pathinfo;
use function realpath;
use function time;
use function touch;
use function unlink;

/**
* Class DirHandler
*
* @package doganoo\PHPUtil\FileSystem
*/
class DirHandler {

public const DEFAULT_PERMISSION_MODE = 0770;
private $path = null;

Expand All @@ -48,7 +63,7 @@ public function __construct(string $path) {
* @return bool
*/
public function isReadable(): bool {
return $this->isDir() && \is_readable($this->path);
return $this->isDir() && is_readable($this->path);
}

/**
Expand All @@ -61,12 +76,12 @@ public function isDir(): bool {
}

/**
* @param int $mode
* @param int $mode
* @param bool $recursive
* @return bool
*/
public function mkdir(int $mode = DirHandler::DEFAULT_PERMISSION_MODE, bool $recursive = true): bool {
return \mkdir($this->getPath(), $mode, $recursive);
return mkdir($this->getPath(), $mode, $recursive);
}

/**
Expand All @@ -89,7 +104,7 @@ public function setPath(string $path) {
* @return bool
*/
public function isWritable(): bool {
return $this->isDir() && \is_writable($this->path);
return $this->isDir() && is_writable($this->path);
}

/**
Expand All @@ -111,7 +126,7 @@ public function list(): array {
*/
private function _list(string $path): array {
$result = [];
$scan = glob($path . '/*');
$scan = glob($path . '/*');
foreach ($scan as $item) {
if (is_dir($item)) {
$result[basename($item)] = $this->_list($item);
Expand All @@ -124,25 +139,25 @@ private function _list(string $path): array {

/**
* @param string $name
* @param bool $override
* @param bool $override
* @param string $content
* @return bool
*/
public function createFile(string $name, bool $override = false, string $content = null): bool {
if (!$this->exists()) return false;
if (!$override && $this->hasFile($name)) return true;
$path = $this->toRealPath();
$path = $this->toRealPath();
$filePath = $path . "/" . $name;
$touched = \touch($filePath, \time(), \time());
$touched = touch($filePath, time(), time());
if (null === $content) return $touched;
if (false === $touched) return false;
$handle = fopen($filePath, "w+");
if (false === $handle) {
$this->deleteFile($filePath);
return false;
}
$written = \fwrite($handle,$content);
if (false === $written){
$written = fwrite($handle, $content);
if (false === $written) {
$this->deleteFile($written);
return false;
}
Expand All @@ -155,14 +170,14 @@ public function createFile(string $name, bool $override = false, string $content
public function exists(): bool {
$path = $this->toRealPath();

return null !== $path && true === \is_dir($path);
return null !== $path && true === is_dir($path);
}

/**
* @return string|null
*/
public function toRealPath(): ?string {
$realpath = \realpath($this->path);
$realpath = realpath($this->path);
if (false === $realpath) return null;
return $realpath;
}
Expand All @@ -188,15 +203,15 @@ public function findFile(string $fileName): ?FileHandler {
*
* @param $dirName
* @param $fileName
* @return string
* @return FileHandler
*/
private function _findFile(string $dirName, string $fileName): ?FileHandler {
$dirs = glob($dirName . '*');
$file = null;
foreach ($dirs as $d) {
if (is_file($d)) {
$pathInfo = \pathinfo($d);
$pathInfo2 = \pathinfo($fileName);
$pathInfo = pathinfo($d);
$pathInfo2 = pathinfo($fileName);

if (isset($pathInfo2["extension"])) {
$condition = $pathInfo["basename"] === $pathInfo2["basename"];
Expand All @@ -207,10 +222,12 @@ private function _findFile(string $dirName, string $fileName): ?FileHandler {
if ($condition) {
return new FileHandler($dirName . "/" . $pathInfo["basename"]);
}
} else if (is_dir($d)) {
$tmp = $this->_findFile($d . "/", $fileName);
if (null !== $tmp) {
$file = $tmp;
} else {
if (is_dir($d)) {
$tmp = $this->_findFile($d . "/", $fileName);
if (null !== $tmp) {
$file = $tmp;
}
}
}
}
Expand All @@ -225,7 +242,30 @@ public function deleteFile(string $name): bool {
if (false === $this->exists()) return false;
if (false === $this->hasFile($name)) return false;
$path = $this->toRealPath();
return \unlink($path . "/" . $name);
return unlink($path . "/" . $name);
}

/**
* removes a directory in the base directory
*
* @param string|null $name
* @return bool
*/
public function rmdir(string $name = null): bool {
$iterator = new RecursiveDirectoryIterator(
$this->getPath()
);

/** @var SplFileInfo $item */
foreach ($iterator as $item) {
if (false === $item->isDir()) continue;
if ($item->getBasename() === $name) {
unlink($item->getRealPath());
return true;
}
}

return false;
}

}
15 changes: 15 additions & 0 deletions test/Integration/Base/ITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace doganoo\PHPUtil\IntegrationTest\Base;


interface ITest {

public function setUp(): void;

public function run(): bool;

public function tearDown(): void;

}
21 changes: 21 additions & 0 deletions test/Integration/FileSystem/DirHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace doganoo\PHPUtil\IntegrationTest;

use doganoo\PHPUtil\IntegrationTest\Base\ITest;

class DirHandler implements ITest {

public function setUp(): void {
// TODO: Implement setUp() method.
}

public function run(): bool {
// TODO: Implement run() method.
}

public function tearDown(): void {
// TODO: Implement tearDown() method.
}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion phpunit.xml → test/Unit/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
syntaxCheck="false">
<testsuites>
<testsuite name="PHPUtil Test Suite">
<directory suffix=".php">./test/</directory>
<directory suffix=".php">./</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit ca4a5b1

Please sign in to comment.