Completed
Push — master ( 7082f0...55a2b2 )
by Siad
15:25
created

PrefixLines::_initialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 20
rs 10
c 0
b 0
f 0
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:[email protected]">Yannick Lecaillez</a>
33
 * @author  hans lellelid, [email protected]
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
    const PREFIX_KEY = "lines";
46
47
    /**
48
     * The prefix to be used.
49
     *
50
     * @var string
51
     */
52
    private $prefix = null;
53
54
    /**
55
     * Adds a prefix to each line of input stream and returns resulting stream.
56
     *
57
     * @param  int $len
58
     * @return mixed buffer, -1 on EOF
59
     */
60
    public function read($len = null)
61
    {
62
        if (!$this->getInitialized()) {
63
            $this->initialize();
64
            $this->setInitialized(true);
65
        }
66
67
        $buffer = $this->in->read($len);
68
69
        if ($buffer === -1) {
70
            return -1;
71
        }
72
73
        $lines = explode("\n", $buffer);
74
        $filtered = [];
75
76
        foreach ($lines as $line) {
77
            $line = $this->prefix . $line;
78
            $filtered[] = $line;
79
        }
80
81
        $filtered_buffer = implode("\n", $filtered);
82
83
        return $filtered_buffer;
84
    }
85
86
    /**
87
     * Sets the prefix to add at the start of each input line.
88
     *
89
     * @param string $prefix The prefix to add at the start of each input line.
90
     *                       May be <code>null</code>, in which case no prefix
91
     *                       is added.
92
     */
93
    public function setPrefix($prefix)
94
    {
95
        $this->prefix = (string) $prefix;
96
    }
97
98
    /**
99
     * Returns the prefix which will be added at the start of each input line.
100
     *
101
     * @return string The prefix which will be added at the start of each input line
102
     */
103
    public function getPrefix()
104
    {
105
        return $this->prefix;
106
    }
107
108
    /**
109
     * Creates a new PrefixLines filter using the passed in
110
     * Reader for instantiation.
111
     *
112
     * @param Reader $reader
113
     * @return PrefixLines A new filter based on this configuration, but filtering
114
     *                the specified reader
115
     * @internal param A $object Reader object providing the underlying stream.
116
     *               Must not be <code>null</code>.
117
     */
118
    public function chain(Reader $reader): Reader
119
    {
120
        $newFilter = new PrefixLines($reader);
121
        $newFilter->setPrefix($this->getPrefix());
122
        $newFilter->setInitialized(true);
123
        $newFilter->setProject($this->getProject());
124
125
        return $newFilter;
126
    }
127
128
    /**
129
     * Initializes the prefix if it is available from the parameters.
130
     */
131
    private function initialize()
132
    {
133
        $params = $this->getParameters();
134
        if ($params !== null) {
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
135
            for ($i = 0, $_i = count($params); $i < $_i; $i++) {
136
                if (self::PREFIX_KEY == $params[$i]->getName()) {
137
                    $this->prefix = (string) $params[$i]->getValue();
138
                    break;
139
                }
140
            }
141
        }
142
    }
143
}
144