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
 * Filter which includes only those lines that contain the user-specified
22
 * regular expression matching strings.
23
 *
24
 * Example:
25
 * <pre><linecontainsregexp>
26
 *   <regexp pattern="foo*">
27
 * </linecontainsregexp></pre>
28
 *
29
 * Or:
30
 *
31
 * <pre><filterreader classname="phing.filters.LineContainsRegExp">
32
 *    <param type="regexp" value="foo*"/>
33
 * </filterreader></pre>
34
 *
35
 * This will fetch all those lines that contain the pattern <code>foo</code>
36
 *
37
 * @author  Yannick Lecaillez <yl@seasonfive.com>
38
 * @author  Hans Lellelid <hans@xmpl.org>
39
 * @see     FilterReader
40
 * @package phing.filters
41
 */
42
class LineContainsRegexp extends BaseParamFilterReader implements ChainableReader
43
{
44

45
    /**
46
     * Parameter name for regular expression.
47
     *
48
     * @var string
49
     */
50
    public const REGEXP_KEY = "regexp";
51
    public const NEGATE_KEY = 'negate';
52
    public const CS_KEY = 'casesensitive';
53

54
    /**
55
     * Regular expressions that are applied against lines.
56
     *
57
     * @var RegularExpression[]
58
     */
59
    private $regexps = [];
60

61
    /**
62
     * @var bool $negate
63
     */
64
    private $negate = false;
65

66
    /**
67
     * @var bool $casesensitive
68
     */
69
    private $casesensitive = true;
70

71
    /**
72
     * Returns all lines in a buffer that contain specified strings.
73
     *
74
     * @param int $len
75
     * @return mixed buffer, -1 on EOF
76
     * @throws IOException
77
     * @throws RegexpException
78
     */
79 1
    public function read($len = null)
80
    {
81 1
        if (!$this->getInitialized()) {
82 1
            $this->initialize();
83 1
            $this->setInitialized(true);
84
        }
85

86 1
        $buffer = $this->in->read($len);
87

88 1
        if ($buffer === -1) {
89 1
            return -1;
90
        }
91

92 1
        $lines = explode("\n", $buffer);
93 1
        $matched = [];
94

95 1
        $regexpsSize = count($this->regexps);
96 1
        foreach ($lines as $line) {
97 1
            for ($i = 0; $i < $regexpsSize; $i++) {
98 1
                $regexp = $this->regexps[$i];
99 1
                $re = $regexp->getRegexp($this->getProject());
100 1
                $re->setIgnoreCase(!$this->casesensitive);
101 1
                $matches = $re->matches($line);
102 1
                if (!$matches) {
103 1
                    $line = null;
104 1
                    break;
105
                }
106
            }
107 1
            if ($line !== null) {
108 1
                $matched[] = $line;
109
            }
110
        }
111 1
        $filtered_buffer = implode("\n", $matched);
112

113 1
        if ($this->isNegated()) {
114 1
            $filtered_buffer = implode("\n", array_diff($lines, $matched));
115
        }
116

117 1
        return $filtered_buffer;
118
    }
119

120
    /**
121
     * Whether to match casesensitevly.
122
     * @param bool $b
123
     */
124 1
    public function setCaseSensitive(bool $b)
125
    {
126 1
        $this->casesensitive = $b;
127
    }
128

129
    /**
130
     * Find out whether we match casesensitevly.
131
     *
132
     * @return boolean negation flag.
133
     */
134 0
    public function isCaseSensitive()
135
    {
136 0
        return $this->casesensitive;
137
    }
138

139
    /**
140
     * Set the negation mode.  Default false (no negation).
141
     *
142
     * @param boolean $b the boolean negation mode to set.
143
     */
144 1
    public function setNegate(bool $b)
145
    {
146 1
        $this->negate = $b;
147
    }
148

149
    /**
150
     * Find out whether we have been negated.
151
     *
152
     * @return boolean negation flag.
153
     */
154 1
    public function isNegated()
155
    {
156 1
        return $this->negate;
157
    }
158

159
    /**
160
     * Adds a <code>regexp</code> element.
161
     *
162
     * @return object regExp The <code>regexp</code> element added.
163
     */
164 0
    public function createRegexp()
165
    {
166 0
        $num = array_push($this->regexps, new RegularExpression());
167

168 0
        return $this->regexps[$num - 1];
169
    }
170

171
    /**
172
     * Sets the vector of regular expressions which must be contained within
173
     * a line read from the original stream in order for it to match this
174
     * filter.
175
     *
176
     * @param    array $regexps
177
     * @throws   Exception
178
     * @internal param An $regexps array of regular expressions which must be contained
179
     *                within a line in order for it to match in this filter. Must not be
180
     *                <code>null</code>.
181
     */
182 0
    public function setRegexps($regexps)
183
    {
184
        // type check, error must never occur, bad code of it does
185 0
        if (!is_array($regexps)) {
186 0
            throw new Exception("Expected an 'array', got something else");
187
        }
188 0
        $this->regexps = $regexps;
189
    }
190

191
    /**
192
     * Returns the array of regular expressions which must be contained within
193
     * a line read from the original stream in order for it to match this
194
     * filter.
195
     *
196
     * @return array The array of regular expressions which must be contained within
197
     *               a line read from the original stream in order for it to match this
198
     *               filter. The returned object is "live" - in other words, changes made to
199
     *               the returned object are mirrored in the filter.
200
     */
201 0
    public function getRegexps()
202
    {
203 0
        return $this->regexps;
204
    }
205

206
    /**
207
     * Set the regular expression as an attribute.
208
     */
209 0
    public function setRegexp($pattern)
210
    {
211 0
        $regexp = new RegularExpression();
212 0
        $regexp->setPattern($pattern);
213 0
        $this->regexps[] = $regexp;
214
    }
215

216
    /**
217
     * Creates a new LineContainsRegExp using the passed in
218
     * Reader for instantiation.
219
     *
220
     * @param Reader $reader
221
     * @return LineContainsRegexp A new filter based on this configuration, but filtering
222
     *                the specified reader
223
     * @throws Exception
224
     * @internal param A $object Reader object providing the underlying stream.
225
     *               Must not be <code>null</code>.
226
     */
227 0
    public function chain(Reader $reader): Reader
228
    {
229 0
        $newFilter = new LineContainsRegexp($reader);
230 0
        $newFilter->setRegexps($this->getRegexps());
231 0
        $newFilter->setNegate($this->isNegated());
232 0
        $newFilter->setCaseSensitive($this->isCaseSensitive());
233 0
        $newFilter->setInitialized(true);
234 0
        $newFilter->setProject($this->getProject());
235

236 0
        return $newFilter;
237
    }
238

239
    /**
240
     * Parses parameters to add user defined regular expressions.
241
     */
242 1
    private function initialize()
243
    {
244 1
        $params = $this->getParameters();
245 1
        if ($params !== null) {
246 1
            for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) {
247 1
                if (self::REGEXP_KEY === $params[$i]->getType()) {
248 1
                    $pattern = $params[$i]->getValue();
249 1
                    $regexp = new RegularExpression();
250 1
                    $regexp->setPattern($pattern);
251 1
                    $this->regexps[] = $regexp;
252 1
                } elseif (self::NEGATE_KEY === $params[$i]->getType()) {
253 1
                    $this->setNegate(Project::toBoolean($params[$i]->getValue()));
254 1
                } elseif (self::CS_KEY === $params[$i]->getType()) {
255 1
                    $this->setCaseSensitive(Project::toBoolean($params[$i]->getValue()));
256
                }
257
            }
258
        }
259
    }
260
}

Read our documentation on viewing source code .

Loading