-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolder.php
More file actions
179 lines (146 loc) · 4.47 KB
/
Folder.php
File metadata and controls
179 lines (146 loc) · 4.47 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
<?php
class Folder {
public $file_limit = 1;
public $array_extension = array();
public $files = array();
public $read_folder;
public $page_number = 1;
public $page_size = null;
public $has_more = FALSE;
public function __construct(){
}
public function reset_previous(){
$this->files = array();
}
public function where($folder){
$this->reset_previous();
$this->read_folder = $folder;
}
public function get_files(){
return $this->files;
}
public function not_empty(){
return !empty($this->files);
}
public function limit($limit=false){
if($limit !== false && is_numeric($limit) && (int)$limit > 0){
$this->file_limit = (int)$limit;
$this->disable_paging();
}else{
throw new InvalidArgumentException('You must set a numeric file limit greater than 0!');
}
}
public function no_limit(){
$this->file_limit = 0;
$this->disable_paging();
}
public function page($page=1, $size=100){
if(!is_numeric($page) || !is_numeric($size) || (int)$page < 1 || (int)$size < 1){
throw new InvalidArgumentException('Page and page size must be numeric values greater than 0!');
}
$this->page_number = (int)$page;
$this->page_size = (int)$size;
}
public function disable_paging(){
$this->page_number = 1;
$this->page_size = null;
}
public function has_more(){
return $this->has_more;
}
public function read_page($page=1, $size=100){
$this->page($page, $size);
$this->read();
return $this->files;
}
public function extension($extension){
if(is_array($extension)){
$normalized_extensions = array();
foreach($extension as $item){
$item = strtolower(trim((string)$item));
$item = ltrim($item, '.');
if($item !== ''){
$normalized_extensions[] = $item;
}
}
$this->array_extension = array_values(array_unique($normalized_extensions));
}else{
throw new InvalidArgumentException('The extension for files must be in array!');
}
}
private function file_matches_extension($file_name){
if(empty($this->array_extension)){
return TRUE;
}
$extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
return in_array($extension, $this->array_extension, TRUE);
}
private function should_include_entry($directory_path, $entry_name){
if(in_array($entry_name, array('.', '..'), TRUE)){
return FALSE;
}
$entry_path = $directory_path . '/' . $entry_name;
// Subfolders are always visible.
if(is_dir($entry_path)){
return TRUE;
}
return $this->file_matches_extension($entry_name);
}
private function get_stable_filtered_entries($directory_path){
$entries = array();
if($handle = opendir($directory_path)){
while(($entry_name = readdir($handle)) !== FALSE){
if($this->should_include_entry($directory_path, $entry_name)){
$entries[] = $entry_name;
}
}
closedir($handle);
}
// Stable ordering: folders first, then files, each group sorted by name.
usort($entries, function($left_entry, $right_entry) use ($directory_path){
$left_is_dir = is_dir($directory_path . '/' . $left_entry);
$right_is_dir = is_dir($directory_path . '/' . $right_entry);
if($left_is_dir !== $right_is_dir){
return $left_is_dir ? -1 : 1;
}
$case_insensitive = strcasecmp($left_entry, $right_entry);
if($case_insensitive !== 0){
return $case_insensitive;
}
return strcmp($left_entry, $right_entry);
});
return $entries;
}
public function read(){
$this->reset_previous();
$this->has_more = FALSE;
$directory_path = realpath($this->read_folder);
if($directory_path === FALSE || !is_dir($directory_path)){
throw new RuntimeException('The path is not a folder,please set the correct path!');
}
$offset = 0;
$max_items = 0;
if($this->page_size !== null){
$offset = ($this->page_number - 1) * $this->page_size;
$max_items = $this->page_size;
}else{
$offset = 0;
$max_items = ($this->file_limit > 0) ? $this->file_limit : 0;
}
$filtered_entries = $this->get_stable_filtered_entries($directory_path);
$total_entries = count($filtered_entries);
$selected_entries = array();
if($max_items === 0){
$selected_entries = array_slice($filtered_entries, $offset);
$this->has_more = FALSE;
}else{
$selected_entries = array_slice($filtered_entries, $offset, $max_items);
$this->has_more = (($offset + count($selected_entries)) < $total_entries);
}
$file_index = 1;
foreach($selected_entries as $entry_name){
$this->files[$file_index++] = $directory_path . '/' . $entry_name;
}
}
}
?>