-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectory.php
74 lines (60 loc) · 1.78 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Genesis\Commands\Filesystem;
use Genesis\Commands\Command;
/**
* @author Adam Bisek <[email protected]>
*/
class Directory extends Command
{
public function read($directory)
{
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
return $files;
}
public function clean($directory)
{
if (!is_dir($directory)) {
$this->error("'$directory' is not an directory.");
}
foreach ($this->read($directory) as $fileInfo) {
$this->checkPath($fileInfo->getPathName(), $directory);
$this->cleanFile($fileInfo);
}
}
public function create($dir, $chmod = '0777')
{
if (is_dir($dir)) {
$this->error("Dir '$dir' already exists.");
}
$result = mkdir($dir, $chmod, TRUE);
exec('chmod ' . escapeshellarg($chmod) . ' ' . escapeshellarg($dir)); // TODO: find workaround - native PHP chmod didnt work
if (!$result) {
$this->error("Cannot create dir '$dir'.");
}
}
private function cleanFile(\SplFileInfo $fileInfo)
{
if ($fileInfo->isLink()) {
$result = @unlink($fileInfo->getPathname());
if (!$result) {
$this->error("Cannot delete symlink '{$fileInfo->getPathname()}'.");
}
} elseif ($fileInfo->isDir()) {
$result = @rmdir($fileInfo->getPathname());
if (!$result) {
$this->error("Cannot delete file '{$fileInfo->getPathname()}'.");
}
} elseif ($fileInfo->isFile()) {
$result = @unlink($fileInfo->getPathname());
if (!$result) {
$this->error("Cannot delete file '{$fileInfo->getPathname()}'.");
}
}
}
private function checkPath($current, $directory)
{
if (strpos($current, $directory) !== 0) {
$this->error("Cannot access directory '$current' outside working directory '$directory'.");
}
}
}