123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- /**
- * Hoa
- *
- *
- * @license
- *
- * New BSD License
- *
- * Copyright © 2007-2017, Hoa community. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the Hoa nor the names of its contributors may be
- * used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- namespace Hoa\Stream;
- /**
- * Class \Hoa\Stream\Bucket.
- *
- * Manipulate stream buckets through brigades.
- *
- * @copyright Copyright © 2007-2017 Hoa community
- * @license New BSD License
- */
- class Bucket
- {
- /**
- * Whether the stream is already a brigade.
- *
- * @const bool
- */
- const IS_A_BRIGADE = true;
- /**
- * Whether the stream is not a brigade.
- *
- * @const bool
- */
- const IS_A_STREAM = false;
- /**
- * Type of the bucket.
- *
- * @var bool
- */
- protected $_type = null;
- /**
- * Brigade.
- *
- * @var resource
- */
- protected $_brigade = null;
- /**
- * Bucket.
- *
- * @var resource
- */
- protected $_bucket = null;
- /**
- * Set a brigade.
- * If a stream is given (with the constant `self::IS_A_STREAM`), it will
- * create a brigade automatically.
- *
- * @param resource &$brigade A stream or a brigade.
- * @param bool $is Specify if `$brigade` is a stream or a
- * brigade, given by `self::IS_A_*` constant.
- * @param string $buffer Stream buffer.
- */
- public function __construct(&$brigade, $is = self::IS_A_BRIGADE, $buffer = '')
- {
- $this->setType($is);
- if (self::IS_A_BRIGADE === $this->getType()) {
- $this->setBrigade($brigade);
- } else {
- $this->setBucket(stream_bucket_new($brigade, $buffer));
- $bucket = $this->getBucket();
- $this->setBrigade($bucket);
- }
- return;
- }
- /**
- * Test the end-of-bucket.
- * When testing, set the new bucket object.
- *
- * @return bool
- */
- public function eob()
- {
- $this->_bucket = null;
- return false == $this->getBucket();
- }
- /**
- * Append bucket to the brigade.
- *
- * @param \Hoa\Stream\Bucket $bucket Bucket to add.
- * @return void
- */
- public function append(Bucket $bucket)
- {
- stream_bucket_append($this->getBrigade(), $bucket->getBucket());
- return;
- }
- /**
- * Prepend bucket to the brigade.
- *
- * @param \Hoa\Stream\Bucket $bucket Bucket to add.
- * @return void
- */
- public function prepend(Bucket $bucket)
- {
- stream_bucket_prepend($this->getBrigade(), $bucket->getBucket());
- return;
- }
- /**
- * Set type.
- *
- * @param bool $type Type. Please, see self::IS_A_* constants.
- * @return bool
- */
- protected function setType($type)
- {
- $old = $this->_type;
- $this->_type = $type;
- return $old;
- }
- /**
- * Get type.
- *
- * @return bool
- */
- public function getType()
- {
- return $this->_type;
- }
- /**
- * Set bucket data.
- *
- * @param string $data Data to set.
- * @return string
- */
- public function setData($data)
- {
- $old = $this->getBucket()->data;
- $this->getBucket()->data = $data;
- $this->getBucket()->datalen = strlen($this->getBucket()->data);
- return $old;
- }
- /**
- * Get bucket data.
- *
- * @return string
- */
- public function getData()
- {
- if (null === $this->getBucket()) {
- return null;
- }
- return $this->getBucket()->data;
- }
- /**
- * Get bucket length.
- *
- * @return int
- */
- public function getLength()
- {
- if (null === $this->getBucket()) {
- return 0;
- }
- return $this->getBucket()->datalen;
- }
- /**
- * Set the brigade.
- *
- * @param resource &$brigade Brigade to add.
- * @return resource
- */
- protected function setBrigade(&$brigade)
- {
- $old = $this->_brigade;
- $this->_brigade = $brigade;
- return $old;
- }
- /**
- * Get the brigade.
- *
- * @return resource
- */
- public function getBrigade()
- {
- return $this->_brigade;
- }
- /**
- * Set bucket.
- *
- * @param resource $bucket Bucket.
- * @return resource
- */
- protected function setBucket($bucket)
- {
- $old = $this->_bucket;
- $this->_bucket = $bucket;
- return $old;
- }
- /**
- * Get the current bucket.
- *
- * @return mixed
- */
- protected function getBucket()
- {
- if (null === $this->_bucket && self::IS_A_BRIGADE === $this->getType()) {
- $this->_bucket = stream_bucket_make_writeable($this->getBrigade());
- }
- return $this->_bucket;
- }
- }
|