forked from ezyang/simpletest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollector.php
More file actions
125 lines (118 loc) · 3.38 KB
/
Copy pathcollector.php
File metadata and controls
125 lines (118 loc) · 3.38 KB
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
<?php
/**
* This file contains the following classes: {@link SimpleCollector},
* {@link SimplePatternCollector}.
*
* @author Travis Swicegood <development@domain51.com>
*/
/**
* The basic collector for {@link GroupTest}
*
* @see collect(), GroupTest::collect()
*/
class SimpleCollector
{
/**
* Strips off any kind of slash at the end so as to normalise the path.
*
* @param string $path Path to normalise.
*
* @return string Path without trailing slash.
*/
protected function removeTrailingSlash($path)
{
if (substr($path, -1) == DIRECTORY_SEPARATOR) {
return substr($path, 0, -1);
} elseif (substr($path, -1) == '/') {
return substr($path, 0, -1);
} else {
return $path;
}
}
/**
* Scans the directory and adds what it can.
*
* @param object $test Group test with {@link GroupTest::addFile)} method.
* @param string $path Directory to scan.
*
* @see _attemptToAdd()
*/
public function collect(&$test, $path)
{
$path = $this->removeTrailingSlash($path);
if ($handle = opendir($path)) {
while (($entry = readdir($handle)) !== false) {
if ($this->isHidden($entry)) {
continue;
}
$this->handle($test, $path . DIRECTORY_SEPARATOR . $entry);
}
closedir($handle);
}
}
/**
* This method determines what should be done with a given file and adds
* it via {@link GroupTest::addFile)} if necessary.
*
* This method should be overriden to provide custom matching criteria,
* such as pattern matching, recursive matching, etc. For an example, see
* {@link SimplePatternCollector::_handle()}.
*
* @param object $test Group test with {@link GroupTest::addFile)} method.
* @param string $filename A filename as generated by {@link collect()}
*
* @see collect()
*/
protected function handle(&$test, $file)
{
if (is_dir($file)) {
return;
}
$test->addFile($file);
}
/**
* Tests for hidden files so as to skip them.
* Currently only tests for Unix hidden files.
*
* @param string $filename Plain filename.
*
* @return bool True if hidden file.
*/
protected function isHidden($filename)
{
return strncmp($filename, '.', 1) == 0;
}
}
/**
* An extension to {@link SimpleCollector} that only adds files matching a given pattern.
*
* See {@link http://php.net/manual/en/reference.pcre.pattern.syntax.php PHP's PCRE}
* for documentation of valid patterns.
*
* @see SimpleCollector
*/
class SimplePatternCollector extends SimpleCollector
{
private $pattern;
/**
* @param string $pattern Perl compatible regex to test name against
*/
public function __construct($pattern = '/php$/i')
{
$this->pattern = $pattern;
}
/**
* Attempts to add files that match a given pattern.
*
* @see SimpleCollector::_handle()
*
* @param object $test Group test with {@link GroupTest::addFile)} method.
* @param string $path Directory to scan.
*/
protected function handle(&$test, $filename)
{
if (preg_match($this->pattern, $filename)) {
parent::handle($test, $filename);
}
}
}