Glob.php 3.7KB

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