SimpleKeyCacheInputStream.php 2.6 KB

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