Glob.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Glob matches globbing patterns against text.
  13. *
  14. * if match_glob("foo.*", "foo.bar") echo "matched\n";
  15. *
  16. * // prints foo.bar and foo.baz
  17. * $regex = glob_to_regex("foo.*");
  18. * for (['foo.bar', 'foo.baz', 'foo', 'bar'] as $t)
  19. * {
  20. * if (/$regex/) echo "matched: $car\n";
  21. * }
  22. *
  23. * Glob implements glob(3) style matching that can be used to match
  24. * against text, rather than fetching names from a filesystem.
  25. *
  26. * Based on the Perl Text::Glob module.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com> PHP port
  29. * @author Richard Clamp <richardc@unixbeard.net> Perl version
  30. * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
  31. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  32. */
  33. class Glob
  34. {
  35. /**
  36. * Returns a regexp which is the equivalent of the glob pattern.
  37. */
  38. public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#'): string
  39. {
  40. $firstByte = true;
  41. $escaping = false;
  42. $inCurlies = 0;
  43. $regex = '';
  44. if ($unanchored = str_starts_with($glob, '**/')) {
  45. $glob = '/'.$glob;
  46. }
  47. $sizeGlob = \strlen($glob);
  48. for ($i = 0; $i < $sizeGlob; ++$i) {
  49. $car = $glob[$i];
  50. if ($firstByte && $strictLeadingDot && '.' !== $car) {
  51. $regex .= '(?=[^\.])';
  52. }
  53. $firstByte = '/' === $car;
  54. if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
  55. $car = '[^/]++/';
  56. if (!isset($glob[$i + 3])) {
  57. $car .= '?';
  58. }
  59. if ($strictLeadingDot) {
  60. $car = '(?=[^\.])'.$car;
  61. }
  62. $car = '/(?:'.$car.')*';
  63. $i += 2 + isset($glob[$i + 3]);
  64. if ('/' === $delimiter) {
  65. $car = str_replace('/', '\\/', $car);
  66. }
  67. }
  68. if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
  69. $regex .= "\\$car";
  70. } elseif ('*' === $car) {
  71. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  72. } elseif ('?' === $car) {
  73. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  74. } elseif ('{' === $car) {
  75. $regex .= $escaping ? '\\{' : '(';
  76. if (!$escaping) {
  77. ++$inCurlies;
  78. }
  79. } elseif ('}' === $car && $inCurlies) {
  80. $regex .= $escaping ? '}' : ')';
  81. if (!$escaping) {
  82. --$inCurlies;
  83. }
  84. } elseif (',' === $car && $inCurlies) {
  85. $regex .= $escaping ? ',' : '|';
  86. } elseif ('\\' === $car) {
  87. if ($escaping) {
  88. $regex .= '\\\\';
  89. $escaping = false;
  90. } else {
  91. $escaping = true;
  92. }
  93. continue;
  94. } else {
  95. $regex .= $car;
  96. }
  97. $escaping = false;
  98. }
  99. if ($unanchored) {
  100. $regex = substr_replace($regex, '?', 1 + ('/' === $delimiter) + ($strictLeadingDot ? \strlen('(?=[^\.])') : 0), 0);
  101. }
  102. return $delimiter.'^'.$regex.'$'.$delimiter;
  103. }
  104. }