Source of file AbstractFilterableInputStream.php
Size: 4,267 Bytes - Last Modified: 2013-06-17T15:32:32+02:00
/home/theseer/Downloads/Symfony/vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 | <?php /* * This file is part of SwiftMailer. * (c) 2004-2009 Chris Corbyn * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /** * Provides the base functionality for an InputStream supporting filters. * * @package Swift * @subpackage ByteStream * @author Chris Corbyn */ abstract class Swift_ByteStream_AbstractFilterableInputStream implements Swift_InputByteStream, Swift_Filterable { /** * Write sequence. */ protected $_sequence = 0; /** * StreamFilters. */ private $_filters = array(); /** * A buffer for writing. */ private $_writeBuffer = ''; /** * Bound streams. * * @var Swift_InputByteStream[] */ private $_mirrors = array(); /** * Commit the given bytes to the storage medium immediately. * * @param string $bytes */ abstract protected function _commit($bytes); /** * Flush any buffers/content with immediate effect. */ abstract protected function _flush(); /** * Add a StreamFilter to this InputByteStream. * * @param Swift_StreamFilter $filter * @param string $key */ public function addFilter(Swift_StreamFilter $filter, $key) { $this->_filters[$key] = $filter; } /** * Remove an already present StreamFilter based on its $key. * * @param string $key */ public function removeFilter($key) { unset($this->_filters[$key]); } /** * Writes $bytes to the end of the stream. * * @param string $bytes * * @return integer * * @throws Swift_IoException */ public function write($bytes) { $this->_writeBuffer .= $bytes; foreach ($this->_filters as $filter) { if ($filter->shouldBuffer($this->_writeBuffer)) { return; } } $this->_doWrite($this->_writeBuffer); return ++$this->_sequence; } /** * For any bytes that are currently buffered inside the stream, force them * off the buffer. * * @throws Swift_IoException */ public function commit() { $this->_doWrite($this->_writeBuffer); } /** * Attach $is to this stream. * * The stream acts as an observer, receiving all data that is written. * All {@link write()} and {@link flushBuffers()} operations will be mirrored. * * @param Swift_InputByteStream $is */ public function bind(Swift_InputByteStream $is) { $this->_mirrors[] = $is; } /** * Remove an already bound stream. * * If $is is not bound, no errors will be raised. * If the stream currently has any buffered data it will be written to $is * before unbinding occurs. * * @param Swift_InputByteStream $is */ public function unbind(Swift_InputByteStream $is) { foreach ($this->_mirrors as $k => $stream) { if ($is === $stream) { if ($this->_writeBuffer !== '') { $stream->write($this->_filter($this->_writeBuffer)); } unset($this->_mirrors[$k]); } } } /** * Flush the contents of the stream (empty it) and set the internal pointer * to the beginning. * * @throws Swift_IoException */ public function flushBuffers() { if ($this->_writeBuffer !== '') { $this->_doWrite($this->_writeBuffer); } $this->_flush(); foreach ($this->_mirrors as $stream) { $stream->flushBuffers(); } } // -- Private methods /** Run $bytes through all filters */ private function _filter($bytes) { foreach ($this->_filters as $filter) { $bytes = $filter->filter($bytes); } return $bytes; } /** Just write the bytes to the stream */ private function _doWrite($bytes) { $this->_commit($this->_filter($bytes)); foreach ($this->_mirrors as $stream) { $stream->write($bytes); } $this->_writeBuffer = ''; } } |