/
CleanCodeDir.php
executable file
·119 lines (97 loc) · 2.18 KB
/
CleanCodeDir.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
*@author Sérgio Eduardo Pinheiro Gomes <sergioeduardo1981@gmail.com>
*/
require_once 'CleanCodeClass.php';
class CleanCodeDir extends CleanCodeClass
{
private static $aliases = array
(
'controllers' => 'app/controllers/',
'models' => 'app/models/'
);
private $path = '';
private $subFolders = array();
public static function format($string)
{
return self::format_url($string, false);
}
public static function addAlias($alias, $path)
{
self::$aliases[$alias] = $path;
}
public static function get($alias)
{
return self::searchPos(self::$aliases, $alias, $alias);
}
function __construct($path)
{
$this->path = $path;
}
public function addSubFolders($folder)
{
$this->subFolders = func_get_args();
}
private function createSubFolders()
{
$ok = true;
foreach ($this->subFolders as $folder)
{
$dir = new self($this->getPath() . $folder);
if(!$dir->create()) $ok = false;
}
return $ok;
}
private function recursive_scan($dir, $funct, $only_dir)
{
if(is_dir($dir))
{
$ret = true;
foreach(scandir($dir) as $content)
{
if(!preg_match('/^(^\.)|(\.{1,})$/', $content))
{
$path = $dir . '/' . $content;
if(is_dir($path))
{
$this->recursive_scan($path, $funct, $only_dir);
}
else if($only_dir == false)
{
$ret = $funct($path, false);
}
}
}
return $ret && $funct($dir, true);
}
else return false;
}
public function scan($funct, $only_dir = false)
{
return $this->recursive_scan($this->path, $funct, $only_dir);
}
public function getPath()
{
return $this->path;
}
public function setPath($new_path)
{
if(rename($this->path, $new_path)) $this->path = $new_path;
}
public function rename_folder($oldFolder, $newFolder)
{
return $oldFolder == $newFolder || rename($this->path . '/' . $oldFolder, $this->path . '/' . $newFolder);
}
public function create($chmod = 0777)
{
return mkdir($this->path, $chmod, true) && (count($this->subFolders) == 0 || $this->createSubFolders());
}
public function delete()
{
return $this->scan(function($item, $isDir)
{
return $isDir? rmdir($item) : unlink($item);
});
}
}
?>