TYPO3  7.6
AbstractFilterableInputStream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
21  protected $_sequence = 0;
22 
26  private $_filters = array();
27 
31  private $_writeBuffer = '';
32 
38  private $_mirrors = array();
39 
45  abstract protected function _commit($bytes);
46 
50  abstract protected function _flush();
51 
58  public function addFilter(Swift_StreamFilter $filter, $key)
59  {
60  $this->_filters[$key] = $filter;
61  }
62 
68  public function removeFilter($key)
69  {
70  unset($this->_filters[$key]);
71  }
72 
82  public function write($bytes)
83  {
84  $this->_writeBuffer .= $bytes;
85  foreach ($this->_filters as $filter) {
86  if ($filter->shouldBuffer($this->_writeBuffer)) {
87  return;
88  }
89  }
90  $this->_doWrite($this->_writeBuffer);
91 
92  return ++$this->_sequence;
93  }
94 
101  public function commit()
102  {
103  $this->_doWrite($this->_writeBuffer);
104  }
105 
114  public function bind(Swift_InputByteStream $is)
115  {
116  $this->_mirrors[] = $is;
117  }
118 
128  public function unbind(Swift_InputByteStream $is)
129  {
130  foreach ($this->_mirrors as $k => $stream) {
131  if ($is === $stream) {
132  if ($this->_writeBuffer !== '') {
133  $stream->write($this->_writeBuffer);
134  }
135  unset($this->_mirrors[$k]);
136  }
137  }
138  }
139 
146  public function flushBuffers()
147  {
148  if ($this->_writeBuffer !== '') {
149  $this->_doWrite($this->_writeBuffer);
150  }
151  $this->_flush();
152 
153  foreach ($this->_mirrors as $stream) {
154  $stream->flushBuffers();
155  }
156  }
157 
159  private function _filter($bytes)
160  {
161  foreach ($this->_filters as $filter) {
162  $bytes = $filter->filter($bytes);
163  }
164 
165  return $bytes;
166  }
167 
169  private function _doWrite($bytes)
170  {
171  $this->_commit($this->_filter($bytes));
172 
173  foreach ($this->_mirrors as $stream) {
174  $stream->write($bytes);
175  }
176 
177  $this->_writeBuffer = '';
178  }
179 }