Comparator
3 years ago
Exception
3 years ago
Iterator
3 years ago
Finder.php
4 years ago
Gitignore.php
4 years ago
Glob.php
4 years ago
SplFileInfo.php
4 years ago
index.php
3 years ago
Gitignore.php
54 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Finder; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class Gitignore |
| 5 | { |
| 6 | public static function toRegex(string $gitignoreFileContent) : string |
| 7 | { |
| 8 | $gitignoreFileContent = \preg_replace('~(?<!\\\\)#[^\\n\\r]*~', '', $gitignoreFileContent); |
| 9 | $gitignoreLines = \preg_split('~\\r\\n?|\\n~', $gitignoreFileContent); |
| 10 | $res = self::lineToRegex(''); |
| 11 | foreach ($gitignoreLines as $i => $line) { |
| 12 | $line = \preg_replace('~(?<!\\\\)[ \\t]+$~', '', $line); |
| 13 | if ('!' === \substr($line, 0, 1)) { |
| 14 | $line = \substr($line, 1); |
| 15 | $isNegative = \true; |
| 16 | } else { |
| 17 | $isNegative = \false; |
| 18 | } |
| 19 | if ('' !== $line) { |
| 20 | if ($isNegative) { |
| 21 | $res = '(?!' . self::lineToRegex($line) . '$)' . $res; |
| 22 | } else { |
| 23 | $res = '(?:' . $res . '|' . self::lineToRegex($line) . ')'; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | return '~^(?:' . $res . ')~s'; |
| 28 | } |
| 29 | private static function lineToRegex(string $gitignoreLine) : string |
| 30 | { |
| 31 | if ('' === $gitignoreLine) { |
| 32 | return '$f'; |
| 33 | // always false |
| 34 | } |
| 35 | $slashPos = \strpos($gitignoreLine, '/'); |
| 36 | if (\false !== $slashPos && \strlen($gitignoreLine) - 1 !== $slashPos) { |
| 37 | if (0 === $slashPos) { |
| 38 | $gitignoreLine = \substr($gitignoreLine, 1); |
| 39 | } |
| 40 | $isAbsolute = \true; |
| 41 | } else { |
| 42 | $isAbsolute = \false; |
| 43 | } |
| 44 | $regex = \preg_quote(\str_replace('\\', '', $gitignoreLine), '~'); |
| 45 | $regex = \preg_replace_callback('~\\\\\\[((?:\\\\!)?)([^\\[\\]]*)\\\\\\]~', function (array $matches) : string { |
| 46 | return '[' . ('' !== $matches[1] ? '^' : '') . \str_replace('\\-', '-', $matches[2]) . ']'; |
| 47 | }, $regex); |
| 48 | $regex = \preg_replace('~(?:(?:\\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?<!//))+$1)?', $regex); |
| 49 | $regex = \preg_replace('~\\\\\\*~', '[^/]*', $regex); |
| 50 | $regex = \preg_replace('~\\\\\\?~', '[^/]', $regex); |
| 51 | return ($isAbsolute ? '' : '(?:[^/]+/)*') . $regex . (!\str_ends_with($gitignoreLine, '/') ? '(?:$|/)' : ''); |
| 52 | } |
| 53 | } |
| 54 |