Final Class Yiisoft\Files\PathMatcher\PathMatcher
| Inheritance | Yiisoft\Files\PathMatcher\PathMatcher |
|---|---|
| Implements | Yiisoft\Files\PathMatcher\PathMatcherInterface |
Path matcher is based on Yiisoft\Files\PathMatcher\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 Yiisoft\Files\PathMatcher\PathMatcherInterface or strings could be used in all above. They will be converted Yiisoft\Files\PathMatcher\PathPattern according to the options.
If the string ends in /, then Yiisoft\Files\PathMatcher\PathPattern will be created with Yiisoft\Files\PathMatcher\PathPattern::onlyDirectories() option.
Else it will be create with Yiisoft\Files\PathMatcher\PathPattern::onlyFiles() option. You can disable this behavior using
\Yiisoft\Files\PathMatcher\PathMatcher::notCheckFilesystem().
There are several other options available:
- Yiisoft\Files\PathMatcher\PathMatcher::caseSensitive() makes string patterns case sensitive;
- \Yiisoft\Files\PathMatcher\PathMatcher::withFullPath() string patterns will be matched as full path, not just as ending of the path;
- \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 callback( callable $callbacks ): self | ||
| $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 caseSensitive( ): self |
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 doNotCheckFilesystem( ): self |
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 except( Yiisoft\Files\PathMatcher\PathMatcherInterface|string $patterns ): self | ||
| $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 match( string $path ): boolean | ||
| $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 only( Yiisoft\Files\PathMatcher\PathMatcherInterface|string $patterns ): self | ||
| $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.