|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\FileSystem; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Library\Core\Exception\FileSystem\FileNotFoundException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Directory helper. |
|
18
|
|
|
* |
|
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
20
|
|
|
* @package WBW\Library\Core\IO |
|
21
|
|
|
*/ |
|
22
|
|
|
class DirectoryHelper { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a directory. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $dirname The directory name. |
|
28
|
|
|
* @param int $mode The directory mode. |
|
29
|
|
|
* @param boolean $recursive Recursive ? |
|
30
|
|
|
* @return boolean Returns true in case of success, false otherwise or null if the directory exists. |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function create($dirname, $mode = 0755, $recursive = false) { |
|
33
|
|
|
if (true === file_exists($dirname)) { |
|
34
|
|
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
return mkdir($dirname, $mode, $recursive); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Delete a directory. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $dirname The directory name. |
|
43
|
|
|
* @return boolean Returns true in case of success, false otherwise or null if the directory can't be deleted. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function delete($dirname) { |
|
46
|
|
|
if (true !== self::isEmpty($dirname)) { |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
return rmdir($dirname); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Determines if a directory is empty. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $dirname The directory name. |
|
56
|
|
|
* @return boolean Returns true in case of success, false otherwise or null if the directory is not readable. |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function isEmpty($dirname) { |
|
59
|
|
|
if (false === is_readable($dirname)) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
return (count(scandir($dirname)) == 2); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Rename a directory. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $oldDirname The old directory name. |
|
69
|
|
|
* @param string $newDirname The new directory name. |
|
70
|
|
|
* @return boolean Returns true in case of success, false otherwise or null if the new directory name already exists. |
|
71
|
|
|
* @throws FileNotFoundException Throws a file not found exception if the directory does not exists. |
|
72
|
|
|
* @see FileHelper |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function rename($oldDirname, $newDirname) { |
|
75
|
|
|
return FileHelper::rename($oldDirname, $newDirname); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|