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
 * Regular Expression Task for properties.
22
 *
23
 * <pre>
24
 *   <propertyregex property="pack.name"
25
 *                  subject="package.ABC.name"
26
 *                  pattern="package\.([^.]*)\.name"
27
 *                  match="$1"
28
 *                  casesensitive="false"
29
 *                  defaultvalue="test1"/>
30
 *
31
 *   <echo message="${pack.name}"/>
32
 *
33
 *   <propertyregex property="pack.name"
34
 *                  override="true"
35
 *                  subject="package.ABC.name"
36
 *                  pattern="(package)\.[^.]*\.(name)"
37
 *                  replace="$1.DEF.$2"
38
 *                  casesensitive="false"
39
 *                  defaultvalue="test2"/>
40
 *
41
 *   <echo message="${pack.name}"/>
42
 *
43
 * </pre>
44
 *
45
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
46
 * @package phing.tasks.regex
47
 */
48
class RegexTask extends AbstractPropertySetterTask
49
{
50
    /**
51
     * @var string $subject
52
     */
53
    private $subject;
54

55
    /**
56
     * @var string $pattern
57
     */
58
    private $pattern;
59

60
    /**
61
     * @var string $match
62
     */
63
    private $match;
64

65
    /**
66
     * @var string $replace
67
     */
68
    private $replace;
69

70
    /**
71
     * @var string $defaultValue
72
     */
73
    private $defaultValue;
74

75
    /**
76
     * @var bool $caseSensitive
77
     */
78
    private $caseSensitive = true;
79

80
    /**
81
     * @var array $modifiers
82
     */
83
    private $modifiers = '';
84

85
    /**
86
     * @var Regexp $reg
87
     */
88
    private $reg;
89

90
    /**
91
     * @var int $limit
92
     */
93
    private $limit = -1;
94

95 1
    public function init()
96
    {
97 1
        $this->reg = new Regexp();
98
    }
99

100
    /**
101
     * @param int $limit
102
     */
103 0
    public function setLimit($limit)
104
    {
105 0
        $this->limit = $limit;
106
    }
107

108
    /**
109
     * @param string $subject
110
     */
111 1
    public function setSubject($subject)
112
    {
113 1
        $this->subject = $subject;
114
    }
115

116
    /**
117
     * @param string $defaultValue
118
     */
119 1
    public function setDefaultValue($defaultValue)
120
    {
121 1
        $this->log('Set default value to ' . $defaultValue, Project::MSG_DEBUG);
122

123 1
        $this->defaultValue = $defaultValue;
124
    }
125

126
    /**
127
     * @param  string $pattern
128
     * @throws BuildException
129
     */
130 1
    public function setPattern($pattern)
131
    {
132 1
        if ($this->pattern !== null) {
133 0
            throw new BuildException(
134 0
                'Cannot specify more than one regular expression'
135
            );
136
        }
137

138 1
        $this->log('Set pattern to ' . $pattern, Project::MSG_DEBUG);
139

140 1
        $this->pattern = $pattern;
141
    }
142

143
    /**
144
     * @param $replace
145
     * @throws BuildException
146
     */
147 1
    public function setReplace($replace)
148
    {
149 1
        if ($this->replace !== null) {
150 0
            throw new BuildException(
151 0
                'Cannot specify more than one replace expression'
152
            );
153
        }
154 1
        if ($this->match !== null) {
155 0
            throw new BuildException(
156 0
                'You cannot specify both a select and replace expression'
157
            );
158
        }
159

160 1
        $this->log('Set replace to ' . $replace, Project::MSG_DEBUG);
161

162 1
        $this->replace = $replace;
163
    }
164

165
    /**
166
     * @param $match
167
     * @throws BuildException
168
     */
169 1
    public function setMatch($match)
170
    {
171 1
        if ($this->match !== null) {
172 0
            throw new BuildException(
173 0
                'Cannot specify more than one match expression'
174
            );
175
        }
176

177 1
        $this->log('Set match to ' . $match, Project::MSG_DEBUG);
178

179 1
        $this->match = $match;
180
    }
181

182
    /**
183
     * @param $caseSensitive
184
     */
185 1
    public function setCaseSensitive($caseSensitive)
186
    {
187 1
        $this->log("Set case-sensitive to $caseSensitive", Project::MSG_DEBUG);
188

189 1
        $this->caseSensitive = $caseSensitive;
190
    }
191

192
    /**
193
     * @return mixed|string
194
     * @throws BuildException
195
     */
196 1
    protected function doReplace()
197
    {
198 1
        if ($this->replace === null) {
199 0
            throw new BuildException('No replace expression specified.');
200
        }
201 1
        $this->reg->setPattern($this->pattern);
202 1
        $this->reg->setReplace($this->replace);
203 1
        $this->reg->setModifiers($this->modifiers);
204 1
        $this->reg->setIgnoreCase(!$this->caseSensitive);
205 1
        $this->reg->setLimit($this->limit);
206

207
        try {
208 1
            $output = $this->reg->replace($this->subject);
209 0
        } catch (Exception $e) {
210 0
            $output = $this->defaultValue;
211
        }
212

213 1
        return $output;
214
    }
215

216
    /**
217
     * @return string
218
     *
219
     * @throws BuildException
220
     */
221 1
    protected function doSelect()
222
    {
223 1
        $this->reg->setPattern($this->pattern);
224 1
        $this->reg->setModifiers($this->modifiers);
225 1
        $this->reg->setIgnoreCase(!$this->caseSensitive);
226

227 1
        $output = $this->defaultValue;
228

229
        try {
230 1
            if ($this->reg->matches($this->subject)) {
231 1
                $output = $this->reg->getGroup((int) ltrim($this->match, '$'));
232
            }
233 0
        } catch (Exception $e) {
234 0
            throw new BuildException($e);
235
        }
236

237 1
        return $output;
238
    }
239

240
    /**
241
     * @throws BuildException
242
     */
243 1
    protected function validate()
244
    {
245 1
        if ($this->pattern === null) {
246 0
            throw new BuildException('No match expression specified.');
247
        }
248 1
        if ($this->replace === null && $this->match === null) {
249 0
            throw new BuildException(
250 0
                'You must specify either a preg_replace or preg_match pattern'
251
            );
252
        }
253
    }
254

255
    /**
256
     * @throws BuildException
257
     */
258 1
    public function main()
259
    {
260 1
        $this->validate();
261

262 1
        $output = $this->match;
263

264 1
        if ($this->replace !== null) {
265 1
            $output = $this->doReplace();
266
        } else {
267 1
            $output = $this->doSelect();
268
        }
269

270 1
        if ($output !== null) {
271 1
            $this->setPropertyValue($output);
272
        }
273
    }
274
}

Read our documentation on viewing source code .

Loading