Glob.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Expression;
  11. /**
  12. * @author Jean-François Simon <contact@jfsimon.fr>
  13. */
  14. class Glob implements ValueInterface
  15. {
  16. /**
  17. * @var string
  18. */
  19. private $pattern;
  20. /**
  21. * @param string $pattern
  22. */
  23. public function __construct($pattern)
  24. {
  25. $this->pattern = $pattern;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function render()
  31. {
  32. return $this->pattern;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function renderPattern()
  38. {
  39. return $this->pattern;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getType()
  45. {
  46. return Expression::TYPE_GLOB;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function isCaseSensitive()
  52. {
  53. return true;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function prepend($expr)
  59. {
  60. $this->pattern = $expr.$this->pattern;
  61. return $this;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function append($expr)
  67. {
  68. $this->pattern .= $expr;
  69. return $this;
  70. }
  71. /**
  72. * Tests if glob is expandable ("*.{a,b}" syntax).
  73. *
  74. * @return bool
  75. */
  76. public function isExpandable()
  77. {
  78. return false !== strpos($this->pattern, '{')
  79. && false !== strpos($this->pattern, '}');
  80. }
  81. /**
  82. * @param bool $strictLeadingDot
  83. * @param bool $strictWildcardSlash
  84. *
  85. * @return Regex
  86. */
  87. public function toRegex($strictLeadingDot = true, $strictWildcardSlash = true)
  88. {
  89. $firstByte = true;
  90. $escaping = false;
  91. $inCurlies = 0;
  92. $regex = '';
  93. $sizeGlob = strlen($this->pattern);
  94. for ($i = 0; $i < $sizeGlob; $i++) {
  95. $car = $this->pattern[$i];
  96. if ($firstByte) {
  97. if ($strictLeadingDot && '.' !== $car) {
  98. $regex .= '(?=[^\.])';
  99. }
  100. $firstByte = false;
  101. }
  102. if ('/' === $car) {
  103. $firstByte = true;
  104. }
  105. if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
  106. $regex .= "\\$car";
  107. } elseif ('*' === $car) {
  108. $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
  109. } elseif ('?' === $car) {
  110. $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
  111. } elseif ('{' === $car) {
  112. $regex .= $escaping ? '\\{' : '(';
  113. if (!$escaping) {
  114. ++$inCurlies;
  115. }
  116. } elseif ('}' === $car && $inCurlies) {
  117. $regex .= $escaping ? '}' : ')';
  118. if (!$escaping) {
  119. --$inCurlies;
  120. }
  121. } elseif (',' === $car && $inCurlies) {
  122. $regex .= $escaping ? ',' : '|';
  123. } elseif ('\\' === $car) {
  124. if ($escaping) {
  125. $regex .= '\\\\';
  126. $escaping = false;
  127. } else {
  128. $escaping = true;
  129. }
  130. continue;
  131. } else {
  132. $regex .= $car;
  133. }
  134. $escaping = false;
  135. }
  136. return new Regex('^'.$regex.'$');
  137. }
  138. }