1<?php
2
3/**
4 * Swift Mailer Joint Output stream to chain multiple output streams together
5 * Please read the LICENSE file
6 * @author Chris Corbyn <chris@w3style.co.uk>
7 * @package Swift_Cache
8 * @license GNU Lesser General Public License
9 */
10
11require_once dirname(__FILE__) . "/../ClassLoader.php";
12Swift_ClassLoader::load("Swift_Cache_OutputStream");
13
14/**
15 * Makes multiple output streams act as one super sream
16 * @package Swift_Cache
17 * @author Chris Corbyn <chris@w3style.co.uk>
18 */
19class Swift_Cache_JointOutputStream extends Swift_Cache_OutputStream
20{
21  /**
22   * The streams to join
23   * @var array
24   */
25  protected $streams = array();
26  /**
27   * The current stream in use
28   * @var int
29   */
30  protected $pointer = 0;
31
32  /**
33   * Ctor
34   * @param array An array of Swift_Cache_OutputStream instances
35   */
36  public function __construct($streams=array())
37  {
38    $this->streams = $streams;
39  }
40  /**
41   * Add a new output stream
42   * @param Swift_Cache_OutputStream
43   */
44  public function addStream(Swift_Cache_OutputStream $stream)
45  {
46    $this->streams[] = $stream;
47  }
48  /**
49   * Read data from all streams as if they are one stream
50   * @param int The number of bytes to read from each stream
51   * @return string
52   */
53  public function read($size=null)
54  {
55    $ret = $this->streams[$this->pointer]->read($size);
56    if ($ret !== false)
57    {
58      return $ret;
59    }
60    else
61    {
62      if (isset($this->streams[($this->pointer+1)]))
63      {
64        $this->pointer++;
65        return $this->read($size);
66      }
67      else
68      {
69        $this->pointer = 0;
70        return false;
71      }
72    }
73  }
74}