Final Class Yiisoft\Files\PathMatcher\PathMatcher
| Inheritance | Yiisoft\Files\PathMatcher\PathMatcher |
|---|---|
| Implements | Yiisoft\Files\PathMatcher\PathMatcherInterface |
Path matcher is based on {@see PathPattern} with the following logic:
- Process
only(). If there is at least one match, then continue, else returnfalse; - Process
except(). If there is at least one match, returnfalse, else continue; - Process
callback(). If there is at least one not match, returnfalse, else returntrue.
Either implementations of {@see \Yiisoft\Files\PathMatcher\PathMatcherInterface} or strings could be used in all above. They will be converted {@see \Yiisoft\Files\PathMatcher\PathPattern} according to the options.
If the string ends in /, then {@see \Yiisoft\Files\PathMatcher\PathPattern} will be created with {@see \Yiisoft\Files\PathMatcher\PathPattern::onlyDirectories()} option.
Else it will be create with {@see \Yiisoft\Files\PathMatcher\PathPattern::onlyFiles()} option. You can disable this behavior using
{@see \Yiisoft\Files\PathMatcher\PathMatcher::notCheckFilesystem()}.
There are several other options available:
- {@see \Yiisoft\Files\PathMatcher\PathMatcher::caseSensitive()} makes string patterns case sensitive;
- {@see \Yiisoft\Files\PathMatcher\PathMatcher::withFullPath()} string patterns will be matched as full path, not just as ending of the path;
- {@see \Yiisoft\Files\PathMatcher\PathMatcher::withNotExactSlashes()} match
/character with wildcards in string patterns.
Usage example:
$matcher = (new PathMatcher())
->notCheckFilesystem()
->only('*.css', '*.js')
->except('theme.css');
$matcher->match('/var/www/example.com/assets/css/main.css'); // true
$matcher->match('/var/www/example.com/assets/css/main.css.map'); // false
$matcher->match('/var/www/example.com/assets/css/theme.css'); // false
Public Methods
| Method | Description | Defined By |
|---|---|---|
| callback() | Set list of PHP callbacks that are called for each path. | Yiisoft\Files\PathMatcher\PathMatcher |
| caseSensitive() | Make string patterns case sensitive. | Yiisoft\Files\PathMatcher\PathMatcher |
| doNotCheckFilesystem() | Match path only as string, do not check if file or directory exists. | Yiisoft\Files\PathMatcher\PathMatcher |
| except() | Set list of patterns that the files or directories should not match. | Yiisoft\Files\PathMatcher\PathMatcher |
| match() | Checks if the passed path match specified conditions. | Yiisoft\Files\PathMatcher\PathMatcher |
| only() | Set list of patterns that the files or directories should match. | Yiisoft\Files\PathMatcher\PathMatcher |
Method Details
Set list of PHP callbacks that are called for each path.
The signature of the callback should be: function ($path), where $path refers the full path to be filtered.
The callback should return true if there is a match and false otherwise.
| public self callback ( callable $callbacks ) | ||
| $callbacks | callable | |
public function callback(callable ...$callbacks): self
{
$new = clone $this;
$new->callbacks = $callbacks;
return $new;
}
Make string patterns case sensitive.
Note: applies only to string patterns.
| public self caseSensitive ( ) |
public function caseSensitive(): self
{
$new = clone $this;
$new->caseSensitive = true;
return $new;
}
Match path only as string, do not check if file or directory exists.
Note: applies only to string patterns.
| public self doNotCheckFilesystem ( ) |
public function doNotCheckFilesystem(): self
{
$new = clone $this;
$new->checkFilesystem = false;
return $new;
}
Set list of patterns that the files or directories should not match.
See also https://github.com/yiisoft/strings#wildcardpattern-usage.
| public self except ( Yiisoft\Files\PathMatcher\PathMatcherInterface|string $patterns ) | ||
| $patterns | Yiisoft\Files\PathMatcher\PathMatcherInterface|string |
Simple POSIX-style string matching. |
public function except(PathMatcherInterface|string ...$patterns): self
{
$new = clone $this;
$new->except = $this->prepareMatchers($patterns);
return $new;
}
Checks if the passed path match specified conditions.
| public boolean match ( string $path ) | ||
| $path | string |
The tested path. |
| return | boolean |
Whether the path matches conditions or not. |
|---|---|---|
public function match(string $path): bool
{
if (!$this->matchOnly($path) || $this->matchExcept($path)) {
return false;
}
if ($this->callbacks !== null) {
foreach ($this->callbacks as $callback) {
if (!$callback($path)) {
return false;
}
}
}
return true;
}
Set list of patterns that the files or directories should match.
| public self only ( Yiisoft\Files\PathMatcher\PathMatcherInterface|string $patterns ) | ||
| $patterns | Yiisoft\Files\PathMatcher\PathMatcherInterface|string | |
public function only(PathMatcherInterface|string ...$patterns): self
{
$new = clone $this;
$new->only = $this->prepareMatchers($patterns);
return $new;
}
Signup or Login in order to comment.