| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\Finder; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gitignore matches against text. |
| 15 |
* |
| 16 |
* @author Michael Voříšek <vorismi3@fel.cvut.cz> |
| 17 |
* @author Ahmed Abdou <mail@ahmd.io> |
| 18 |
*/ |
| 19 |
class Gitignore |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Returns a regexp which is the equivalent of the gitignore pattern. |
| 23 |
* |
| 24 |
* Format specification: https://git-scm.com/docs/gitignore#_pattern_format |
| 25 |
*/ |
| 26 |
public static function toRegex(string $gitignoreFileContent): string |
| 27 |
{ |
| 28 |
return self::buildRegex($gitignoreFileContent, \false); |
| 29 |
} |
| 30 |
public static function toRegexMatchingNegatedPatterns(string $gitignoreFileContent): string |
| 31 |
{ |
| 32 |
return self::buildRegex($gitignoreFileContent, \true); |
| 33 |
} |
| 34 |
private static function buildRegex(string $gitignoreFileContent, bool $inverted): string |
| 35 |
{ |
| 36 |
$gitignoreFileContent = preg_replace('~(?<!\\\\)#[^\n\r]*~', '', $gitignoreFileContent); |
| 37 |
$gitignoreLines = preg_split('~\r\n?|\n~', $gitignoreFileContent); |
| 38 |
$res = self::lineToRegex(''); |
| 39 |
foreach ($gitignoreLines as $line) { |
| 40 |
$line = preg_replace('~(?<!\\\\)[ \t]+$~', '', $line); |
| 41 |
if ('!' === substr($line, 0, 1)) { |
| 42 |
$line = substr($line, 1); |
| 43 |
$isNegative = \true; |
| 44 |
} else { |
| 45 |
$isNegative = \false; |
| 46 |
} |
| 47 |
if ('' !== $line) { |
| 48 |
if ($isNegative xor $inverted) { |
| 49 |
$res = '(?!' . self::lineToRegex($line) . '$)' . $res; |
| 50 |
} else { |
| 51 |
$res = '(?:' . $res . '|' . self::lineToRegex($line) . ')'; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
return '~^(?:' . $res . ')~s'; |
| 56 |
} |
| 57 |
private static function lineToRegex(string $gitignoreLine): string |
| 58 |
{ |
| 59 |
if ('' === $gitignoreLine) { |
| 60 |
return '$f'; |
| 61 |
// always false |
| 62 |
} |
| 63 |
$slashPos = strpos($gitignoreLine, '/'); |
| 64 |
if (\false !== $slashPos && \strlen($gitignoreLine) - 1 !== $slashPos) { |
| 65 |
if (0 === $slashPos) { |
| 66 |
$gitignoreLine = substr($gitignoreLine, 1); |
| 67 |
} |
| 68 |
$isAbsolute = \true; |
| 69 |
} else { |
| 70 |
$isAbsolute = \false; |
| 71 |
} |
| 72 |
$regex = preg_quote(str_replace('\\', '', $gitignoreLine), '~'); |
| 73 |
$regex = preg_replace_callback('~\\\\\\[((?:\\\\!)?)([^\[\]]*)\\\\\\]~', function (array $matches): string { |
| 74 |
return '[' . ('' !== $matches[1] ? '^' : '') . str_replace('\-', '-', $matches[2]) . ']'; |
| 75 |
}, $regex); |
| 76 |
$regex = preg_replace('~(?:(?:\\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?<!//))+$1)?', $regex); |
| 77 |
$regex = preg_replace('~\\\\\\*~', '[^/]*', $regex); |
| 78 |
$regex = preg_replace('~\\\\\\?~', '[^/]', $regex); |
| 79 |
return ($isAbsolute ? '' : '(?:[^/]+/)*') . $regex . (!str_ends_with($gitignoreLine, '/') ? '(?:$|/)' : ''); |
| 80 |
} |
| 81 |
} |
| 82 |
|