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
 * Attaches a prefix to every line.
22
 *
23
 * Example:
24
 * <pre><prefixlines prefix="Foo"/></pre>
25
 *
26
 * Or:
27
 *
28
 * <pre><filterreader classname="phing.filters.PrefixLines">
29
 *  <param name="prefix" value="Foo"/>
30
 * </filterreader></pre>
31
 *
32
 * @author  <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
33
 * @author  hans lellelid, hans@velum.net
34
 * @see     FilterReader
35
 * @package phing.filters
36
 */
37
class PrefixLines extends BaseParamFilterReader implements ChainableReader
38
{
39

40
    /**
41
     * Parameter name for the prefix.
42
     *
43
     * @var string
44
     */
45
    public const PREFIX_KEY = "lines";
46

47
    /**
48
     * The prefix to be used.
49
     *
50
     * @var string
51
     */
52
    private $prefix;
53

54
    /** @var string|null $queuedData */
55
    private $queuedData;
56

57
    /**
58
     * Adds a prefix to each line of input stream and returns resulting stream.
59
     *
60
     * @param int $len
61
     * @return mixed buffer, -1 on EOF
62
     * @throws IOException
63
     */
64 1
    public function read($len = null)
65
    {
66 1
        if (!$this->getInitialized()) {
67 1
            $this->initialize();
68 1
            $this->setInitialized(true);
69
        }
70

71 1
        $ch = -1;
72

73 1
        if ($this->queuedData !== null && $this->queuedData === '') {
74 0
            $this->queuedData = null;
75
        }
76

77 1
        if ($this->queuedData !== null) {
78 1
            $ch = $this->queuedData[0];
79 1
            $this->queuedData = (string) substr($this->queuedData, 1);
80 1
            if ($this->queuedData === '') {
81 1
                $this->queuedData = null;
82
            }
83
        } else {
84 1
            $this->queuedData = $this->readLine();
85 1
            if ($this->queuedData === null) {
86 1
                $ch = -1;
87
            } else {
88 1
                if ($this->prefix !== null) {
89 1
                    $this->queuedData = $this->prefix . $this->queuedData;
90
                }
91 1
                return $this->read();
92
            }
93
        }
94

95 1
        return $ch;
96
    }
97

98
    /**
99
     * Sets the prefix to add at the start of each input line.
100
     *
101
     * @param string $prefix The prefix to add at the start of each input line.
102
     *                       May be <code>null</code>, in which case no prefix
103
     *                       is added.
104
     */
105 0
    public function setPrefix($prefix)
106
    {
107 0
        $this->prefix = (string) $prefix;
108
    }
109

110
    /**
111
     * Returns the prefix which will be added at the start of each input line.
112
     *
113
     * @return string The prefix which will be added at the start of each input line
114
     */
115 0
    public function getPrefix()
116
    {
117 0
        return $this->prefix;
118
    }
119

120
    /**
121
     * Creates a new PrefixLines filter using the passed in
122
     * Reader for instantiation.
123
     *
124
     * @param Reader $reader
125
     * @return PrefixLines A new filter based on this configuration, but filtering
126
     *                the specified reader
127
     * @internal param A $object Reader object providing the underlying stream.
128
     *               Must not be <code>null</code>.
129
     */
130 0
    public function chain(Reader $reader): Reader
131
    {
132 0
        $newFilter = new PrefixLines($reader);
133 0
        $newFilter->setPrefix($this->getPrefix());
134 0
        $newFilter->setInitialized(true);
135 0
        $newFilter->setProject($this->getProject());
136

137 0
        return $newFilter;
138
    }
139

140
    /**
141
     * Initializes the prefix if it is available from the parameters.
142
     */
143 1
    private function initialize()
144
    {
145 1
        $params = $this->getParameters();
146 1
        if ($params !== null) {
147 1
            for ($i = 0, $_i = count($params); $i < $_i; $i++) {
148 1
                if (self::PREFIX_KEY == $params[$i]->getName()) {
149 1
                    $this->prefix = (string) $params[$i]->getValue();
150 1
                    break;
151
                }
152
            }
153
        }
154
    }
155
}

Read our documentation on viewing source code .

Loading