You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.5 KiB

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