PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Finder/Glob.php

https://github.com/sebastian-kopatz/symfony
PHP | 101 lines | 56 code | 8 blank | 37 comment | 18 complexity | 80450754dbc23fed0ac3cf2fe2e2e111 MD5 | raw file
  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. *
  40. * @return string regex The regexp
  41. */
  42. static public function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
  43. {
  44. $firstByte = true;
  45. $escaping = false;
  46. $inCurlies = 0;
  47. $regex = '';
  48. $sizeGlob = strlen($glob);
  49. for ($i = 0; $i < $sizeGlob; $i++) {
  50. $car = $glob[$i];
  51. if ($firstByte) {
  52. if ($strictLeadingDot && '.' !== $car) {
  53. $regex .= '(?=[^\.])';
  54. }
  55. $firstByte = false;
  56. }
  57. if ('/' === $car) {
  58. $firstByte = true;
  59. }
  60. if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
  61. $regex .= "\\$car";
  62. } elseif ('*' === $car) {
  63. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  64. } elseif ('?' === $car) {
  65. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  66. } elseif ('{' === $car) {
  67. $regex .= $escaping ? '\\{' : '(';
  68. if (!$escaping) {
  69. ++$inCurlies;
  70. }
  71. } elseif ('}' === $car && $inCurlies) {
  72. $regex .= $escaping ? '}' : ')';
  73. if (!$escaping) {
  74. --$inCurlies;
  75. }
  76. } elseif (',' === $car && $inCurlies) {
  77. $regex .= $escaping ? ',' : '|';
  78. } elseif ('\\' === $car) {
  79. if ($escaping) {
  80. $regex .= '\\\\';
  81. $escaping = false;
  82. } else {
  83. $escaping = true;
  84. }
  85. continue;
  86. } else {
  87. $regex .= $car;
  88. }
  89. $escaping = false;
  90. }
  91. return '#^'.$regex.'$#';
  92. }
  93. }