-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystem.php
More file actions
111 lines (97 loc) · 2.7 KB
/
Filesystem.php
File metadata and controls
111 lines (97 loc) · 2.7 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
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* Collection of filesystem functions
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* Collection of filesystem functions
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
class MindFrame2_Filesystem
{
public static function assertFileExists($file_name)
{
if (!file_exists($file_name))
{
throw new RuntimeException(
sprintf('The specified file does not exist (%s)', $file_name));
}
}
public static function assertFileIsReadable($file_name)
{
if (!is_readable($file_name))
{
throw new RuntimeException(
sprintf('Could not read the specified file (%s)', $file_name));
}
}
/**
* Returns a directory listing filtered by the specified regular
* expression
*
* @param string $directory The directory path to get the listing for
* @param string $filter The regular expression used to filter the results
*
* @return array
*/
public static function ls($directory, $filter)
{
$files = array();
$dir = dir($directory);
while (FALSE !== ($item = $dir->read()))
{
if (preg_match($filter, $item) === 1)
{
$files[] = $item;
}
// end if // is_dir //
}
// end while // read directory //
sort($files);
return($files);
}
/**
* Returns a recursive directory listing filtered by the specified regular
* expression
*
* @param string $directory The directory path to get the listing for
* @param string $filter The regular expression used to filter the results
*
* @return array
*/
public static function lsRecursive($directory, $filter)
{
$dir = self::ls($directory, '/^[^\.]+/');
$files = array();
foreach ($dir as $item)
{
if (preg_match($filter, $item) === 1)
{
$files[] = $directory . '/' . $item;
}
// end if // (preg_match($filter, $item) === 1) //
$subdir = $directory . '/' . $item;
if (is_dir($subdir))
{
$files = array_merge($files, self::lsRecursive($subdir, $filter));
}
// end if // (is_dir($subdir)) //
}
// end foreach // ($dir as $item) //
sort($files);
return($files);
}
}