phpDocumentor PEAR_PackageFileManager
[ class tree: PEAR_PackageFileManager ] [ index: PEAR_PackageFileManager ] [ all elements ]

Source for file File.php

Documentation is available at File.php


1 <?php
2 /**
3 * Retrieve the files from a directory listing
4 * @package PEAR_PackageFileManager
5 */
6 /**
7 * Retrieve the files from a directory listing
8 * @package PEAR_PackageFileManager
9 */
10 class PEAR_PackageFileManager_File {
11 /**
12 * @var array
13 * @access private
14 */
15 var $_options =
16 array(
17 );
18
19 /**
20 * @access private
21 * @var PEAR_PackageFileManager
22 */
23 var $_parent;
24
25 /**
26 * @access private
27 * @var array|false
28 */
29 var $_ignore = false;
30
31 /**
32 * @param PEAR_PackageFileManager
33 * @param array
34 */
35 function PEAR_PackageFileManager_File(&$parent, $options)
36 {
37 $this->_parent = &$parent;
38 $this->_options = array_merge($this->_options, $options);
39 }
40
41 /**
42 * Generate the <filelist></filelist> section
43 * of the package file.
44 *
45 * This function performs the backend generation of the array
46 * containing all files in this package
47 * @return array
48 */
49 function getFileList()
50 {
51 $package_directory = $this->_options['packagedirectory'];
52 $ignore = $this->_options['ignore'];
53 $allfiles = $this->dirList(substr($package_directory, 0, strlen($package_directory) - 1));
54 if (!count($allfiles)) {
55 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_NO_FILES,
56 substr($package_directory, 0, strlen($package_directory) - 1));
57 }
58 $struc = array();
59 foreach($allfiles as $file) {
60 if ($this->_checkIgnore(basename($file), dirname($file), $ignore, false)) {
61 // print 'Ignoring '.$file."<br>\n";
62 continue;
63 }
64 $path = substr(dirname($file), strlen(str_replace(DIRECTORY_SEPARATOR,
65 '/',
66 realpath($package_directory))) + 1);
67 if (!$path) {
68 $path = '/';
69 }
70 $ext = array_pop(explode('.', $file));
71 if (strlen($ext) == strlen($file)) {
72 $ext = '';
73 }
74 $struc[$path][] = array('file' => basename($file),
75 'ext' => $ext,
76 'path' => (($path == '/') ? basename($file) : $path . '/' . basename($file)),
77 'fullpath' => $file);
78 }
79 if (!count($struc)) {
80 $newig = '';
81 foreach($this->_options['ignore'] as $ig) {
82 if (!empty($newig)) {
83 $newig .= ', ';
84 }
85 $newig .= $ig;
86 }
87 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING,
88 substr($package_directory, 0, strlen($package_directory) - 1), $newig);
89 }
90 uksort($struc,'strnatcasecmp');
91 foreach($struc as $key => $ind) {
92 usort($ind, array($this, 'sortfiles'));
93 $struc[$key] = $ind;
94 }
95
96 $tempstruc = $struc;
97 $struc = array('/' => $tempstruc['/']);
98 $bv = 0;
99 foreach($tempstruc as $key => $ind) {
100 $save = $key;
101 if ($key != '/')
102 {
103 $struc['/'] = $this->_setupDirs($struc['/'], explode('/',$key), $tempstruc[$key]);
104 }
105 }
106 uksort($struc['/'], array($this, 'mystrucsort'));
107
108 return $struc;
109 }
110
111 /**
112 * @return array list of files in a directory
113 * @param string $directory full path to the directory you want the list of
114 */
115 function dirList($directory)
116 {
117 $ret = false;
118 if (@is_dir($directory)) {
119 $ret = array();
120 $d = @dir($directory); // thanks to Jason E Sweat (jsweat@users.sourceforge.net) for fix
121 while($d && $entry=$d->read()) {
122 if ($entry{0} != '.') {
123 if (is_file($directory . '/' . $entry)) {
124 $ret[] = $directory . '/' . $entry;
125 }
126 if (is_dir($directory . '/' . $entry)) {
127 $tmp = $this->dirList($directory . '/' . $entry);
128 if (is_array($tmp)) {
129 foreach($tmp as $ent) {
130 $ret[] = $ent;
131 }
132 }
133 }
134 }
135 }
136 if ($d) {
137 $d->close();
138 }
139 } else {
140 return PEAR_PackageFileManager::raiseError(PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST, $directory);
141 }
142 return $ret;
143 }
144
145 /**
146 * Tell whether to ignore a file or a directory
147 * allows * and ? wildcards
148 *
149 * @param string $file just the file name of the file or directory,
150 * in the case of directories this is the last dir
151 * @param string $path the full path
152 * @param array $ignore
153 * @return bool true if $path should be ignored, false if it should not
154 * @access private
155 */
156 function _checkIgnore($file, $path, $ignore, $ignore_no_ext = false)
157 {
158 $path = realpath($path);
159 if (!count($ignore)) {
160 return false;
161 }
162 if ($ignore_no_ext && strtoupper($file) != 'README' && strtoupper($file) != 'INSTALL'
163 && strtoupper($file) != 'CHANGELOG' && strtoupper($file) != 'FAQ'
164 && strtoupper($file) != 'NEWS') {
165 if (!is_numeric(strpos($file,'.'))) return true;
166 }
167 if (!isset($this->ignore) || !$this->ignore) {
168 $this->_setupIgnore($ignore);
169 if (!$this->ignore) {
170 return false;
171 }
172 }
173 if (is_array($this->ignore)) {
174 foreach($this->ignore as $match) {
175 // match is an array if the ignore parameter was a /path/to/pattern
176 if (is_array($match)) {
177 // check to see if the path matches with a path delimiter appended
178 preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path) . '/',$find);
179 if (!count($find)) {
180 // check to see if it matches without an appended path delimiter
181 preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path), $find);
182 }
183 if (count($find)) {
184 // check to see if the file matches the file portion of the regex string
185 preg_match('/^' . strtoupper($match[1]).'$/', strtoupper($file), $find);
186 if (count($find)) {
187 return true;
188 }
189 }
190 // check to see if the full path matches the regex
191 preg_match('/^' . strtoupper($match[0]).'$/',
192 strtoupper($path . DIRECTORY_SEPARATOR . $file), $find);
193 if (count($find)) {
194 return true;
195 }
196 } else {
197 // ignore parameter was just a pattern with no path delimiters
198 // check it against the path
199 preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find);
200 if (count($find)) {
201 return true;
202 }
203 // check it against the file only
204 preg_match('/^' . strtoupper($match).'$/', strtoupper($file), $find);
205 if (count($find)) {
206 return true;
207 }
208 }
209 }
210 }
211 return false;
212 }
213
214 /**
215 * Construct the {@link $ignore} array
216 * @param array strings of files/paths/wildcards to ignore
217 * @access private
218 */
219 function _setupIgnore($ignore)
220 {
221 $ig = array();
222 if (is_array($ignore)) {
223 for($i=0; $i<count($ignore);$i++) {
224 $ignore[$i] = strtr($ignore[$i], "\\", "/");
225 $ignore[$i] = str_replace('//','/',$ignore[$i]);
226
227 if (!empty($ignore[$i])) {
228 if (!is_numeric(strpos($ignore[$i], '/'))) {
229 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
230 } else {
231 if (basename($ignore[$i]) . '/' == $ignore[$i]) {
232 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
233 } else {
234 $ig[] = array($this->_getRegExpableSearchString($ignore[$i]),
235 $this->_getRegExpableSearchString(basename($ignore[$i])));
236 }
237 }
238 }
239 }
240 if (count($ig)) {
241 $this->ignore = $ig;
242 }
243 } else $this->ignore = false;
244 }
245
246 /**
247 * Converts $s into a string that can be used with preg_match
248 * @param string $s string with wildcards ? and *
249 * @return string converts * to .*, ? to ., etc.
250 * @access private
251 */
252 function _getRegExpableSearchString($s)
253 {
254 $y = '\/';
255 if (DIRECTORY_SEPARATOR == '\\') {
256 $y = '\\\\';
257 }
258 $s = str_replace('/', DIRECTORY_SEPARATOR, $s);
259 $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/',
260 '[' => '\\[',']' => '\\]','-' => '\\-'));
261 if (strpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) {
262 $x = "(?:.*$y$x?.*|$x.*)";
263 }
264 return $x;
265 }
266
267 /**
268 * Recursively move contents of $struc into associative array
269 *
270 * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
271 * This function converts them to
272 * array('dir' => array('subdir' => array('subdir2')))
273 * @param array struc is array('dir' => array of files in dir,
274 * 'dir/subdir' => array of files in dir/subdir,...)
275 * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
276 * @return array same as struc but with array('dir' =>
277 * array(file1,file2,'subdir' => array(file1,...)))
278 * @access private
279 */
280 function _setupDirs($struc, $dir, $contents)
281 {
282 if (!count($dir)) {
283 foreach($contents as $dir => $files) {
284 if (is_string($dir)) {
285 if (strpos($dir, '/')) {
286 $test = true;
287 $a = $contents[$dir];
288 unset($contents[$dir]);
289 $b = explode('/', $dir);
290 $c = array_shift($b);
291 if (isset($contents[$c])) {
292 $contents[$c] = $this->_setDir($contents[$c], $this->_setupDirs(array(), $b, $a));
293 } else {
294 $contents[$c] = $this->_setupDirs(array(), $b, $a);
295 }
296 }
297 }
298 }
299 return $contents;
300 }
301 $me = array_shift($dir);
302 if (!isset($struc[$me])) {
303 $struc[$me] = array();
304 }
305 $struc[$me] = $this->_setupDirs($struc[$me], $dir, $contents);
306 return $struc;
307 }
308
309
310 /**
311 * Recursively add all the subdirectories of $contents to $dir without erasing anything in
312 * $dir
313 * @param array
314 * @param array
315 * @return array processed $dir
316 * @access private
317 */
318 function _setDir($dir, $contents)
319 {
320 while(list($one,$two) = each($contents)) {
321 if (isset($dir[$one])) {
322 $dir[$one] = $this->_setDir($dir[$one], $contents[$one]);
323 } else {
324 $dir[$one] = $two;
325 }
326 }
327 return $dir;
328 }
329
330
331 /**#@+
332 * Sorting functions for the file list
333 * @param string
334 * @param string
335 * @access private
336 */
337 function sortfiles($a, $b)
338 {
339 return strnatcasecmp($a['file'],$b['file']);
340 }
341
342 function mystrucsort($a, $b)
343 {
344 if (is_numeric($a) && is_string($b)) return 1;
345 if (is_numeric($b) && is_string($a)) return -1;
346 if (is_numeric($a) && is_numeric($b))
347 {
348 if ($a > $b) return 1;
349 if ($a < $b) return -1;
350 if ($a == $b) return 0;
351 }
352 return strnatcasecmp($a,$b);
353 }
354 /**#@-*/
355 }
356 ?>

Documentation generated on Thu, 24 Jul 2003 22:35:52 -0400 by phpDocumentor 1.2.2