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 $this->_setupIgnore($ignore);
168 if (!$this->ignore) {
169 return false;
170 }
171 if (is_array($this->ignore)) {
172 foreach($this->ignore as $match) {
173 if (is_array($match)) {
174 preg_match('/^'.strtoupper($match[0]).'$/',strtoupper($path) . PATH_DELIMITER,$find);
175 if (!count($find)) {
176 preg_match('/^'.strtoupper($match[0]).'$/',strtoupper($path),$find);
177 }
178 if (count($find)) {
179 preg_match('/^'.strtoupper($match[1]).'$/',strtoupper($file),$find);
180 if (count($find)) return true;
181 }
182 } else {
183 preg_match('/^'.strtoupper($match).'$/',strtoupper($path),$find);
184 if (count($find)) return true;
185 preg_match('/^'.strtoupper($match).'$/',strtoupper($file),$find);
186 if (count($find)) return true;
187 }
188 }
189 }
190 return false;
191 }
192
193 /**
194 * Construct the {@link $ignore} array
195 * @param array strings of files/paths/wildcards to ignore
196 * @access private
197 */
198 function _setupIgnore($ignore)
199 {
200 $ig = array();
201 if (is_array($ignore)) {
202 for($i=0; $i<count($ignore);$i++) {
203 $ignore[$i] = strtr($ignore[$i], "\\", "/");
204 $ignore[$i] = str_replace('//','/',$ignore[$i]);
205
206 if (!empty($ignore[$i])) {
207 if (!is_numeric(strpos($ignore[$i], '/'))) {
208 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
209 } else {
210 if (basename($ignore[$i]) . '/' == $ignore[$i]) {
211 $ig[] = $this->_getRegExpableSearchString($ignore[$i]);
212 } else {
213 $ig[] = array($this->_getRegExpableSearchString($ignore[$i]),
214 $this->_getRegExpableSearchString(basename($ignore[$i])));
215 }
216 }
217 }
218 }
219 if (count($ig)) {
220 $this->ignore = $ig;
221 }
222 } else $this->ignore = false;
223 }
224
225 /**
226 * Converts $s into a string that can be used with preg_match
227 * @param string $s string with wildcards ? and *
228 * @return string converts * to .*, ? to ., etc.
229 * @access private
230 */
231 function _getRegExpableSearchString($s)
232 {
233 $y = '\/';
234 if (DIRECTORY_SEPARATOR == '\\') {
235 $y = '\\\\';
236 }
237 if (strpos($s,'/') === strlen($s) - 1) {
238 $s = str_replace('/', DIRECTORY_SEPARATOR, $s);
239 }
240 $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/',
241 '[' => '\\[',']' => '\\]','-' => '\\-'));
242 if (strpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1) {
243 $x = "(?:.*$y$x?.*|$x.*)";
244 }
245 return $x;
246 }
247
248 /**
249 * Recursively move contents of $struc into associative array
250 *
251 * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
252 * This function converts them to
253 * array('dir' => array('subdir' => array('subdir2')))
254 * @param array struc is array('dir' => array of files in dir,
255 * 'dir/subdir' => array of files in dir/subdir,...)
256 * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
257 * @return array same as struc but with array('dir' =>
258 * array(file1,file2,'subdir' => array(file1,...)))
259 * @access private
260 */
261 function _setupDirs($struc, $dir, $contents)
262 {
263 if (!count($dir)) {
264 foreach($contents as $dir => $files) {
265 if (is_string($dir)) {
266 if (strpos($dir, '/')) {
267 $test = true;
268 $a = $contents[$dir];
269 unset($contents[$dir]);
270 $b = explode('/', $dir);
271 $c = array_shift($b);
272 if (isset($contents[$c])) {
273 $contents[$c] = $this->_setDir($contents[$c], $this->_setupDirs(array(), $b, $a));
274 } else {
275 $contents[$c] = $this->_setupDirs(array(), $b, $a);
276 }
277 }
278 }
279 }
280 return $contents;
281 }
282 $me = array_shift($dir);
283 if (!isset($struc[$me])) {
284 $struc[$me] = array();
285 }
286 $struc[$me] = $this->_setupDirs($struc[$me], $dir, $contents);
287 return $struc;
288 }
289
290
291 /**
292 * Recursively add all the subdirectories of $contents to $dir without erasing anything in
293 * $dir
294 * @param array
295 * @param array
296 * @return array processed $dir
297 * @access private
298 */
299 function _setDir($dir, $contents)
300 {
301 while(list($one,$two) = each($contents)) {
302 if (isset($dir[$one])) {
303 $dir[$one] = $this->_setDir($dir[$one], $contents[$one]);
304 } else {
305 $dir[$one] = $two;
306 }
307 }
308 return $dir;
309 }
310
311
312 /**#@+
313 * Sorting functions for the file list
314 * @param string
315 * @param string
316 * @access private
317 */
318 function sortfiles($a, $b)
319 {
320 return strnatcasecmp($a['file'],$b['file']);
321 }
322
323 function mystrucsort($a, $b)
324 {
325 if (is_numeric($a) && is_string($b)) return 1;
326 if (is_numeric($b) && is_string($a)) return -1;
327 if (is_numeric($a) && is_numeric($b))
328 {
329 if ($a > $b) return 1;
330 if ($a < $b) return -1;
331 if ($a == $b) return 0;
332 }
333 return strnatcasecmp($a,$b);
334 }
335 /**#@-*/
336 }
337 ?>

Documentation generated on Wed, 23 Jul 2003 17:00:31 -0400 by phpDocumentor 1.2.2