Gitignore.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder;
  11. /**
  12. * Gitignore matches against text.
  13. *
  14. * @author Ahmed Abdou <mail@ahmd.io>
  15. */
  16. class Gitignore
  17. {
  18. /**
  19. * Returns a regexp which is the equivalent of the gitignore pattern.
  20. *
  21. * @return string The regexp
  22. */
  23. public static function toRegex(string $gitignoreFileContent): string
  24. {
  25. $gitignoreFileContent = preg_replace('/^[^\\\r\n]*#.*/m', '', $gitignoreFileContent);
  26. $gitignoreLines = preg_split('/\r\n|\r|\n/', $gitignoreFileContent);
  27. $gitignoreLines = array_map('trim', $gitignoreLines);
  28. $gitignoreLines = array_filter($gitignoreLines);
  29. $ignoreLinesPositive = array_filter($gitignoreLines, function (string $line) {
  30. return !preg_match('/^!/', $line);
  31. });
  32. $ignoreLinesNegative = array_filter($gitignoreLines, function (string $line) {
  33. return preg_match('/^!/', $line);
  34. });
  35. $ignoreLinesNegative = array_map(function (string $line) {
  36. return preg_replace('/^!(.*)/', '${1}', $line);
  37. }, $ignoreLinesNegative);
  38. $ignoreLinesNegative = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesNegative);
  39. $ignoreLinesPositive = array_map([__CLASS__, 'getRegexFromGitignore'], $ignoreLinesPositive);
  40. if (empty($ignoreLinesPositive)) {
  41. return '/^$/';
  42. }
  43. if (empty($ignoreLinesNegative)) {
  44. return sprintf('/%s/', implode('|', $ignoreLinesPositive));
  45. }
  46. return sprintf('/(?=^(?:(?!(%s)).)*$)(%s)/', implode('|', $ignoreLinesNegative), implode('|', $ignoreLinesPositive));
  47. }
  48. private static function getRegexFromGitignore(string $gitignorePattern): string
  49. {
  50. $regex = '(';
  51. if (0 === strpos($gitignorePattern, '/')) {
  52. $gitignorePattern = substr($gitignorePattern, 1);
  53. $regex .= '^';
  54. } else {
  55. $regex .= '(^|\/)';
  56. }
  57. if ('/' === $gitignorePattern[\strlen($gitignorePattern) - 1]) {
  58. $gitignorePattern = substr($gitignorePattern, 0, -1);
  59. }
  60. $iMax = \strlen($gitignorePattern);
  61. for ($i = 0; $i < $iMax; ++$i) {
  62. $doubleChars = substr($gitignorePattern, $i, 2);
  63. if ('**' === $doubleChars) {
  64. $regex .= '.+';
  65. ++$i;
  66. continue;
  67. }
  68. $c = $gitignorePattern[$i];
  69. switch ($c) {
  70. case '*':
  71. $regex .= '[^\/]+';
  72. break;
  73. case '/':
  74. case '.':
  75. case ':':
  76. case '(':
  77. case ')':
  78. case '{':
  79. case '}':
  80. $regex .= '\\'.$c;
  81. break;
  82. default:
  83. $regex .= $c;
  84. }
  85. }
  86. $regex .= '($|\/)';
  87. $regex .= ')';
  88. return $regex;
  89. }
  90. }