1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of ci-phpunit-test |
4
|
|
|
* |
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2015 Kenji Suzuki |
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kenjis\MonkeyPatch; |
12
|
|
|
|
13
|
|
|
use RuntimeException; |
14
|
|
|
|
15
|
|
|
class PathChecker |
16
|
|
|
{ |
17
|
|
|
private static $include_paths = []; |
18
|
|
|
private static $exclude_paths = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $paths directory or file path |
22
|
|
|
* @return array |
23
|
|
|
* @throws RuntimeException |
24
|
|
|
*/ |
25
|
|
|
protected static function normalizePaths(array $paths) |
26
|
|
|
{ |
27
|
|
|
$new_paths = []; |
28
|
|
|
$excluded = false; |
29
|
|
|
foreach ($paths as $path) |
30
|
|
|
{ |
31
|
|
|
// Path starting with '-' has special meaning (excluding it) |
32
|
|
|
if (substr($path, 0, 1) === '-') |
33
|
|
|
{ |
34
|
|
|
$excluded = true; |
35
|
|
|
$path = ltrim($path, '-'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$real_path = realpath($path); |
39
|
|
|
if ($real_path === FALSE) |
40
|
|
|
{ |
41
|
|
|
throw new RuntimeException($path . ' does not exist?'); |
42
|
|
|
} |
43
|
|
|
if (is_dir($real_path)) |
44
|
|
|
{ |
45
|
|
|
// Must use DIRECTORY_SEPARATOR for Windows |
46
|
|
|
$real_path = $real_path . DIRECTORY_SEPARATOR; |
47
|
|
|
} |
48
|
|
|
$new_paths[] = $excluded ? '-'.$real_path : $real_path; |
49
|
|
|
} |
50
|
|
|
array_unique($new_paths, SORT_STRING); |
51
|
|
|
sort($new_paths, SORT_STRING); |
52
|
|
|
return $new_paths; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function setIncludePaths(array $dir) |
56
|
|
|
{ |
57
|
|
|
self::$include_paths = self::normalizePaths($dir); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function setExcludePaths(array $dir) |
61
|
|
|
{ |
62
|
|
|
self::$exclude_paths = self::normalizePaths($dir); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function getIncludePaths() |
66
|
|
|
{ |
67
|
|
|
return self::$include_paths; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function getExcludePaths() |
71
|
|
|
{ |
72
|
|
|
return self::$exclude_paths; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function check($path) |
76
|
|
|
{ |
77
|
|
|
// Windows |
78
|
|
|
if (DIRECTORY_SEPARATOR === '\\') { |
79
|
|
|
$path = str_replace('/', '\\', $path); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Whitelist first |
83
|
|
|
$is_white = false; |
84
|
|
|
foreach (self::$include_paths as $white_dir) { |
85
|
|
|
$len = strlen($white_dir); |
86
|
|
|
if (substr($path, 0, $len) === $white_dir) |
87
|
|
|
{ |
88
|
|
|
$is_white = true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
if ($is_white === false) |
92
|
|
|
{ |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Then blacklist |
97
|
|
|
foreach (self::$exclude_paths as $black_dir) { |
98
|
|
|
// Check excluded path that starts with '-'. |
99
|
|
|
// '-' is smaller than '/', so this checking always comes first. |
100
|
|
|
if (substr($black_dir, 0, 1) === '-') |
101
|
|
|
{ |
102
|
|
|
$black_dir = ltrim($black_dir, '-'); |
103
|
|
|
$len = strlen($black_dir); |
104
|
|
|
if (substr($path, 0, $len) === $black_dir) |
105
|
|
|
{ |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$len = strlen($black_dir); |
111
|
|
|
if (substr($path, 0, $len) === $black_dir) |
112
|
|
|
{ |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|