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