PageRenderTime 57ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/vendor/swift/lib/classes/Swift/ByteStream/AbstractFilterableInputStream.php

http://zoop.googlecode.com/
PHP | 178 lines | 83 code | 23 blank | 72 comment | 4 complexity | 0328438801b419673a095cccd2b2ecd9 MD5 | raw 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
 15/**
 16 * Provides the base functionality for an InputStream supporting filters.
 17 * @package Swift
 18 * @subpackage ByteStream
 19 * @author Chris Corbyn
 20 */
 21abstract class Swift_ByteStream_AbstractFilterableInputStream
 22  implements Swift_InputByteStream, Swift_Filterable
 23{
 24  
 25  /** Write sequence */
 26  private $_sequence = 0;
 27  
 28  /** StreamFilters */
 29  private $_filters = array();
 30  
 31  /** A buffer for writing */
 32  private $_writeBuffer = '';
 33  
 34  /** Bound streams */
 35  private $_mirrors = array();
 36  
 37  /**
 38   * Commit the given bytes to the storage medium immediately.
 39   * @param string $bytes
 40   * @access protected
 41   */
 42  abstract protected function _commit($bytes);
 43  
 44  /**
 45   * Flush any buffers/content with immediate effect.
 46   * @access protected
 47   */
 48  abstract protected function _flush();
 49  
 50  /**
 51   * Add a StreamFilter to this InputByteStream.
 52   * @param Swift_StreamFilter $filter
 53   * @param string $key
 54   */
 55  public function addFilter(Swift_StreamFilter $filter, $key)
 56  {
 57    $this->_filters[$key] = $filter;
 58  }
 59  
 60  /**
 61   * Remove an already present StreamFilter based on its $key.
 62   * @param string $key
 63   */
 64  public function removeFilter($key)
 65  {
 66    unset($this->_filters[$key]);
 67  }
 68  
 69  /**
 70   * Writes $bytes to the end of the stream.
 71   * @param string $bytes
 72   * @throws Swift_IoException
 73   */
 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  
 88  /**
 89   * For any bytes that are currently buffered inside the stream, force them
 90   * off the buffer.
 91   * 
 92   * @throws Swift_IoException
 93   */
 94  public function commit()
 95  {
 96    $this->_doWrite($this->_writeBuffer);
 97  }
 98  
 99  /**
100   * Attach $is to this stream.
101   * The stream acts as an observer, receiving all data that is written.
102   * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
103   * 
104   * @param Swift_InputByteStream $is
105   */
106  public function bind(Swift_InputByteStream $is)
107  {
108    $this->_mirrors[] = $is;
109  }
110  
111  /**
112   * Remove an already bound stream.
113   * If $is is not bound, no errors will be raised.
114   * If the stream currently has any buffered data it will be written to $is
115   * before unbinding occurs.
116   * 
117   * @param Swift_InputByteStream $is
118   */
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  
134  /**
135   * Flush the contents of the stream (empty it) and set the internal pointer
136   * to the beginning.
137   * @throws Swift_IoException
138   */
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  
155  /** Run $bytes through all filters */
156  private function _filter($bytes)
157  {
158    foreach ($this->_filters as $filter)
159    {
160      $bytes = $filter->filter($bytes);
161    }
162    return $bytes;
163  }
164  
165  /** Just write the bytes to the stream */
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}