1 | <?php |
||
2 | |||
3 | /** |
||
4 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
5 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
6 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
7 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
8 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
9 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
10 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
11 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
12 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
13 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
14 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
15 | * |
||
16 | * This software consists of voluntary contributions made by many individuals |
||
17 | * and is licensed under the LGPL. For more information please see |
||
18 | * <http://phing.info>. |
||
19 | */ |
||
20 | |||
21 | namespace Phing\Filter; |
||
22 | |||
23 | use Phing\Io\FilterReader; |
||
24 | use Phing\Io\IOException; |
||
25 | use Phing\Io\Reader; |
||
26 | use Phing\Io\StringReader; |
||
27 | use Phing\Project; |
||
28 | |||
29 | /** |
||
30 | * Base class for core filter readers. |
||
31 | * |
||
32 | * @author <a href="mailto:[email protected]">Yannick Lecaillez</a> |
||
33 | * |
||
34 | * @see FilterReader |
||
35 | */ |
||
36 | class BaseFilterReader extends FilterReader |
||
37 | { |
||
38 | /** |
||
39 | * Have the parameters passed been interpreted? |
||
40 | */ |
||
41 | protected $initialized = false; |
||
42 | |||
43 | /** |
||
44 | * The Phing project this filter is part of. |
||
45 | * |
||
46 | * @var Project |
||
47 | */ |
||
48 | protected $project; |
||
49 | |||
50 | /** |
||
51 | * Constructor used by Phing's introspection mechanism. |
||
52 | * The original filter reader is only used for chaining |
||
53 | * purposes, never for filtering purposes (and indeed |
||
54 | * it would be useless for filtering purposes, as it has |
||
55 | * no real data to filter). ChainedReaderHelper uses |
||
56 | * this placeholder instance to create a chain of real filters. |
||
57 | * |
||
58 | * @param Reader $in |
||
59 | */ |
||
60 | 28 | public function __construct($in = null) |
|
61 | { |
||
62 | 28 | if (null === $in) { |
|
63 | 28 | $dummy = ''; |
|
64 | 28 | $in = new StringReader($dummy); |
|
65 | } |
||
66 | 28 | parent::__construct($in); |
|
67 | 28 | } |
|
68 | |||
69 | /** |
||
70 | * Returns the initialized status. |
||
71 | * |
||
72 | * @return bool whether or not the filter is initialized |
||
73 | */ |
||
74 | 21 | public function getInitialized() |
|
75 | { |
||
76 | 21 | return $this->initialized; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Sets the initialized status. |
||
81 | * |
||
82 | * @param bool $initialized whether or not the filter is initialized |
||
83 | */ |
||
84 | 21 | public function setInitialized($initialized) |
|
85 | { |
||
86 | 21 | $this->initialized = (bool) $initialized; |
|
87 | 21 | } |
|
88 | |||
89 | /** |
||
90 | * Sets the project to work with. |
||
91 | * |
||
92 | * @param Project $project the project this filter is part of |
||
93 | */ |
||
94 | 28 | public function setProject(Project $project) |
|
95 | { |
||
96 | // type check, error must never occur, bad code of it does |
||
97 | 28 | $this->project = $project; |
|
98 | 28 | } |
|
99 | |||
100 | /** |
||
101 | * Returns the project this filter is part of. |
||
102 | * |
||
103 | * @return object The project this filter is part of |
||
104 | */ |
||
105 | 24 | public function getProject() |
|
106 | { |
||
107 | 24 | return $this->project; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Reads characters. |
||
112 | * |
||
113 | * @param int $len maximum number of characters to read |
||
114 | * |
||
115 | * @throws IOException If an I/O error occurs |
||
116 | * |
||
117 | * @return string Characters read, or -1 if the end of the stream |
||
118 | * has been reached |
||
119 | */ |
||
120 | 1 | public function read($len = null) |
|
121 | { |
||
122 | 1 | return $this->in->read($len); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Reads a line of text ending with '\n' (or until the end of the stream). |
||
127 | * The returned String retains the '\n'. |
||
128 | * |
||
129 | * @throws IOException if the underlying reader throws one during |
||
130 | * reading |
||
131 | * |
||
132 | * @return null|string the line read, or <code>null</code> if the end of the |
||
133 | * stream has already been reached |
||
134 | */ |
||
135 | 5 | public function readLine() |
|
136 | { |
||
137 | 5 | $line = null; |
|
138 | |||
139 | 5 | while (($ch = $this->in->read(1)) !== -1) { |
|
140 | 5 | $line .= $ch; |
|
141 | 5 | if ("\n" === $ch) { |
|
142 | 5 | break; |
|
143 | } |
||
144 | } |
||
145 | |||
146 | 5 | return $line; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns whether the end of file has been reached with input stream. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function eof() |
||
155 | { |
||
156 | return $this->in->eof(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Convenience method to support logging in filters. |
||
161 | * |
||
162 | * @param string $msg message to log |
||
163 | * @param int $level priority level |
||
164 | */ |
||
165 | 10 | public function log($msg, $level = Project::MSG_INFO) |
|
166 | { |
||
167 | 10 | if (null !== $this->project) { |
|
168 | 10 | $this->project->log('[filter:' . get_class($this) . '] ' . $msg, $level); |
|
169 | } |
||
170 | 10 | } |
|
171 | } |
||
172 |