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
 * Base class for core filter readers.
22
 *
23
 * @author  <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
24
 * @see     FilterReader
25
 * @package phing.filters
26
 */
27
class BaseFilterReader extends FilterReader
28
{
29

30
    /**
31
     * Have the parameters passed been interpreted?
32
     */
33
    protected $initialized = false;
34

35
    /**
36
     * The Phing project this filter is part of.
37
     *
38
     * @var Project
39
     */
40
    protected $project = null;
41

42
    /**
43
     * Constructor used by Phing's introspection mechanism.
44
     * The original filter reader is only used for chaining
45
     * purposes, never for filtering purposes (and indeed
46
     * it would be useless for filtering purposes, as it has
47
     * no real data to filter). ChainedReaderHelper uses
48
     * this placeholder instance to create a chain of real filters.
49
     *
50
     * @param Reader $in
51
     */
52 1
    public function __construct($in = null)
53
    {
54 1
        if ($in === null) {
55 1
            $dummy = "";
56 1
            $in = new StringReader($dummy);
57
        }
58 1
        parent::__construct($in);
59
    }
60

61
    /**
62
     * Returns the initialized status.
63
     *
64
     * @return boolean whether or not the filter is initialized
65
     */
66 1
    public function getInitialized()
67
    {
68 1
        return $this->initialized;
69
    }
70

71
    /**
72
     * Sets the initialized status.
73
     *
74
     * @param boolean $initialized Whether or not the filter is initialized.
75
     */
76 1
    public function setInitialized($initialized)
77
    {
78 1
        $this->initialized = (bool) $initialized;
79
    }
80

81
    /**
82
     * Sets the project to work with.
83
     *
84
     * @param object|Project $project The project this filter is part of.
85
     *                        Should not be <code>null</code>.
86
     */
87 1
    public function setProject(Project $project)
88
    {
89
        // type check, error must never occur, bad code of it does
90 1
        $this->project = $project;
91
    }
92

93
    /**
94
     * Returns the project this filter is part of.
95
     *
96
     * @return object The project this filter is part of
97
     */
98 1
    public function getProject()
99
    {
100 1
        return $this->project;
101
    }
102

103
    /**
104
     * Reads characters.
105
     *
106
     * @param int $len Maximum number of characters to read.
107
     *
108
     * @return string Characters read, or -1 if the end of the stream
109
     *                    has been reached
110
     *
111
     * @throws IOException If an I/O error occurs
112
     */
113 1
    public function read($len = null)
114
    {
115 1
        return $this->in->read($len);
116
    }
117

118
    /**
119
     * Reads a line of text ending with '\n' (or until the end of the stream).
120
     * The returned String retains the '\n'.
121
     *
122
     * @return string|null the line read, or <code>null</code> if the end of the
123
     *             stream has already been reached
124
     *
125
     * @throws IOException if the underlying reader throws one during
126
     *                     reading
127
     */
128 1
    public function readLine()
129
    {
130 1
        $line = null;
131

132 1
        while (($ch = $this->in->read(1)) !== -1) {
133 1
            $line .= $ch;
134 1
            if ($ch === "\n") {
135 1
                break;
136
            }
137
        }
138

139 1
        return $line;
140
    }
141

142
    /**
143
     * Returns whether the end of file has been reached with input stream.
144
     *
145
     * @return boolean
146
     */
147 0
    public function eof()
148
    {
149 0
        return $this->in->eof();
150
    }
151

152
    /**
153
     * Convenience method to support logging in filters.
154
     *
155
     * @param string $msg Message to log.
156
     * @param int $level Priority level.
157
     *
158
     * @return void
159
     */
160 1
    public function log($msg, $level = Project::MSG_INFO)
161
    {
162 1
        if ($this->project !== null) {
163 1
            $this->project->log("[filter:" . get_class($this) . "] " . $msg, $level);
164
        }
165
    }
166
}

Read our documentation on viewing source code .

Loading