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

/htdocs/symfony/2.0.0pr2/src/vendor/symfony/src/Symfony/Components/Finder/Glob.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 103 lines | 56 code | 8 blank | 39 comment | 18 complexity | f9bae6b3a434a3c34b8386a507787c3d 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\Components\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. * @package Symfony
  29. * @subpackage Components_Finder
  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. /**
  38. * Returns a regexp which is the equivalent of the glob pattern.
  39. *
  40. * @param string $glob The glob pattern
  41. *
  42. * @return string regex The regexp
  43. */
  44. static public function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
  45. {
  46. $firstByte = true;
  47. $escaping = false;
  48. $inCurlies = 0;
  49. $regex = '';
  50. $sizeGlob = strlen($glob);
  51. for ($i = 0; $i < $sizeGlob; $i++) {
  52. $car = $glob[$i];
  53. if ($firstByte) {
  54. if ($strictLeadingDot && $car !== '.') {
  55. $regex .= '(?=[^\.])';
  56. }
  57. $firstByte = false;
  58. }
  59. if ($car === '/') {
  60. $firstByte = true;
  61. }
  62. if ($car === '.' || $car === '(' || $car === ')' || $car === '|' || $car === '+' || $car === '^' || $car === '$') {
  63. $regex .= "\\$car";
  64. } elseif ($car === '*') {
  65. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  66. } elseif ($car === '?') {
  67. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  68. } elseif ($car === '{') {
  69. $regex .= $escaping ? '\\{' : '(';
  70. if (!$escaping) {
  71. ++$inCurlies;
  72. }
  73. } elseif ($car === '}' && $inCurlies) {
  74. $regex .= $escaping ? '}' : ')';
  75. if (!$escaping) {
  76. --$inCurlies;
  77. }
  78. } elseif ($car === ',' && $inCurlies) {
  79. $regex .= $escaping ? ',' : '|';
  80. } elseif ($car === '\\') {
  81. if ($escaping) {
  82. $regex .= '\\\\';
  83. $escaping = false;
  84. } else {
  85. $escaping = true;
  86. }
  87. continue;
  88. } else {
  89. $regex .= $car;
  90. }
  91. $escaping = false;
  92. }
  93. return '#^'.$regex.'$#';
  94. }
  95. }