/git/src/TQ/Git/StreamWrapper/FileBuffer/StreamBuffer.php

https://github.com/WPsites/WPide · PHP · 176 lines · 60 code · 14 blank · 102 comment · 3 complexity · d255351fc713986324109499c9b53b91 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2011 by TEQneers GmbH & Co. KG
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * Git Streamwrapper for PHP
  25. *
  26. * @category TQ
  27. * @package TQ_Git
  28. * @subpackage StreamWrapper
  29. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  30. */
  31. /**
  32. * @namespace
  33. */
  34. namespace TQ\Git\StreamWrapper\FileBuffer;
  35. /**
  36. * Encapsulates a file stream buffer to be used in the streamwrapper
  37. *
  38. * @author Stefan Gehrig <gehrigteqneers.de>
  39. * @category TQ
  40. * @package TQ_Git
  41. * @subpackage StreamWrapper
  42. * @copyright Copyright (C) 2011 by TEQneers GmbH & Co. KG
  43. */
  44. class StreamBuffer implements FileBuffer
  45. {
  46. /**
  47. * The file resource
  48. *
  49. * @var resource
  50. */
  51. protected $stream;
  52. /**
  53. * Creates a new file buffer with the given path
  54. *
  55. * @param string $path The path
  56. * @param string $mode The file mode
  57. */
  58. public function __construct($path, $mode = 'r+')
  59. {
  60. $this->stream = @fopen($path, $mode);
  61. if ($this->stream === false) {
  62. throw new StreamException(sprintf('Cannot access "%s" in mode "%s"', $path, $mode));
  63. }
  64. }
  65. /**
  66. * Destructor closes file stream handle
  67. */
  68. public function __destruct()
  69. {
  70. $this->close();
  71. }
  72. /**
  73. * Returns the complete contents of the buffer
  74. *
  75. * @return string
  76. */
  77. public function getBuffer()
  78. {
  79. $currentPos = $this->getPosition();
  80. $this->setPosition(0, SEEK_SET);
  81. $buffer = stream_get_contents($this->stream);
  82. $this->setPosition($currentPos, SEEK_SET);
  83. return $buffer;
  84. }
  85. /**
  86. * Returns true if the pointer is at the end of the buffer
  87. *
  88. * @return boolean
  89. */
  90. public function isEof()
  91. {
  92. return feof($this->stream);
  93. }
  94. /**
  95. * Reads $count bytes from the buffer
  96. *
  97. * @param integer $count The number of bytes to read
  98. * @return string|null
  99. */
  100. public function read($count)
  101. {
  102. return fread($this->stream, $count);
  103. }
  104. /**
  105. * Writes the given date into the buffer at the current pointer position
  106. *
  107. * @param string $data The data to write
  108. * @return integer The number of bytes written
  109. */
  110. public function write($data)
  111. {
  112. return fwrite($this->stream, $data);
  113. }
  114. /**
  115. * Returns the current pointer position
  116. *
  117. * @return integer
  118. */
  119. public function getPosition()
  120. {
  121. return ftell($this->stream);
  122. }
  123. /**
  124. * Sets the pointer position
  125. *
  126. * @param integer $position The position in bytes
  127. * @param integer $whence The reference from where to measure $position (SEEK_SET, SEEK_CUR or SEEK_END)
  128. * @return boolean True if the position could be set
  129. */
  130. public function setPosition($position, $whence)
  131. {
  132. return (fseek($this->stream, $position, $whence) == 0);
  133. }
  134. /**
  135. * Returns the stat information for the buffer
  136. *
  137. * @return array
  138. */
  139. public function getStat()
  140. {
  141. return fstat($this->stream);
  142. }
  143. /**
  144. * Flushes the buffer to the storage media
  145. *
  146. * @return boolean
  147. */
  148. public function flush()
  149. {
  150. return fflush($this->stream);
  151. }
  152. /**
  153. * Closes the buffer
  154. */
  155. public function close()
  156. {
  157. if ($this->stream !== null) {
  158. fclose($this->stream);
  159. $this->stream = null;
  160. }
  161. }
  162. }