PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Stdlib/Glob.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 197 lines | 143 code | 13 blank | 41 comment | 17 complexity | 7f88dbf39acf970d76ba5c970982e04b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Stdlib
  9. */
  10. namespace Zend\Stdlib;
  11. /**
  12. * Wrapper for glob with fallback if GLOB_BRACE is not available.
  13. *
  14. * @category Zend
  15. * @package Zend_Stdlib
  16. */
  17. abstract class Glob
  18. {
  19. /**#@+
  20. * Glob constants.
  21. */
  22. const GLOB_MARK = 0x01;
  23. const GLOB_NOSORT = 0x02;
  24. const GLOB_NOCHECK = 0x04;
  25. const GLOB_NOESCAPE = 0x08;
  26. const GLOB_BRACE = 0x10;
  27. const GLOB_ONLYDIR = 0x20;
  28. const GLOB_ERR = 0x40;
  29. /**#@-*/
  30. /**
  31. * Find pathnames matching a pattern.
  32. *
  33. * @see http://docs.php.net/glob
  34. * @param string $pattern
  35. * @param integer $flags
  36. * @param boolean $forceFallback
  37. * @return array|false
  38. */
  39. public static function glob($pattern, $flags, $forceFallback = false)
  40. {
  41. if (!defined('GLOB_BRACE') || $forceFallback) {
  42. return static::fallbackGlob($pattern, $flags);
  43. } else {
  44. return static::systemGlob($pattern, $flags);
  45. }
  46. }
  47. /**
  48. * Use the glob function provided by the system.
  49. *
  50. * @param string $pattern
  51. * @param integer $flags
  52. * @return array|false
  53. */
  54. protected static function systemGlob($pattern, $flags)
  55. {
  56. if ($flags) {
  57. $flagMap = array(
  58. self::GLOB_MARK => GLOB_MARK,
  59. self::GLOB_NOSORT => GLOB_NOSORT,
  60. self::GLOB_NOCHECK => GLOB_NOCHECK,
  61. self::GLOB_NOESCAPE => GLOB_NOESCAPE,
  62. self::GLOB_BRACE => GLOB_BRACE,
  63. self::GLOB_ONLYDIR => GLOB_ONLYDIR,
  64. self::GLOB_ERR => GLOB_ERR,
  65. );
  66. $globFlags = 0;
  67. foreach ($flagMap as $internalFlag => $globFlag) {
  68. if ($flags & $internalFlag) {
  69. $globFlags |= $globFlag;
  70. }
  71. }
  72. } else {
  73. $globFlags = 0;
  74. }
  75. return glob($pattern, $globFlags);
  76. }
  77. /**
  78. * Expand braces manually, then use the system glob.
  79. *
  80. * @param string $pattern
  81. * @param integer $flags
  82. * @return array|false
  83. */
  84. protected static function fallbackGlob($pattern, $flags)
  85. {
  86. if (!$flags & self::GLOB_BRACE) {
  87. return static::systemGlob($pattern, $flags);
  88. }
  89. $flags &= ~self::GLOB_BRACE;
  90. $length = strlen($pattern);
  91. $paths = array();
  92. if ($flags & self::GLOB_NOESCAPE) {
  93. $begin = strpos($pattern, '{');
  94. } else {
  95. $begin = 0;
  96. while (true) {
  97. if ($begin === $length) {
  98. $begin = false;
  99. break;
  100. } elseif ($pattern[$begin] === '\\' && ($begin + 1) < $length) {
  101. $begin++;
  102. } elseif ($pattern[$begin] === '{') {
  103. break;
  104. }
  105. $begin++;
  106. }
  107. }
  108. if ($begin === false) {
  109. return static::systemGlob($pattern, $flags);
  110. }
  111. $next = static::nextBraceSub($pattern, $begin + 1, $flags);
  112. if ($next === null) {
  113. return static::systemGlob($pattern, $flags);
  114. }
  115. $rest = $next;
  116. while ($pattern[$rest] !== '}') {
  117. $rest = static::nextBraceSub($pattern, $rest + 1, $flags);
  118. if ($rest === null) {
  119. return static::systemGlob($pattern, $flags);
  120. }
  121. }
  122. $p = $begin + 1;
  123. while (true) {
  124. $subPattern = substr($pattern, 0, $begin)
  125. . substr($pattern, $p, $next - $p)
  126. . substr($pattern, $rest + 1);
  127. $result = static::fallbackGlob($subPattern, $flags | self::GLOB_BRACE);
  128. if ($result) {
  129. $paths = array_merge($paths, $result);
  130. }
  131. if ($pattern[$next] === '}') {
  132. break;
  133. }
  134. $p = $next + 1;
  135. $next = static::nextBraceSub($pattern, $p, $flags);
  136. }
  137. return array_unique($paths);
  138. }
  139. /**
  140. * Find the end of the sub-pattern in a brace expression.
  141. *
  142. * @param string $pattern
  143. * @param integer $begin
  144. * @param integer $flags
  145. * @return integer|null
  146. */
  147. protected static function nextBraceSub($pattern, $begin, $flags)
  148. {
  149. $length = strlen($pattern);
  150. $depth = 0;
  151. $current = $begin;
  152. while ($current < $length) {
  153. if (!$flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') {
  154. if (++$current === $length) {
  155. break;
  156. }
  157. $current++;
  158. } else {
  159. if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) {
  160. break;
  161. } elseif ($pattern[$current++] === '{') {
  162. $depth++;
  163. }
  164. }
  165. }
  166. return ($current < $length ? $current : null);
  167. }
  168. }