Bucket.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Stream;
  36. /**
  37. * Class \Hoa\Stream\Bucket.
  38. *
  39. * Manipulate stream buckets through brigades.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Bucket
  45. {
  46. /**
  47. * Whether the stream is already a brigade.
  48. *
  49. * @const bool
  50. */
  51. const IS_A_BRIGADE = true;
  52. /**
  53. * Whether the stream is not a brigade.
  54. *
  55. * @const bool
  56. */
  57. const IS_A_STREAM = false;
  58. /**
  59. * Type of the bucket.
  60. *
  61. * @var bool
  62. */
  63. protected $_type = null;
  64. /**
  65. * Brigade.
  66. *
  67. * @var resource
  68. */
  69. protected $_brigade = null;
  70. /**
  71. * Bucket.
  72. *
  73. * @var resource
  74. */
  75. protected $_bucket = null;
  76. /**
  77. * Set a brigade.
  78. * If a stream is given (with the constant `self::IS_A_STREAM`), it will
  79. * create a brigade automatically.
  80. *
  81. * @param resource &$brigade A stream or a brigade.
  82. * @param bool $is Specify if `$brigade` is a stream or a
  83. * brigade, given by `self::IS_A_*` constant.
  84. * @param string $buffer Stream buffer.
  85. */
  86. public function __construct(&$brigade, $is = self::IS_A_BRIGADE, $buffer = '')
  87. {
  88. $this->setType($is);
  89. if (self::IS_A_BRIGADE === $this->getType()) {
  90. $this->setBrigade($brigade);
  91. } else {
  92. $this->setBucket(stream_bucket_new($brigade, $buffer));
  93. $bucket = $this->getBucket();
  94. $this->setBrigade($bucket);
  95. }
  96. return;
  97. }
  98. /**
  99. * Test the end-of-bucket.
  100. * When testing, set the new bucket object.
  101. *
  102. * @return bool
  103. */
  104. public function eob()
  105. {
  106. $this->_bucket = null;
  107. return false == $this->getBucket();
  108. }
  109. /**
  110. * Append bucket to the brigade.
  111. *
  112. * @param \Hoa\Stream\Bucket $bucket Bucket to add.
  113. * @return void
  114. */
  115. public function append(Bucket $bucket)
  116. {
  117. stream_bucket_append($this->getBrigade(), $bucket->getBucket());
  118. return;
  119. }
  120. /**
  121. * Prepend bucket to the brigade.
  122. *
  123. * @param \Hoa\Stream\Bucket $bucket Bucket to add.
  124. * @return void
  125. */
  126. public function prepend(Bucket $bucket)
  127. {
  128. stream_bucket_prepend($this->getBrigade(), $bucket->getBucket());
  129. return;
  130. }
  131. /**
  132. * Set type.
  133. *
  134. * @param bool $type Type. Please, see self::IS_A_* constants.
  135. * @return bool
  136. */
  137. protected function setType($type)
  138. {
  139. $old = $this->_type;
  140. $this->_type = $type;
  141. return $old;
  142. }
  143. /**
  144. * Get type.
  145. *
  146. * @return bool
  147. */
  148. public function getType()
  149. {
  150. return $this->_type;
  151. }
  152. /**
  153. * Set bucket data.
  154. *
  155. * @param string $data Data to set.
  156. * @return string
  157. */
  158. public function setData($data)
  159. {
  160. $old = $this->getBucket()->data;
  161. $this->getBucket()->data = $data;
  162. $this->getBucket()->datalen = strlen($this->getBucket()->data);
  163. return $old;
  164. }
  165. /**
  166. * Get bucket data.
  167. *
  168. * @return string
  169. */
  170. public function getData()
  171. {
  172. if (null === $this->getBucket()) {
  173. return null;
  174. }
  175. return $this->getBucket()->data;
  176. }
  177. /**
  178. * Get bucket length.
  179. *
  180. * @return int
  181. */
  182. public function getLength()
  183. {
  184. if (null === $this->getBucket()) {
  185. return 0;
  186. }
  187. return $this->getBucket()->datalen;
  188. }
  189. /**
  190. * Set the brigade.
  191. *
  192. * @param resource &$brigade Brigade to add.
  193. * @return resource
  194. */
  195. protected function setBrigade(&$brigade)
  196. {
  197. $old = $this->_brigade;
  198. $this->_brigade = $brigade;
  199. return $old;
  200. }
  201. /**
  202. * Get the brigade.
  203. *
  204. * @return resource
  205. */
  206. public function getBrigade()
  207. {
  208. return $this->_brigade;
  209. }
  210. /**
  211. * Set bucket.
  212. *
  213. * @param resource $bucket Bucket.
  214. * @return resource
  215. */
  216. protected function setBucket($bucket)
  217. {
  218. $old = $this->_bucket;
  219. $this->_bucket = $bucket;
  220. return $old;
  221. }
  222. /**
  223. * Get the current bucket.
  224. *
  225. * @return mixed
  226. */
  227. protected function getBucket()
  228. {
  229. if (null === $this->_bucket && self::IS_A_BRIGADE === $this->getType()) {
  230. $this->_bucket = stream_bucket_make_writeable($this->getBrigade());
  231. }
  232. return $this->_bucket;
  233. }
  234. }