function Gitignore::buildRegex
2 calls to Gitignore::buildRegex()
- Gitignore::toRegex in vendor/
symfony/ finder/ Gitignore.php - Returns a regexp which is the equivalent of the gitignore pattern.
- Gitignore::toRegexMatchingNegatedPatterns in vendor/
symfony/ finder/ Gitignore.php
File
-
vendor/
symfony/ finder/ Gitignore.php, line 37
Class
- Gitignore
- Gitignore matches against text.
Namespace
Symfony\Component\FinderCode
private static function buildRegex(string $gitignoreFileContent, bool $inverted) : string {
$gitignoreFileContent = preg_replace('~(?<!\\\\)#[^\\n\\r]*~', '', $gitignoreFileContent);
$gitignoreLines = preg_split('~\\r\\n?|\\n~', $gitignoreFileContent);
$res = self::lineToRegex('');
foreach ($gitignoreLines as $line) {
$line = preg_replace('~(?<!\\\\)[ \\t]+$~', '', $line);
if (str_starts_with($line, '!')) {
$line = substr($line, 1);
$isNegative = true;
}
else {
$isNegative = false;
}
if ('' !== $line) {
if ($isNegative xor $inverted) {
$res = '(?!' . self::lineToRegex($line) . '$)' . $res;
}
else {
$res = '(?:' . $res . '|' . self::lineToRegex($line) . ')';
}
}
}
return '~^(?:' . $res . ')~s';
}