/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. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/InputByteStream.php';
  10. //@require 'Swift/Filterable.php';
  11. //@require 'Swift/StreamFilter.php';
  12. /**
  13. * Provides the base functionality for an InputStream supporting filters.
  14. * @package Swift
  15. * @subpackage ByteStream
  16. * @author Chris Corbyn
  17. */
  18. abstract class Swift_ByteStream_AbstractFilterableInputStream
  19. implements Swift_InputByteStream, Swift_Filterable
  20. {
  21. /** Write sequence */
  22. private $_sequence = 0;
  23. /** StreamFilters */
  24. private $_filters = array();
  25. /** A buffer for writing */
  26. private $_writeBuffer = '';
  27. /** Bound streams */
  28. private $_mirrors = array();
  29. /**
  30. * Commit the given bytes to the storage medium immediately.
  31. * @param string $bytes
  32. * @access protected
  33. */
  34. abstract protected function _commit($bytes);
  35. /**
  36. * Flush any buffers/content with immediate effect.
  37. * @access protected
  38. */
  39. abstract protected function _flush();
  40. /**
  41. * Add a StreamFilter to this InputByteStream.
  42. * @param Swift_StreamFilter $filter
  43. * @param string $key
  44. */
  45. public function addFilter(Swift_StreamFilter $filter, $key)
  46. {
  47. $this->_filters[$key] = $filter;
  48. }
  49. /**
  50. * Remove an already present StreamFilter based on its $key.
  51. * @param string $key
  52. */
  53. public function removeFilter($key)
  54. {
  55. unset($this->_filters[$key]);
  56. }
  57. /**
  58. * Writes $bytes to the end of the stream.
  59. * @param string $bytes
  60. * @throws Swift_IoException
  61. */
  62. public function write($bytes)
  63. {
  64. $this->_writeBuffer .= $bytes;
  65. foreach ($this->_filters as $filter)
  66. {
  67. if ($filter->shouldBuffer($this->_writeBuffer))
  68. {
  69. return;
  70. }
  71. }
  72. $this->_doWrite($this->_writeBuffer);
  73. return ++$this->_sequence;
  74. }
  75. /**
  76. * For any bytes that are currently buffered inside the stream, force them
  77. * off the buffer.
  78. *
  79. * @throws Swift_IoException
  80. */
  81. public function commit()
  82. {
  83. $this->_doWrite($this->_writeBuffer);
  84. }
  85. /**
  86. * Attach $is to this stream.
  87. * The stream acts as an observer, receiving all data that is written.
  88. * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
  89. *
  90. * @param Swift_InputByteStream $is
  91. */
  92. public function bind(Swift_InputByteStream $is)
  93. {
  94. $this->_mirrors[] = $is;
  95. }
  96. /**
  97. * Remove an already bound stream.
  98. * If $is is not bound, no errors will be raised.
  99. * If the stream currently has any buffered data it will be written to $is
  100. * before unbinding occurs.
  101. *
  102. * @param Swift_InputByteStream $is
  103. */
  104. public function unbind(Swift_InputByteStream $is)
  105. {
  106. foreach ($this->_mirrors as $k => $stream)
  107. {
  108. if ($is === $stream)
  109. {
  110. if ($this->_writeBuffer !== '')
  111. {
  112. $stream->write($this->_filter($this->_writeBuffer));
  113. }
  114. unset($this->_mirrors[$k]);
  115. }
  116. }
  117. }
  118. /**
  119. * Flush the contents of the stream (empty it) and set the internal pointer
  120. * to the beginning.
  121. * @throws Swift_IoException
  122. */
  123. public function flushBuffers()
  124. {
  125. if ($this->_writeBuffer !== '')
  126. {
  127. $this->_doWrite($this->_writeBuffer);
  128. }
  129. $this->_flush();
  130. foreach ($this->_mirrors as $stream)
  131. {
  132. $stream->flushBuffers();
  133. }
  134. }
  135. // -- Private methods
  136. /** Run $bytes through all filters */
  137. private function _filter($bytes)
  138. {
  139. foreach ($this->_filters as $filter)
  140. {
  141. $bytes = $filter->filter($bytes);
  142. }
  143. return $bytes;
  144. }
  145. /** Just write the bytes to the stream */
  146. private function _doWrite($bytes)
  147. {
  148. $this->_commit($this->_filter($bytes));
  149. foreach ($this->_mirrors as $stream)
  150. {
  151. $stream->write($bytes);
  152. }
  153. $this->_writeBuffer = '';
  154. }
  155. }