summaryrefslogtreecommitdiff
path: root/ext/spl/examples/findfile.inc
blob: 05d525a3fbd6c238ed956f6f0241d2eaa98f3e0a (plain)
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
<?php

/** @file findfile.inc
 * @ingroup Examples
 * @brief class FindFile
 * @author  Marcus Boerger
 * @date    2003 - 2005
 *
 * SPL - Standard PHP Library
 */

if (!class_exists("FindFile", false)) require_once("findfile.inc");
if (!class_exists("AppendIterator", false)) require_once("appenditerator.inc");

/** @ingroup Examples
 * @brief   Base class to find files
 * @author  Marcus Boerger
 * @version 1.1
 *
 */
class FindFile extends FilterIterator
{
	/** @internal filename to find */
	private $file;

	/** Construct from path and filename
	 *
	 * @param $path the directory to search in
	 *              If path contains ';' then this parameter is split and every
	 *              part of it is used as separate directory.
	 * @param $file the name of the files to search fro
	 */
	function __construct($path, $file)
	{
		$this->file = $file;
		$list = split(';', $path);
		if (count($list) <= 1) {
			parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
		} else {
			$it = new AppendIterator();
			foreach($list as $path) {
				$it->append(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
			}
			parent::__construct($it);
		}
	}

	/** @return whether the current file matches the given filename
	 */
	function accept()
	{
		return !strcmp($this->current(), $this->file);
	}

	/** @return the filename to search for.
	 * @note This may be overloaded and contain a regular expression for an
	 *       extended class that uses regular expressions to search.
	 */
	function getSearch()
	{
		return $this->file;
	}
}

?>