PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Framework/WebBundle/Util/Glob.php

https://github.com/hartym/symfony
PHP | 132 lines | 84 code | 11 blank | 37 comment | 16 complexity | 0ee04ab2a493bfd906308b56fb368f5b MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. namespace Symfony\Framework\WebBundle\Util;
  3. /*
  4. * This file is part of the symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Match 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 perl Text::Glob module.
  27. *
  28. * @package symfony
  29. * @author Fabien Potencier <fabien.potencier@gmail.com> php port
  30. * @author Richard Clamp <richardc@unixbeard.net> perl version
  31. * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com>
  32. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  33. */
  34. class Glob
  35. {
  36. protected static $strict_leading_dot = true;
  37. protected static $strict_wildcard_slash = true;
  38. public static function setStrictLeadingDot($boolean)
  39. {
  40. self::$strict_leading_dot = $boolean;
  41. }
  42. public static function setStrictWildcardSlash($boolean)
  43. {
  44. self::$strict_wildcard_slash = $boolean;
  45. }
  46. /**
  47. * Returns a compiled regex which is the equiavlent of the globbing pattern.
  48. *
  49. * @param string $glob pattern
  50. * @return string regex
  51. */
  52. public static function toRegex($glob)
  53. {
  54. $first_byte = true;
  55. $escaping = false;
  56. $in_curlies = 0;
  57. $regex = '';
  58. $sizeGlob = strlen($glob);
  59. for ($i = 0; $i < $sizeGlob; $i++)
  60. {
  61. $car = $glob[$i];
  62. if ($first_byte)
  63. {
  64. if (self::$strict_leading_dot && $car !== '.')
  65. {
  66. $regex .= '(?=[^\.])';
  67. }
  68. $first_byte = false;
  69. }
  70. if ($car === '/')
  71. {
  72. $first_byte = true;
  73. }
  74. if ($car === '.' || $car === '(' || $car === ')' || $car === '|' || $car === '+' || $car === '^' || $car === '$')
  75. {
  76. $regex .= "\\$car";
  77. }
  78. elseif ($car === '*')
  79. {
  80. $regex .= ($escaping ? '\\*' : (self::$strict_wildcard_slash ? '[^/]*' : '.*'));
  81. }
  82. elseif ($car === '?')
  83. {
  84. $regex .= ($escaping ? '\\?' : (self::$strict_wildcard_slash ? '[^/]' : '.'));
  85. }
  86. elseif ($car === '{')
  87. {
  88. $regex .= ($escaping ? '\\{' : '(');
  89. if (!$escaping) ++$in_curlies;
  90. }
  91. elseif ($car === '}' && $in_curlies)
  92. {
  93. $regex .= ($escaping ? '}' : ')');
  94. if (!$escaping) --$in_curlies;
  95. }
  96. elseif ($car === ',' && $in_curlies)
  97. {
  98. $regex .= ($escaping ? ',' : '|');
  99. }
  100. elseif ($car === '\\')
  101. {
  102. if ($escaping)
  103. {
  104. $regex .= '\\\\';
  105. $escaping = false;
  106. }
  107. else
  108. {
  109. $escaping = true;
  110. }
  111. continue;
  112. }
  113. else
  114. {
  115. $regex .= $car;
  116. }
  117. $escaping = false;
  118. }
  119. return '#^'.$regex.'$#';
  120. }
  121. }