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 | |||
27 | /** |
||
28 | * Attaches a prefix to every line. |
||
29 | * |
||
30 | * Example: |
||
31 | * <pre><prefixlines prefix="Foo"/></pre> |
||
32 | * |
||
33 | * Or: |
||
34 | * |
||
35 | * <pre><filterreader classname="phing.filters.PrefixLines"> |
||
36 | * <param name="prefix" value="Foo"/> |
||
37 | * </filterreader></pre> |
||
38 | * |
||
39 | * @author <a href="mailto:[email protected]">Yannick Lecaillez</a> |
||
40 | * @author hans lellelid, [email protected] |
||
41 | * |
||
42 | * @see FilterReader |
||
43 | */ |
||
44 | class PrefixLines extends BaseParamFilterReader implements ChainableReader |
||
45 | { |
||
46 | /** |
||
47 | * Parameter name for the prefix. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | public const PREFIX_KEY = 'lines'; |
||
52 | |||
53 | /** |
||
54 | * The prefix to be used. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $prefix; |
||
59 | |||
60 | /** @var null|string */ |
||
61 | private $queuedData; |
||
62 | |||
63 | /** |
||
64 | * Adds a prefix to each line of input stream and returns resulting stream. |
||
65 | * |
||
66 | * @param int $len |
||
67 | * |
||
68 | * @throws IOException |
||
69 | * |
||
70 | * @return mixed buffer, -1 on EOF |
||
71 | */ |
||
72 | 1 | public function read($len = null) |
|
73 | { |
||
74 | 1 | if (!$this->getInitialized()) { |
|
75 | 1 | $this->initialize(); |
|
76 | 1 | $this->setInitialized(true); |
|
77 | } |
||
78 | |||
79 | 1 | $ch = -1; |
|
80 | |||
81 | 1 | if (null !== $this->queuedData && '' === $this->queuedData) { |
|
82 | $this->queuedData = null; |
||
83 | } |
||
84 | |||
85 | 1 | if (null !== $this->queuedData) { |
|
86 | 1 | $ch = $this->queuedData[0]; |
|
87 | 1 | $this->queuedData = (string) substr($this->queuedData, 1); |
|
88 | 1 | if ('' === $this->queuedData) { |
|
89 | 1 | $this->queuedData = null; |
|
90 | } |
||
91 | } else { |
||
92 | 1 | $this->queuedData = $this->readLine(); |
|
93 | 1 | if (null === $this->queuedData) { |
|
94 | 1 | $ch = -1; |
|
95 | } else { |
||
96 | 1 | if (null !== $this->prefix) { |
|
97 | 1 | $this->queuedData = $this->prefix . $this->queuedData; |
|
98 | } |
||
99 | |||
100 | 1 | return $this->read(); |
|
101 | } |
||
102 | } |
||
103 | |||
104 | 1 | return $ch; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Sets the prefix to add at the start of each input line. |
||
109 | * |
||
110 | * @param string $prefix The prefix to add at the start of each input line. |
||
111 | * May be <code>null</code>, in which case no prefix |
||
112 | * is added. |
||
113 | */ |
||
114 | public function setPrefix($prefix) |
||
115 | { |
||
116 | $this->prefix = (string) $prefix; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the prefix which will be added at the start of each input line. |
||
121 | * |
||
122 | * @return string The prefix which will be added at the start of each input line |
||
123 | */ |
||
124 | public function getPrefix() |
||
125 | { |
||
126 | return $this->prefix; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Creates a new PrefixLines filter using the passed in |
||
131 | * Reader for instantiation. |
||
132 | * |
||
133 | * @return PrefixLines A new filter based on this configuration, but filtering |
||
134 | * the specified reader |
||
135 | * |
||
136 | * @internal param A $object Reader object providing the underlying stream. |
||
137 | * Must not be <code>null</code>. |
||
138 | */ |
||
139 | public function chain(Reader $reader): Reader |
||
140 | { |
||
141 | $newFilter = new PrefixLines($reader); |
||
142 | $newFilter->setPrefix($this->getPrefix()); |
||
143 | $newFilter->setInitialized(true); |
||
144 | $newFilter->setProject($this->getProject()); |
||
145 | |||
146 | return $newFilter; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Initializes the prefix if it is available from the parameters. |
||
151 | */ |
||
152 | 1 | private function initialize() |
|
153 | { |
||
154 | 1 | $params = $this->getParameters(); |
|
155 | 1 | if (null !== $params) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
156 | 1 | for ($i = 0, $_i = count($params); $i < $_i; ++$i) { |
|
157 | 1 | if (self::PREFIX_KEY == $params[$i]->getName()) { |
|
158 | 1 | $this->prefix = (string) $params[$i]->getValue(); |
|
159 | |||
160 | 1 | break; |
|
161 | } |
||
162 | } |
||
163 | } |
||
164 | 1 | } |
|
165 | } |
||
166 |