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