|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Files\PathMatch; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Strings\WildcardPattern; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A shell path pattern to match against. |
|
11
|
|
|
* Based on {@see WildcardPattern}. |
|
12
|
|
|
*/ |
|
13
|
|
|
final class PathPattern implements PathMatcherInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private const FILES = 1; |
|
16
|
|
|
private const DIRECTORIES = 2; |
|
17
|
|
|
|
|
18
|
|
|
private WildcardPattern $pattern; |
|
19
|
|
|
private ?int $matchOnly = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $pattern The path pattern to match against. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(string $pattern) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->pattern = (new WildcardPattern($pattern)) |
|
27
|
|
|
->withExactSlashes() |
|
28
|
|
|
->ignoreCase() |
|
29
|
|
|
->withEnding(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Make pattern case sensitive. |
|
34
|
|
|
* @return self |
|
35
|
|
|
*/ |
|
36
|
|
|
public function caseSensitive(): self |
|
37
|
|
|
{ |
|
38
|
|
|
$new = clone $this; |
|
39
|
|
|
$new->pattern = $this->pattern->ignoreCase(false); |
|
40
|
|
|
return $new; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Match full path, not just ending of path. |
|
45
|
|
|
* @return self |
|
46
|
|
|
*/ |
|
47
|
|
|
public function withFullPath(): self |
|
48
|
|
|
{ |
|
49
|
|
|
$new = clone $this; |
|
50
|
|
|
$new->pattern = $this->pattern->withEnding(false); |
|
51
|
|
|
return $new; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Match `/` character with wildcards. |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
|
|
public function withNotExactSlashes(): self |
|
59
|
|
|
{ |
|
60
|
|
|
$new = clone $this; |
|
61
|
|
|
$new->pattern = $this->pattern->withExactSlashes(false); |
|
62
|
|
|
return $new; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* If path is not file match return null |
|
67
|
|
|
* @return self |
|
68
|
|
|
*/ |
|
69
|
|
|
public function onlyFiles(): self |
|
70
|
|
|
{ |
|
71
|
|
|
$new = clone $this; |
|
72
|
|
|
$new->matchOnly = self::FILES; |
|
73
|
|
|
return $new; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* If path is not directory match return null |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
|
|
public function onlyDirectories(): self |
|
81
|
|
|
{ |
|
82
|
|
|
$new = clone $this; |
|
83
|
|
|
$new->matchOnly = self::DIRECTORIES; |
|
84
|
|
|
return $new; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Checks if the passed path would match the given shell path pattern. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $path The tested path. |
|
91
|
|
|
* @return bool|null Whether the path matches pattern or not. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function match(string $path): ?bool |
|
94
|
|
|
{ |
|
95
|
|
|
if ( |
|
96
|
|
|
($this->matchOnly === self::FILES && is_dir($path)) || |
|
97
|
|
|
($this->matchOnly === self::DIRECTORIES && is_file($path)) |
|
98
|
|
|
) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->pattern->match($path); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|