Skip to content

Commit ca4a5b1

Browse files
committed
remove dir
1 parent 3886e1a commit ca4a5b1

File tree

11 files changed

+115
-32
lines changed

11 files changed

+115
-32
lines changed

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"autoload": {
1313
"psr-4": {
14-
"doganoo\\PHPUtil\\": "src/"
14+
"doganoo\\PHPUtil\\": "src/",
15+
"doganoo\\PHPUtil\\IntegrationTest\\": "test/Integration"
1516
}
1617
},
1718
"require": {
@@ -22,6 +23,10 @@
2223
"ext-mbstring": "*"
2324
},
2425
"require-dev": {
25-
"phpunit/phpunit": "6.5"
26+
"phpunit/phpunit": "^6"
27+
},
28+
"scripts": {
29+
"unit-test": "./vendor/bin/phpunit test/Unit",
30+
"integration-test": "php test/Integration/main.php"
2631
}
2732
}

composer.lock

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FileSystem/DirHandler.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* MIT License
45
*
@@ -25,12 +26,26 @@
2526

2627
namespace doganoo\PHPUtil\FileSystem;
2728

29+
use RecursiveDirectoryIterator;
30+
use SplFileInfo;
31+
use function fwrite;
32+
use function is_dir;
33+
use function is_readable;
34+
use function is_writable;
35+
use function mkdir;
36+
use function pathinfo;
37+
use function realpath;
38+
use function time;
39+
use function touch;
40+
use function unlink;
41+
2842
/**
2943
* Class DirHandler
3044
*
3145
* @package doganoo\PHPUtil\FileSystem
3246
*/
3347
class DirHandler {
48+
3449
public const DEFAULT_PERMISSION_MODE = 0770;
3550
private $path = null;
3651

@@ -48,7 +63,7 @@ public function __construct(string $path) {
4863
* @return bool
4964
*/
5065
public function isReadable(): bool {
51-
return $this->isDir() && \is_readable($this->path);
66+
return $this->isDir() && is_readable($this->path);
5267
}
5368

5469
/**
@@ -61,12 +76,12 @@ public function isDir(): bool {
6176
}
6277

6378
/**
64-
* @param int $mode
79+
* @param int $mode
6580
* @param bool $recursive
6681
* @return bool
6782
*/
6883
public function mkdir(int $mode = DirHandler::DEFAULT_PERMISSION_MODE, bool $recursive = true): bool {
69-
return \mkdir($this->getPath(), $mode, $recursive);
84+
return mkdir($this->getPath(), $mode, $recursive);
7085
}
7186

7287
/**
@@ -89,7 +104,7 @@ public function setPath(string $path) {
89104
* @return bool
90105
*/
91106
public function isWritable(): bool {
92-
return $this->isDir() && \is_writable($this->path);
107+
return $this->isDir() && is_writable($this->path);
93108
}
94109

95110
/**
@@ -111,7 +126,7 @@ public function list(): array {
111126
*/
112127
private function _list(string $path): array {
113128
$result = [];
114-
$scan = glob($path . '/*');
129+
$scan = glob($path . '/*');
115130
foreach ($scan as $item) {
116131
if (is_dir($item)) {
117132
$result[basename($item)] = $this->_list($item);
@@ -124,25 +139,25 @@ private function _list(string $path): array {
124139

125140
/**
126141
* @param string $name
127-
* @param bool $override
142+
* @param bool $override
128143
* @param string $content
129144
* @return bool
130145
*/
131146
public function createFile(string $name, bool $override = false, string $content = null): bool {
132147
if (!$this->exists()) return false;
133148
if (!$override && $this->hasFile($name)) return true;
134-
$path = $this->toRealPath();
149+
$path = $this->toRealPath();
135150
$filePath = $path . "/" . $name;
136-
$touched = \touch($filePath, \time(), \time());
151+
$touched = touch($filePath, time(), time());
137152
if (null === $content) return $touched;
138153
if (false === $touched) return false;
139154
$handle = fopen($filePath, "w+");
140155
if (false === $handle) {
141156
$this->deleteFile($filePath);
142157
return false;
143158
}
144-
$written = \fwrite($handle,$content);
145-
if (false === $written){
159+
$written = fwrite($handle, $content);
160+
if (false === $written) {
146161
$this->deleteFile($written);
147162
return false;
148163
}
@@ -155,14 +170,14 @@ public function createFile(string $name, bool $override = false, string $content
155170
public function exists(): bool {
156171
$path = $this->toRealPath();
157172

158-
return null !== $path && true === \is_dir($path);
173+
return null !== $path && true === is_dir($path);
159174
}
160175

161176
/**
162177
* @return string|null
163178
*/
164179
public function toRealPath(): ?string {
165-
$realpath = \realpath($this->path);
180+
$realpath = realpath($this->path);
166181
if (false === $realpath) return null;
167182
return $realpath;
168183
}
@@ -188,15 +203,15 @@ public function findFile(string $fileName): ?FileHandler {
188203
*
189204
* @param $dirName
190205
* @param $fileName
191-
* @return string
206+
* @return FileHandler
192207
*/
193208
private function _findFile(string $dirName, string $fileName): ?FileHandler {
194209
$dirs = glob($dirName . '*');
195210
$file = null;
196211
foreach ($dirs as $d) {
197212
if (is_file($d)) {
198-
$pathInfo = \pathinfo($d);
199-
$pathInfo2 = \pathinfo($fileName);
213+
$pathInfo = pathinfo($d);
214+
$pathInfo2 = pathinfo($fileName);
200215

201216
if (isset($pathInfo2["extension"])) {
202217
$condition = $pathInfo["basename"] === $pathInfo2["basename"];
@@ -207,10 +222,12 @@ private function _findFile(string $dirName, string $fileName): ?FileHandler {
207222
if ($condition) {
208223
return new FileHandler($dirName . "/" . $pathInfo["basename"]);
209224
}
210-
} else if (is_dir($d)) {
211-
$tmp = $this->_findFile($d . "/", $fileName);
212-
if (null !== $tmp) {
213-
$file = $tmp;
225+
} else {
226+
if (is_dir($d)) {
227+
$tmp = $this->_findFile($d . "/", $fileName);
228+
if (null !== $tmp) {
229+
$file = $tmp;
230+
}
214231
}
215232
}
216233
}
@@ -225,7 +242,30 @@ public function deleteFile(string $name): bool {
225242
if (false === $this->exists()) return false;
226243
if (false === $this->hasFile($name)) return false;
227244
$path = $this->toRealPath();
228-
return \unlink($path . "/" . $name);
245+
return unlink($path . "/" . $name);
246+
}
247+
248+
/**
249+
* removes a directory in the base directory
250+
*
251+
* @param string|null $name
252+
* @return bool
253+
*/
254+
public function rmdir(string $name = null): bool {
255+
$iterator = new RecursiveDirectoryIterator(
256+
$this->getPath()
257+
);
258+
259+
/** @var SplFileInfo $item */
260+
foreach ($iterator as $item) {
261+
if (false === $item->isDir()) continue;
262+
if ($item->getBasename() === $name) {
263+
unlink($item->getRealPath());
264+
return true;
265+
}
266+
}
267+
268+
return false;
229269
}
230270

231271
}

test/Integration/Base/ITest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
4+
namespace doganoo\PHPUtil\IntegrationTest\Base;
5+
6+
7+
interface ITest {
8+
9+
public function setUp(): void;
10+
11+
public function run(): bool;
12+
13+
public function tearDown(): void;
14+
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace doganoo\PHPUtil\IntegrationTest;
4+
5+
use doganoo\PHPUtil\IntegrationTest\Base\ITest;
6+
7+
class DirHandler implements ITest {
8+
9+
public function setUp(): void {
10+
// TODO: Implement setUp() method.
11+
}
12+
13+
public function run(): bool {
14+
// TODO: Implement run() method.
15+
}
16+
17+
public function tearDown(): void {
18+
// TODO: Implement tearDown() method.
19+
}
20+
21+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

phpunit.xml renamed to test/Unit/phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
syntaxCheck="false">
1212
<testsuites>
1313
<testsuite name="PHPUtil Test Suite">
14-
<directory suffix=".php">./test/</directory>
14+
<directory suffix=".php">./</directory>
1515
</testsuite>
1616
</testsuites>
1717
</phpunit>

0 commit comments

Comments
 (0)