PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/tidal/symfony
PHP | 133 lines | 84 code | 11 blank | 38 comment | 16 complexity | fc3653b00526d69d4a56c1509b3e4a4b 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. * @subpackage Framework_WebBundle
  30. * @author Fabien Potencier <fabien.potencier@symfony-project.com> php port
  31. * @author Richard Clamp <richardc@unixbeard.net> perl version
  32. * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
  33. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  34. */
  35. class Glob
  36. {
  37. protected static $strict_leading_dot = true;
  38. protected static $strict_wildcard_slash = true;
  39. public static function setStrictLeadingDot($boolean)
  40. {
  41. self::$strict_leading_dot = $boolean;
  42. }
  43. public static function setStrictWildcardSlash($boolean)
  44. {
  45. self::$strict_wildcard_slash = $boolean;
  46. }
  47. /**
  48. * Returns a compiled regex which is the equivalent of the globbing pattern.
  49. *
  50. * @param string $glob pattern
  51. * @return string regex
  52. */
  53. public static function toRegex($glob)
  54. {
  55. $first_byte = true;
  56. $escaping = false;
  57. $in_curlies = 0;
  58. $regex = '';
  59. $sizeGlob = strlen($glob);
  60. for ($i = 0; $i < $sizeGlob; $i++)
  61. {
  62. $car = $glob[$i];
  63. if ($first_byte)
  64. {
  65. if (self::$strict_leading_dot && $car !== '.')
  66. {
  67. $regex .= '(?=[^\.])';
  68. }
  69. $first_byte = false;
  70. }
  71. if ($car === '/')
  72. {
  73. $first_byte = true;
  74. }
  75. if ($car === '.' || $car === '(' || $car === ')' || $car === '|' || $car === '+' || $car === '^' || $car === '$')
  76. {
  77. $regex .= "\\$car";
  78. }
  79. elseif ($car === '*')
  80. {
  81. $regex .= ($escaping ? '\\*' : (self::$strict_wildcard_slash ? '[^/]*' : '.*'));
  82. }
  83. elseif ($car === '?')
  84. {
  85. $regex .= ($escaping ? '\\?' : (self::$strict_wildcard_slash ? '[^/]' : '.'));
  86. }
  87. elseif ($car === '{')
  88. {
  89. $regex .= ($escaping ? '\\{' : '(');
  90. if (!$escaping) ++$in_curlies;
  91. }
  92. elseif ($car === '}' && $in_curlies)
  93. {
  94. $regex .= ($escaping ? '}' : ')');
  95. if (!$escaping) --$in_curlies;
  96. }
  97. elseif ($car === ',' && $in_curlies)
  98. {
  99. $regex .= ($escaping ? ',' : '|');
  100. }
  101. elseif ($car === '\\')
  102. {
  103. if ($escaping)
  104. {
  105. $regex .= '\\\\';
  106. $escaping = false;
  107. }
  108. else
  109. {
  110. $escaping = true;
  111. }
  112. continue;
  113. }
  114. else
  115. {
  116. $regex .= $car;
  117. }
  118. $escaping = false;
  119. }
  120. return '#^'.$regex.'$#';
  121. }
  122. }