|
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
|
6 |
|
public function read($directory) |
|
15
|
|
|
{ |
|
16
|
6 |
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); |
|
17
|
6 |
|
return $files; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
6 |
|
public function clean($directory) |
|
22
|
|
|
{ |
|
23
|
6 |
|
if (!is_dir($directory)) { |
|
24
|
|
|
$this->error("'$directory' is not an directory."); |
|
25
|
|
|
} |
|
26
|
6 |
|
foreach ($this->read($directory) as $fileInfo) { |
|
27
|
5 |
|
$this->checkPath($fileInfo->getPathName(), $directory); |
|
28
|
5 |
|
$this->cleanFile($fileInfo); |
|
29
|
6 |
|
} |
|
30
|
6 |
|
} |
|
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
|
5 |
|
private function cleanFile(\SplFileInfo $fileInfo) |
|
47
|
|
|
{ |
|
48
|
5 |
|
if ($fileInfo->isLink()) { |
|
49
|
3 |
|
$result = @unlink($fileInfo->getPathname()); |
|
50
|
3 |
|
if (!$result) { |
|
51
|
|
|
$this->error("Cannot delete symlink '{$fileInfo->getPathname()}'."); |
|
52
|
|
|
} |
|
53
|
5 |
|
} 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
|
5 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
5 |
|
private function checkPath($current, $directory) |
|
68
|
|
|
{ |
|
69
|
5 |
|
if (strpos($current, $directory) !== 0) { |
|
70
|
|
|
$this->error("Cannot access directory '$current' outside working directory '$directory'."); |
|
71
|
|
|
} |
|
72
|
5 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |