|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* 00004 * This file is part of SwiftMailer. 00005 * (c) 2004-2009 Chris Corbyn 00006 * 00007 * For the full copyright and license information, please view the LICENSE 00008 * file that was distributed with this source code. 00009 */ 00010 00011 //@require 'Swift/InputByteStream.php'; 00012 //@require 'Swift/Filterable.php'; 00013 //@require 'Swift/StreamFilter.php'; 00014 00015 /** 00016 * Provides the base functionality for an InputStream supporting filters. 00017 * @package Swift 00018 * @subpackage ByteStream 00019 * @author Chris Corbyn 00020 */ 00021 abstract class Swift_ByteStream_AbstractFilterableInputStream 00022 implements Swift_InputByteStream, Swift_Filterable 00023 { 00024 00025 /** Write sequence */ 00026 private $_sequence = 0; 00027 00028 /** StreamFilters */ 00029 private $_filters = array(); 00030 00031 /** A buffer for writing */ 00032 private $_writeBuffer = ''; 00033 00034 /** Bound streams */ 00035 private $_mirrors = array(); 00036 00037 /** 00038 * Commit the given bytes to the storage medium immediately. 00039 * @param string $bytes 00040 * @access protected 00041 */ 00042 abstract protected function _commit($bytes); 00043 00044 /** 00045 * Flush any buffers/content with immediate effect. 00046 * @access protected 00047 */ 00048 abstract protected function _flush(); 00049 00050 /** 00051 * Add a StreamFilter to this InputByteStream. 00052 * @param Swift_StreamFilter $filter 00053 * @param string $key 00054 */ 00055 public function addFilter(Swift_StreamFilter $filter, $key) 00056 { 00057 $this->_filters[$key] = $filter; 00058 } 00059 00060 /** 00061 * Remove an already present StreamFilter based on its $key. 00062 * @param string $key 00063 */ 00064 public function removeFilter($key) 00065 { 00066 unset($this->_filters[$key]); 00067 } 00068 00069 /** 00070 * Writes $bytes to the end of the stream. 00071 * @param string $bytes 00072 * @throws Swift_IoException 00073 */ 00074 public function write($bytes) 00075 { 00076 $this->_writeBuffer .= $bytes; 00077 foreach ($this->_filters as $filter) 00078 { 00079 if ($filter->shouldBuffer($this->_writeBuffer)) 00080 { 00081 return; 00082 } 00083 } 00084 $this->_doWrite($this->_writeBuffer); 00085 return ++$this->_sequence; 00086 } 00087 00088 /** 00089 * For any bytes that are currently buffered inside the stream, force them 00090 * off the buffer. 00091 * 00092 * @throws Swift_IoException 00093 */ 00094 public function commit() 00095 { 00096 $this->_doWrite($this->_writeBuffer); 00097 } 00098 00099 /** 00100 * Attach $is to this stream. 00101 * The stream acts as an observer, receiving all data that is written. 00102 * All {@link write()} and {@link flushBuffers()} operations will be mirrored. 00103 * 00104 * @param Swift_InputByteStream $is 00105 */ 00106 public function bind(Swift_InputByteStream $is) 00107 { 00108 $this->_mirrors[] = $is; 00109 } 00110 00111 /** 00112 * Remove an already bound stream. 00113 * If $is is not bound, no errors will be raised. 00114 * If the stream currently has any buffered data it will be written to $is 00115 * before unbinding occurs. 00116 * 00117 * @param Swift_InputByteStream $is 00118 */ 00119 public function unbind(Swift_InputByteStream $is) 00120 { 00121 foreach ($this->_mirrors as $k => $stream) 00122 { 00123 if ($is === $stream) 00124 { 00125 if ($this->_writeBuffer !== '') 00126 { 00127 $stream->write($this->_filter($this->_writeBuffer)); 00128 } 00129 unset($this->_mirrors[$k]); 00130 } 00131 } 00132 } 00133 00134 /** 00135 * Flush the contents of the stream (empty it) and set the internal pointer 00136 * to the beginning. 00137 * @throws Swift_IoException 00138 */ 00139 public function flushBuffers() 00140 { 00141 if ($this->_writeBuffer !== '') 00142 { 00143 $this->_doWrite($this->_writeBuffer); 00144 } 00145 $this->_flush(); 00146 00147 foreach ($this->_mirrors as $stream) 00148 { 00149 $stream->flushBuffers(); 00150 } 00151 } 00152 00153 // -- Private methods 00154 00155 /** Run $bytes through all filters */ 00156 private function _filter($bytes) 00157 { 00158 foreach ($this->_filters as $filter) 00159 { 00160 $bytes = $filter->filter($bytes); 00161 } 00162 return $bytes; 00163 } 00164 00165 /** Just write the bytes to the stream */ 00166 private function _doWrite($bytes) 00167 { 00168 $this->_commit($this->_filter($bytes)); 00169 00170 foreach ($this->_mirrors as $stream) 00171 { 00172 $stream->write($bytes); 00173 } 00174 00175 $this->_writeBuffer = ''; 00176 } 00177 00178 }
1.8.0