-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile-manifest.php
169 lines (115 loc) · 4.41 KB
/
file-manifest.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
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
<?php
class File_manifest {
private static $file_pattern_replacements = array(
'/\\./' => '\\\\.',
'/\\-/' => '\\\\-',
'/\\*/' => '(.*)',
);
public static function read($manifest_filename) {
// prepare an array of all files to include from the manifest
$manifest_files = array();
// split the manifest file by newlines
$manifest = preg_split('/\r?\n/', file_get_contents($manifest_filename), -1, PREG_SPLIT_NO_EMPTY);
// get the pathinfo for the manifest file
$pathinfo_dirname = pathinfo($manifest_filename, PATHINFO_DIRNAME);
// reference the directory where this file resides
$parent_dir = realpath($pathinfo_dirname);
// reference the current working directory to change back to later
$previous_working_dir = getcwd();
// change directory to the manifest file's directory
chdir($parent_dir);
// prepare to reference all and any files in any directory
$candidate_files = array();
// iterate over every manifest line
foreach($manifest as $file_pattern) {
// first check if the pattern points to another directory
$file_path_split = preg_split('/\\//', $file_pattern);
// if another directory is referenced
if(sizeof($file_path_split) != 1) {
// follow the path
chdir(
implode('/',
array_slice($file_path_split,0,-1)
)
);
// only wildcard on the filename portion
$file_pattern = end($file_path_split);
}
// reference the current working directory now
$cwd = getcwd();
// if an array of all files in the cwd doesn't exist yet
if(!isset($candidate_files[$cwd])) {
// scan all the files in this directory
$candidate_files[$cwd] = scandir('.');
}
// translate the wildcard-pattern into regex
$regex_pattern = '/^'.preg_replace(
array_keys(self::$file_pattern_replacements),
array_values(self::$file_pattern_replacements),
$file_pattern
).'$/i';
// iterate over every file in this directory
foreach($candidate_files[$cwd] as $filename) {
// add this file to the
if(preg_match($regex_pattern, $filename)) {
// reference the entire path of the matching filename
$filepath = $cwd.'/'.$filename;
// check if this path hasn't already been added to our manifest list
if(!in_array($filepath, $manifest_files)) {
// add this path to the manifest list
$manifest_files []= getcwd().'/'.$filename;
}
}
}
// change back to parent directory in case another path was followed
chdir($parent_dir);
}
// change back to the original working directory
chdir($previous_working_dir);
// return all this information
return array(
'parent_dir' => $parent_dir,
'manifest_files' => $manifest_files,
);
}
public static function merge($manifest_filename, $header_format="", $footer_format="") {
$info = self::read($manifest_filename);
// reference the manifest files
$manifest_files = $info['manifest_files'];
// get the string length of the parent directory realpath
$parent_dir_strlen = strlen($info['parent_dir']) + 1;
// prepare an array to merge all the file contents
$merge = array();
// iterate over every manifest file
foreach($manifest_files as $filepath) {
$filename = substr($filepath, $parent_dir_strlen);
// show the start of a file
if(!preg_match('/^preload\./', $filename)) {
$merge []= preg_replace('/%PATH%/', $filename, $header_format);
}
// add the contents to the array
$merge []= file_get_contents($filepath);
// append any post-content
if(!preg_match('/^preload\./', $filename)) {
$merge []= preg_replace('/%PATH%/', $filename, $footer_format);
}
}
// concatenate all the files (merge) into one string
return implode("\n", $merge);
}
public static function gen($manifest_filename, $echo_format, $glue="\n") {
$info = self::read($manifest_filename);
// reference the manifest files
$manifest_files = $info['manifest_files'];
// get the string length of the parent directory realpath
$parent_dir_strlen = strlen($info['parent_dir']) + 1;
// prepare an array to merge all the echo strings
$echo = array();
// iterate over every manifest file
foreach($manifest_files as $filepath) {
// show the start of a file
$echo []= preg_replace('/%PATH%/', substr($filepath, $parent_dir_strlen), $echo_format);
}
return implode($glue, $echo);
}
}