SimpleKeyCacheInputStream.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  10. * Writes data to a KeyCache using a stream.
  11. * @package Swift
  12. * @subpackage KeyCache
  13. * @author Chris Corbyn
  14. */
  15. class Swift_KeyCache_SimpleKeyCacheInputStream implements Swift_KeyCache_KeyCacheInputStream
  16. {
  17. /** The KeyCache being written to */
  18. private $_keyCache;
  19. /** The nsKey of the KeyCache being written to */
  20. private $_nsKey;
  21. /** The itemKey of the KeyCache being written to */
  22. private $_itemKey;
  23. /** A stream to write through on each write() */
  24. private $_writeThrough = null;
  25. /**
  26. * Set the KeyCache to wrap.
  27. * @param Swift_KeyCache $keyCache
  28. */
  29. public function setKeyCache(Swift_KeyCache $keyCache)
  30. {
  31. $this->_keyCache = $keyCache;
  32. }
  33. /**
  34. * Specify a stream to write through for each write().
  35. * @param Swift_InputByteStream $is
  36. */
  37. public function setWriteThroughStream(Swift_InputByteStream $is)
  38. {
  39. $this->_writeThrough = $is;
  40. }
  41. /**
  42. * Writes $bytes to the end of the stream.
  43. * @param string $bytes
  44. * @param Swift_InputByteStream $is, optional
  45. */
  46. public function write($bytes, Swift_InputByteStream $is = null)
  47. {
  48. $this->_keyCache->setString(
  49. $this->_nsKey, $this->_itemKey, $bytes, Swift_KeyCache::MODE_APPEND
  50. );
  51. if (isset($is)) {
  52. $is->write($bytes);
  53. }
  54. if (isset($this->_writeThrough)) {
  55. $this->_writeThrough->write($bytes);
  56. }
  57. }
  58. /**
  59. * Not used.
  60. */
  61. public function commit()
  62. {
  63. }
  64. /**
  65. * Not used.
  66. */
  67. public function bind(Swift_InputByteStream $is)
  68. {
  69. }
  70. /**
  71. * Not used.
  72. */
  73. public function unbind(Swift_InputByteStream $is)
  74. {
  75. }
  76. /**
  77. * Flush the contents of the stream (empty it) and set the internal pointer
  78. * to the beginning.
  79. */
  80. public function flushBuffers()
  81. {
  82. $this->_keyCache->clearKey($this->_nsKey, $this->_itemKey);
  83. }
  84. /**
  85. * Set the nsKey which will be written to.
  86. * @param string $nsKey
  87. */
  88. public function setNsKey($nsKey)
  89. {
  90. $this->_nsKey = $nsKey;
  91. }
  92. /**
  93. * Set the itemKey which will be written to.
  94. * @param string $itemKey
  95. */
  96. public function setItemKey($itemKey)
  97. {
  98. $this->_itemKey = $itemKey;
  99. }
  100. /**
  101. * Any implementation should be cloneable, allowing the clone to access a
  102. * separate $nsKey and $itemKey.
  103. */
  104. public function __clone()
  105. {
  106. $this->_writeThrough = null;
  107. }
  108. }