Glob.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $sizeGlob = \strlen($glob);
  45. for ($i = 0; $i < $sizeGlob; ++$i) {
  46. $car = $glob[$i];
  47. if ($firstByte && $strictLeadingDot && '.' !== $car) {
  48. $regex .= '(?=[^\.])';
  49. }
  50. $firstByte = '/' === $car;
  51. if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
  52. $car = '[^/]++/';
  53. if (!isset($glob[$i + 3])) {
  54. $car .= '?';
  55. }
  56. if ($strictLeadingDot) {
  57. $car = '(?=[^\.])'.$car;
  58. }
  59. $car = '/(?:'.$car.')*';
  60. $i += 2 + isset($glob[$i + 3]);
  61. if ('/' === $delimiter) {
  62. $car = str_replace('/', '\\/', $car);
  63. }
  64. }
  65. if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
  66. $regex .= "\\$car";
  67. } elseif ('*' === $car) {
  68. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  69. } elseif ('?' === $car) {
  70. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  71. } elseif ('{' === $car) {
  72. $regex .= $escaping ? '\\{' : '(';
  73. if (!$escaping) {
  74. ++$inCurlies;
  75. }
  76. } elseif ('}' === $car && $inCurlies) {
  77. $regex .= $escaping ? '}' : ')';
  78. if (!$escaping) {
  79. --$inCurlies;
  80. }
  81. } elseif (',' === $car && $inCurlies) {
  82. $regex .= $escaping ? ',' : '|';
  83. } elseif ('\\' === $car) {
  84. if ($escaping) {
  85. $regex .= '\\\\';
  86. $escaping = false;
  87. } else {
  88. $escaping = true;
  89. }
  90. continue;
  91. } else {
  92. $regex .= $car;
  93. }
  94. $escaping = false;
  95. }
  96. return $delimiter.'^'.$regex.'$'.$delimiter;
  97. }
  98. }