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