/vendor/phing/phing/classes/phing/util/PathTokenizer.php

https://gitlab.com/Isaki/le331.fr · PHP · 165 lines · 45 code · 41 blank · 79 comment · 11 complexity · 5614af4daf245abce1f37538e54eddac MD5 · raw file

  1. <?php
  2. /**
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information please see
  17. * <http://phing.info>.
  18. */
  19. include_once 'phing/util/StringHelper.php';
  20. /**
  21. * A Path tokenizer takes a path and returns the components that make up
  22. * that path.
  23. *
  24. * The path can use path separators of either ':' or ';' and file separators
  25. * of either '/' or '\'.
  26. *
  27. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  28. * @author Conor MacNeill (Ant)
  29. * @author Jeff Tulley <jtulley@novell.com> (Ant)
  30. *
  31. * @package phing.util
  32. */
  33. class PathTokenizer
  34. {
  35. /**
  36. * A array of tokens, created by preg_split().
  37. */
  38. private $tokens = array();
  39. /**
  40. * A string which stores any path components which have been read ahead
  41. * due to DOS filesystem compensation.
  42. * @var string
  43. */
  44. private $lookahead;
  45. /**
  46. * Flag to indicate whether or not we are running on a platform with a
  47. * DOS style filesystem
  48. * @var boolean
  49. */
  50. private $dosStyleFilesystem;
  51. /**
  52. * Constructs a path tokenizer for the specified path.
  53. *
  54. * @param string $path The path to tokenize. Must not be <code>null</code>.
  55. */
  56. public function __construct($path)
  57. {
  58. // on Windows and Unix, we can ignore delimiters and still have
  59. // enough information to tokenize correctly.
  60. $this->tokens = preg_split("/[;:]/", $path, -1, PREG_SPLIT_NO_EMPTY);
  61. $this->dosStyleFilesystem = (PATH_SEPARATOR == ';');
  62. }
  63. /**
  64. * Tests if there are more path elements available from this tokenizer's
  65. * path. If this method returns <code>true</code>, then a subsequent call
  66. * to nextToken will successfully return a token.
  67. *
  68. * @return bool <code>true</code> if and only if there is at least one token
  69. * in the string after the current position; <code>false</code> otherwise.
  70. */
  71. public function hasMoreTokens()
  72. {
  73. if ($this->lookahead !== null) {
  74. return true;
  75. }
  76. return !empty($this->tokens);
  77. }
  78. /**
  79. * Returns the next path element from this tokenizer.
  80. *
  81. * @return string the next path element from this tokenizer.
  82. *
  83. * @throws Exception if there are no more elements in this tokenizer's path.
  84. */
  85. public function nextToken()
  86. {
  87. if ($this->lookahead !== null) {
  88. $token = $this->lookahead;
  89. $this->lookahead = null;
  90. } else {
  91. $token = trim(array_shift($this->tokens));
  92. }
  93. if (strlen($token) === 1 && Character::isLetter($token{0})
  94. && $this->dosStyleFilesystem
  95. && !empty($this->tokens)
  96. ) {
  97. // we are on a dos style system so this path could be a drive
  98. // spec. We look at the next token
  99. $nextToken = trim(array_shift($this->tokens));
  100. if (StringHelper::startsWith('\\', $nextToken) || StringHelper::startsWith('/', $nextToken)) {
  101. // we know we are on a DOS style platform and the next path
  102. // starts with a slash or backslash, so we know this is a
  103. // drive spec
  104. $token .= ':' . $nextToken;
  105. } else {
  106. // store the token just read for next time
  107. $this->lookahead = $nextToken;
  108. }
  109. }
  110. return $token;
  111. }
  112. /**
  113. * Non StringTokenizer function, that indicates whether the specified path is contained in loaded tokens.
  114. * We can do this easily because in PHP implimentation we're using arrays.
  115. *
  116. * @param string $path path to search for.
  117. *
  118. * @return boolean
  119. */
  120. public function contains($path)
  121. {
  122. return in_array($path, $this->tokens, true);
  123. }
  124. }