PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/symfony/src/Symfony/Component/Finder/Glob.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 101 lines | 56 code | 8 blank | 37 comment | 18 complexity | f3f83767527b0a6e8b93303ffe85ed4f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Component\Finder;
  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. * 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.potencier@symfony-project.com> PHP port
  29. * @author Richard Clamp <richardc@unixbeard.net> Perl version
  30. * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.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. }