forked from kzap/Eden-PHP-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolder.php
More file actions
257 lines (218 loc) · 6.38 KB
/
Folder.php
File metadata and controls
257 lines (218 loc) · 6.38 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php //-->
/*
* This file is part of the System package of the Eden PHP Library.
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE
* distributed with this package.
*/
namespace Eden\System;
/**
* This is an abstract definition of common
* folder manipulation listing and information
* per folder.
*
* @vendor Eden
* @package System
* @author Christian Blanquera cblanquera@openovate.com
*/
class Folder extends Path
{
const ERROR_CHMOD_IS_INVALID = 'Invalid permissions set when creating the folder %s.';
/**
* Creates a folder given the path
*
* @param int chmod
* @return this
*/
public function create($chmod = 0755)
{
//argument 1 must be an integer
Argument::i()->test(1, 'int');
//if chmod is not and integer or not between 0 and 777
if(!is_int($chmod) || $chmod < 0 || $chmod > 777) {
//throw an error
Exception::i(self::ERROR_CHMOD_IS_INVALID)
->addVariable($this->data)
->trigger();
}
//if it's not a directory
if(!is_dir($this->data)) {
//then make it
mkdir($this->data, $chmod, true);
}
return $this;
}
/**
* Returns a list of files given the path and optionally the pattern
*
* @param string|null regular expression
* @param bool
* @return array
*/
public function getFiles($regex = null, $recursive = false)
{
//argument test
Argument::i()
//argument 1 must be a string
->test(1, 'string', 'null')
//argument 2 must be a boolean
->test(2, 'bool');
$this->absolute();
$files = array();
if ($handle = opendir($this->data)) {
//for each file
while (false !== ($file = readdir($handle))) {
// If this is infact a file
if(filetype($this->data . '/' . $file) == 'file'
&& (!$regex || preg_match($regex, $file))) {
//add it
$files[] = File::i($this->data . '/' . $file);
// recursive and this is infact a directory
} else if($recursive && $file != '.' && $file != '..'
&& filetype($this->data .'/'. $file) == 'dir') {
$subfiles = self::i($this->data.'/'.$file);
$files = array_merge($files, $subfiles->getFiles($regex, $recursive));
}
}
closedir($handle);
}
return $files;
}
/**
* Returns a list of folders given the path and optionally the regular expression
*
* @param string regular expression
* @param bool
* @return array
*/
public function getFolders($regex = null, $recursive = false)
{
//argument test
Argument::i()
//argument 1 must be a string
->test(1, 'string', 'null')
//argument 2 must be a boolean
->test(2, 'bool');
$this->absolute();
$folders = array();
if($handle = opendir($this->data)) {
//walk the directory
while (false !== ($folder = readdir($handle))) {
// If this is infact a directory
//and if it matches the regex
if($folder != '.' && $folder != '..'
&& filetype($this->data .'/'. $folder) == 'dir'
&& (!$regex || preg_match($regex, $folder))) {
//add it
$folders[] = self::i($this->data . '/' . $folder);
if($recursive) {
$subfolders = self::i($this->data.'/'.$folder);
$folders = array_merge($folders, $subfolders->getFolders($regex, $recursive));
}
}
}
closedir($handle);
}
return $folders;
}
/**
* Returns the name of the directory.. just the name
*
* @return string the name
*/
public function getName()
{
$pathArray = $this->getArray();
return array_pop($pathArray);
}
/**
* Checks to see if this
* path is a real file
*
* @param string|null
* @return bool
*/
public function isFolder($path = null)
{
//argument 1 must be a string
Argument::i()->test(1, 'string', 'null');
//if path is string
if(is_string($path)) {
//return path appended
return is_dir($this->data.'/'.$path);
}
return is_dir($this->data);
}
/**
* Removes a folder given the path
*
* @return Eden\System\Folder
*/
public function remove()
{
//get absolute path
$path = $this->absolute();
//if it's a directory
if(is_dir($path)) {
//remove it
rmdir($path);
}
return $this;
}
/**
* Removes files given the path and optionally a regular expression
*
* @param string|null regular expression
* @return Eden\System\Folder
*/
public function removeFiles($regex = null)
{
//argument 1 must be a string
Argument::i()->test(1, 'string', 'null');
//get the files
$files = $this->getFiles($regex);
if(empty($files)) {
return $this;
}
//walk the array
foreach($files as $file) {
//remove everything
$file->remove();
}
return $this;
}
/**
* Removes a folder given the path and optionally the regular expression
*
* @param string regular expression
* @return Eden\System\Folder
*/
public function removeFolders($regex = null)
{
//argument 1 must be a string or null
Argument::i()->test(1, 'string', 'null');
$this->absolute();
$folders = $this->getFolders($regex);
if(empty($folders)) {
return $this;
}
//walk directory
foreach($folders as $folder) {
//remove directory
$folder->remove();
}
return $this;
}
/**
* Removes files and folder given a path
*
* @return Eden\System\Folder
*/
public function truncate()
{
$this->removeFolders();
$this->removeFiles();
return $this;
}
}