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
 * Performs a regexp find/replace on stream.
22
 * <p>
23
 * Example:<br>
24
 * <pre>
25
 * <replaceregexp>
26
 *    <regexp pattern="\r\n" replace="\n"/>
27
 *    <regexp pattern="(\w+)\.xml" replace="\1.php" ignoreCase="true"/>
28
 * </replaceregexp>
29
 * </pre>
30
 *
31
 * @author  Hans Lellelid <hans@xmpl.org>
32
 * @package phing.filters
33
 */
34
class ReplaceRegexp extends BaseFilterReader implements ChainableReader
35
{
36

37
    /**
38
     * @var array RegularExpression[]
39
     */
40
    private $regexps = [];
41

42
    /**
43
     * Creator method handles nested <regexp> tags.
44
     *
45
     * @return RegularExpression
46
     */
47 1
    public function createRegexp()
48
    {
49 1
        $num = array_push($this->regexps, new RegularExpression());
50

51 1
        return $this->regexps[$num - 1];
52
    }
53

54
    /**
55
     * Sets the current regexps.
56
     * (Used when, e.g., cloning/chaining the method.)
57
     *
58
     * @param array RegularExpression[]
59
     */
60 1
    public function setRegexps($regexps)
61
    {
62 1
        $this->regexps = $regexps;
63
    }
64

65
    /**
66
     * Gets the current regexps.
67
     * (Used when, e.g., cloning/chaining the method.)
68
     *
69
     * @return array RegularExpression[]
70
     */
71 1
    public function getRegexps()
72
    {
73 1
        return $this->regexps;
74
    }
75

76
    /**
77
     * Returns the filtered stream.
78
     * The original stream is first read in fully, and the regex replace is performed.
79
     *
80
     * @param int $len Required $len for Reader compliance.
81
     *
82
     * @return mixed The filtered stream, or -1 if the end of the resulting stream has been reached.
83
     *
84
     * @throws IOException if the underlying stream throws an IOException
85
     * during reading
86
     */
87 1
    public function read($len = null)
88
    {
89 1
        $buffer = $this->in->read($len);
90

91 1
        if ($buffer === -1) {
92 1
            return -1;
93
        }
94

95
        // perform regex replace here ...
96 1
        foreach ($this->regexps as $exptype) {
97 1
            $regexp = $exptype->getRegexp($this->project);
98
            try {
99 1
                $buffer = $regexp->replace($buffer);
100 1
                $this->log(
101 1
                    "Performing regexp replace: /" . $regexp->getPattern() . "/" . $regexp->getReplace() . "/g" . $regexp->getModifiers(),
102 1
                    Project::MSG_VERBOSE
103
                );
104 0
            } catch (Exception $e) {
105
                // perhaps mismatch in params (e.g. no replace or pattern specified)
106 0
                $this->log("Error performing regexp replace: " . $e->getMessage(), Project::MSG_WARN);
107
            }
108
        }
109

110 1
        return $buffer;
111
    }
112

113
    /**
114
     * Creates a new ReplaceRegExp filter using the passed in
115
     * Reader for instantiation.
116
     *
117
     * @param Reader $reader A Reader object providing the underlying stream.
118
     *                       Must not be <code>null</code>.
119
     *
120
     * @return ReplaceRegexp A new filter based on this configuration, but filtering
121
     *                       the specified reader
122
     */
123 1
    public function chain(Reader $reader): Reader
124
    {
125 1
        $newFilter = new ReplaceRegexp($reader);
126 1
        $newFilter->setProject($this->getProject());
127 1
        $newFilter->setRegexps($this->getRegexps());
128

129 1
        return $newFilter;
130
    }
131
}

Read our documentation on viewing source code .

Loading