-
Notifications
You must be signed in to change notification settings - Fork 326
/
Filesystem.php
117 lines (95 loc) · 1.98 KB
/
Filesystem.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
<?php
namespace Kohana\Core;
class Filesystem
{
protected $_include_paths = array();
public function __construct(array $paths)
{
$this->_paths = $paths;
}
public function include_paths()
{
return $this->_paths;
}
public function list_files($directory, array $paths = NULL)
{
$found = array();
foreach ($paths === NULL ? $this->_paths : $paths as $path)
{
if (is_dir($path.$directory))
{
$dir = new \DirectoryIterator($path.$directory);
foreach ($dir as $file)
{
$filename = $file->getFilename();
// Skip all hidden files and UNIX backup files
if ($filename[0] === '.' OR $filename[strlen($filename)-1] === '~')
continue;
// Relative filename is the array key
$key = $directory.'/'.$filename;
if ($file->isDir())
{
if ($sub_dir = $this->list_files($key, $paths))
{
if (isset($found[$key]))
{
// Append the sub-directory list
$found[$key] += $sub_dir;
}
else
{
// Create a new sub-directory list
$found[$key] = $sub_dir;
}
}
}
else
{
if ( ! isset($found[$key]))
{
$found[$key] = $this->realpath($file->getPathName());
}
}
}
}
}
return $found;
}
public function find_file($dir, $file, $ext = 'php')
{
$found = FALSE;
$path = $this->_build_file_path($dir, $file, $ext);
foreach ($this->_paths as $dir)
{
if (is_file($dir.$path))
{
$found = $dir.$path;
break;
}
}
return $found;
}
public function find_all_files($dir, $file, $ext = 'php')
{
$found = array();
$path = $this->_build_file_path($dir, $file, $ext);
foreach ($this->_paths as $dir)
{
if (is_file($dir.$path))
$found[] = $dir.$path;
}
return $found;
}
public function load($file)
{
return include $file;
}
public function realpath($path)
{
return realpath($path);
}
protected function _build_file_path($dir, $file, $ext)
{
return "$dir/$file.$ext";
}
}