1 <?php rcs_id('$Id: FileFinder.php,v 1.4 2001-12-13 08:22:47 carstenklapp Exp $');
3 // FIXME: make this work with non-unix (e.g. DOS) filenames.
6 * A class for finding files.
13 * @param $path array A list of directories in which to search for files.
15 function FileFinder ($path = false) {
17 $path = $this->_get_include_path();
24 * @param $file string File to search for.
25 * @return string The filename (including path), if found, otherwise false.
27 function findFile ($file, $missing_okay = false) {
28 if ($this->_is_abs($file)) {
29 if (file_exists($file))
32 elseif ( ($dir = $this->_search_path($file)) ) {
36 return $missing_okay ? false : $this->_not_found($file);
40 * Try to include file.
42 * If file is found in the path, then the files directory is added
43 * to PHP's include_path (if it's not already there.) Then
44 * the file is include_once()'d.
46 * @param $file string File to include.
47 * @return bool True if file was successfully included.
49 function includeOnce ($file) {
50 if ( ($ret = @include_once($file)) )
53 if (!$this->_is_abs($file)) {
54 if ( ($dir = $this->_search_path($file)) && is_file("$dir/$file")) {
55 $this->_append_to_include_path($dir);
56 return include_once($file);
60 return $this->_not_found($file);
64 * Determine if path is absolute.
67 * @param $path string Path.
68 * @return bool True iff path is absolute.
70 function _is_abs($path) {
71 return ereg('^/', $path);
75 * Report a "file not found" error.
78 * @param $file string Name of missing file.
81 function _not_found($file) {
82 trigger_error("$file: file not found", E_USER_ERROR);
88 * Search our path for a file.
91 * @param $file string File to find.
92 * @return string Directory which contains $file, or false.
94 function _search_path ($file) {
95 foreach ($this->_path as $dir) {
96 if (file_exists("$dir/$file"))
103 * The system-dependent path-separator character. On UNIX systems,
104 * this character is ':'; on Win32 systems it is ';'.
107 * @return string path_separator.
109 function _get_path_separator () {
110 return preg_match('/^Windows/', php_uname()) ? ';' : ':';
114 * Get the value of PHP's include_path.
117 * @return array Include path.
119 function _get_include_path() {
120 $path = ini_get('include_path');
123 return explode($this->_get_path_separator(), $path);
127 * Add a directory to the end of PHP's include_path.
129 * The directory is appended only if it is not already listed
130 * in the include_path.
133 * @param $dir string Directory to add.
135 function _append_to_include_path ($dir) {
136 $path = $this->_get_include_path();
137 if (!in_array($dir, $path)) {
139 //ini_set('include_path', implode(':', $path));
142 * Some (buggy) PHP's (notable SourceForge's PHP 4.0.6)
143 * sometimes don't seem to heed their include_path.
144 * I.e. sometimes a file is not found even though it seems
145 * to be in the current include_path.
146 * A simple ini_set('include_path', ini_get('include_path'))
147 * seems to be enough to fix the problem
149 * This following line should be in the above if-block,
150 * but we put it here, as it seems to work-around the bug.
152 ini_set('include_path', implode($this->_get_path_separator(), $path));
157 * A class for finding PEAR code.
159 * This is a subclass of FileFinder which searches a standard list of directories
160 * where PEAR code is likely to be installed.
164 * $pearFinder = new PearFileFinder;
165 * $pearFinder->includeOnce('DB.php');
167 * The above code will look for 'DB.php', if found, the directory in which it was
168 * found will be added to PHP's include_path, and the file will be included.
169 * (If the file is not found, and E_USER_ERROR will be thrown.)
177 * @param $path array Where to look for PEAR library code.
178 * A good set of defaults is provided, so you can probably leave
179 * this parameter blank.
181 function PearFileFinder ($path = false) {
182 $this->FileFinder(array('/usr/share/php4',
186 '/usr/local/share/php4',
187 '/usr/local/share/php',
188 '/usr/local/lib/php4',
189 '/usr/local/lib/php',
190 '/System/Library/PHP'));
195 * Find PhpWiki localized files.
197 * This is a subclass of FileFinder which searches PHP's include_path for
198 * files. It looks first for "locale/$LANG/$file", then for "$file".
200 * If $LANG is something like "de_DE.iso8859-1@euro", this class will
201 * also search under various less specific variations like "de_DE.iso8859-1",
204 class LocalizedFileFinder
210 function LocalizedFileFinder () {
211 $include_path = $this->_get_include_path();
214 $lang = $this->_get_lang();
215 assert(!empty($lang));
217 // A locale can be, e.g. de_DE.iso8859-1@euro.
218 // Try less specific versions of the locale:
220 foreach (array('@', '.', '_') as $sep) {
221 if ( ($tail = strchr($lang, $sep)) )
222 $langs[] = substr($lang, 0, -strlen($tail));
225 foreach ($langs as $lang) {
226 foreach ($include_path as $dir) {
227 $path[] = "$dir/locale/$lang";
231 $this->FileFinder(array_merge($path, $include_path));
235 * Try to figure out the appropriate value for $LANG.
238 *@return string The value of $LANG.
240 function _get_lang() {
241 if (!empty($GLOBALS['LANG']))
242 return $GLOBALS['LANG'];
244 foreach (array('LC_ALL', 'LC_MESSAGES', 'LC_RESPONSES', 'LANG') as $var) {
245 $lang = getenv($var);
259 // c-hanging-comment-ender-p: nil
260 // indent-tabs-mode: nil