1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Output stream subclass for file streams.
22
 *
23
 * @package phing.system.io
24
 */
25
class FileOutputStream extends OutputStream
26
{
27

28
    /**
29
     * @var PhingFile The associated file.
30
     */
31
    protected $file;
32

33
    /**
34
     * Construct a new FileOutputStream.
35
     *
36
     * @param  mixed $file
37
     * @param  boolean $append Whether to append bytes to end of file rather than beginning.
38
     * @throws Exception   - if invalid argument specified.
39
     * @throws IOException - if unable to open file.
40
     */
41 1
    public function __construct($file, $append = false)
42
    {
43 1
        if ($file instanceof PhingFile) {
44 1
            $this->file = $file;
45 1
        } elseif (is_string($file)) {
46 1
            $this->file = new PhingFile($file);
47
        } else {
48 0
            throw new Exception("Invalid argument type for \$file.");
49
        }
50 1
        error_clear_last();
51 1
        if ($append) {
52 1
            $stream = @fopen($this->file->getAbsolutePath(), "ab");
53
        } else {
54 1
            $stream = @fopen($this->file->getAbsolutePath(), "wb");
55
        }
56 1
        if ($stream === false) {
57 1
            $lastError = error_get_last();
58 1
            $errormsg = $lastError['message'];
59 1
            throw new IOException("Unable to open " . $this->file->__toString() . " for writing: " . $errormsg);
60
        }
61 1
        parent::__construct($stream);
62
    }
63

64
    /**
65
     * Returns a string representation of the attached file.
66
     *
67
     * @return string
68
     */
69 0
    public function __toString()
70
    {
71 0
        return $this->file->getPath();
72
    }
73
}

Read our documentation on viewing source code .

Loading