XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
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 
11 //@require 'Swift/InputByteStream.php';
12 //@require 'Swift/Filterable.php';
13 //@require 'Swift/StreamFilter.php';
14 
23 {
24 
26  private $_sequence = 0;
27 
29  private $_filters = array();
30 
32  private $_writeBuffer = '';
33 
35  private $_mirrors = array();
36 
42  abstract protected function _commit($bytes);
43 
48  abstract protected function _flush();
49 
55  public function addFilter(Swift_StreamFilter $filter, $key)
56  {
57  $this->_filters[$key] = $filter;
58  }
59 
64  public function removeFilter($key)
65  {
66  unset($this->_filters[$key]);
67  }
68 
74  public function write($bytes)
75  {
76  $this->_writeBuffer .= $bytes;
77  foreach ($this->_filters as $filter)
78  {
79  if ($filter->shouldBuffer($this->_writeBuffer))
80  {
81  return;
82  }
83  }
84  $this->_doWrite($this->_writeBuffer);
85  return ++$this->_sequence;
86  }
87 
94  public function commit()
95  {
96  $this->_doWrite($this->_writeBuffer);
97  }
98 
106  public function bind(Swift_InputByteStream $is)
107  {
108  $this->_mirrors[] = $is;
109  }
110 
119  public function unbind(Swift_InputByteStream $is)
120  {
121  foreach ($this->_mirrors as $k => $stream)
122  {
123  if ($is === $stream)
124  {
125  if ($this->_writeBuffer !== '')
126  {
127  $stream->write($this->_filter($this->_writeBuffer));
128  }
129  unset($this->_mirrors[$k]);
130  }
131  }
132  }
133 
139  public function flushBuffers()
140  {
141  if ($this->_writeBuffer !== '')
142  {
143  $this->_doWrite($this->_writeBuffer);
144  }
145  $this->_flush();
146 
147  foreach ($this->_mirrors as $stream)
148  {
149  $stream->flushBuffers();
150  }
151  }
152 
153  // -- Private methods
154 
156  private function _filter($bytes)
157  {
158  foreach ($this->_filters as $filter)
159  {
160  $bytes = $filter->filter($bytes);
161  }
162  return $bytes;
163  }
164 
166  private function _doWrite($bytes)
167  {
168  $this->_commit($this->_filter($bytes));
169 
170  foreach ($this->_mirrors as $stream)
171  {
172  $stream->write($bytes);
173  }
174 
175  $this->_writeBuffer = '';
176  }
177 
178 }