|
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
|
|
|
namespace Phing\Task\System\Property; |
|
21
|
|
|
|
|
22
|
|
|
use Exception; |
|
23
|
|
|
use Phing\Exception\BuildException; |
|
24
|
|
|
use Phing\Project; |
|
25
|
|
|
use Phing\Task\System\Property\AbstractPropertySetterTask; |
|
26
|
|
|
use Phing\Util\Regexp; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Regular Expression Task for properties. |
|
30
|
|
|
* |
|
31
|
|
|
* <pre> |
|
32
|
|
|
* <propertyregex property="pack.name" |
|
33
|
|
|
* subject="package.ABC.name" |
|
34
|
|
|
* pattern="package\.([^.]*)\.name" |
|
35
|
|
|
* match="$1" |
|
36
|
|
|
* casesensitive="false" |
|
37
|
|
|
* defaultvalue="test1"/> |
|
38
|
|
|
* |
|
39
|
|
|
* <echo message="${pack.name}"/> |
|
40
|
|
|
* |
|
41
|
|
|
* <propertyregex property="pack.name" |
|
42
|
|
|
* override="true" |
|
43
|
|
|
* subject="package.ABC.name" |
|
44
|
|
|
* pattern="(package)\.[^.]*\.(name)" |
|
45
|
|
|
* replace="$1.DEF.$2" |
|
46
|
|
|
* casesensitive="false" |
|
47
|
|
|
* defaultvalue="test2"/> |
|
48
|
|
|
* |
|
49
|
|
|
* <echo message="${pack.name}"/> |
|
50
|
|
|
* |
|
51
|
|
|
* </pre> |
|
52
|
|
|
* |
|
53
|
|
|
* @author Siad Ardroumli <[email protected]> |
|
54
|
|
|
* @package phing.tasks.regex |
|
55
|
|
|
*/ |
|
56
|
|
|
class RegexTask extends AbstractPropertySetterTask |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* @var string $subject |
|
60
|
|
|
*/ |
|
61
|
|
|
private $subject; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string $pattern |
|
65
|
|
|
*/ |
|
66
|
|
|
private $pattern; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string $match |
|
70
|
|
|
*/ |
|
71
|
|
|
private $match; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string $replace |
|
75
|
|
|
*/ |
|
76
|
|
|
private $replace; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var string $defaultValue |
|
80
|
|
|
*/ |
|
81
|
|
|
private $defaultValue; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var bool $caseSensitive |
|
85
|
|
|
*/ |
|
86
|
|
|
private $caseSensitive = true; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var array $modifiers |
|
90
|
|
|
*/ |
|
91
|
|
|
private $modifiers = ''; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var Regexp $reg |
|
95
|
|
|
*/ |
|
96
|
|
|
private $reg; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @var int $limit |
|
100
|
|
|
*/ |
|
101
|
|
|
private $limit = -1; |
|
102
|
|
|
|
|
103
|
3 |
|
public function init() |
|
104
|
|
|
{ |
|
105
|
3 |
|
$this->reg = new Regexp(); |
|
106
|
3 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $limit |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setLimit($limit) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->limit = $limit; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $subject |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function setSubject($subject) |
|
120
|
|
|
{ |
|
121
|
3 |
|
$this->subject = $subject; |
|
122
|
3 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $defaultValue |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function setDefaultValue($defaultValue) |
|
128
|
|
|
{ |
|
129
|
3 |
|
$this->log('Set default value to ' . $defaultValue, Project::MSG_DEBUG); |
|
130
|
|
|
|
|
131
|
3 |
|
$this->defaultValue = $defaultValue; |
|
132
|
3 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $pattern |
|
136
|
|
|
* @throws BuildException |
|
137
|
|
|
*/ |
|
138
|
3 |
|
public function setPattern($pattern) |
|
139
|
|
|
{ |
|
140
|
3 |
|
if ($this->pattern !== null) { |
|
141
|
|
|
throw new BuildException( |
|
142
|
|
|
'Cannot specify more than one regular expression' |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
$this->log('Set pattern to ' . $pattern, Project::MSG_DEBUG); |
|
147
|
|
|
|
|
148
|
3 |
|
$this->pattern = $pattern; |
|
149
|
3 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param $replace |
|
153
|
|
|
* @throws BuildException |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function setReplace($replace) |
|
156
|
|
|
{ |
|
157
|
1 |
|
if ($this->replace !== null) { |
|
158
|
|
|
throw new BuildException( |
|
159
|
|
|
'Cannot specify more than one replace expression' |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
1 |
|
if ($this->match !== null) { |
|
163
|
|
|
throw new BuildException( |
|
164
|
|
|
'You cannot specify both a select and replace expression' |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
$this->log('Set replace to ' . $replace, Project::MSG_DEBUG); |
|
169
|
|
|
|
|
170
|
1 |
|
$this->replace = $replace; |
|
171
|
1 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param $match |
|
175
|
|
|
* @throws BuildException |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function setMatch($match) |
|
178
|
|
|
{ |
|
179
|
2 |
|
if ($this->match !== null) { |
|
180
|
|
|
throw new BuildException( |
|
181
|
|
|
'Cannot specify more than one match expression' |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
2 |
|
$this->log('Set match to ' . $match, Project::MSG_DEBUG); |
|
186
|
|
|
|
|
187
|
2 |
|
$this->match = $match; |
|
188
|
2 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param $caseSensitive |
|
192
|
|
|
*/ |
|
193
|
3 |
|
public function setCaseSensitive($caseSensitive) |
|
194
|
|
|
{ |
|
195
|
3 |
|
$this->log("Set case-sensitive to $caseSensitive", Project::MSG_DEBUG); |
|
196
|
|
|
|
|
197
|
3 |
|
$this->caseSensitive = $caseSensitive; |
|
198
|
3 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return mixed|string |
|
202
|
|
|
* @throws BuildException |
|
203
|
|
|
*/ |
|
204
|
1 |
|
protected function doReplace() |
|
205
|
|
|
{ |
|
206
|
1 |
|
if ($this->replace === null) { |
|
207
|
|
|
throw new BuildException('No replace expression specified.'); |
|
208
|
|
|
} |
|
209
|
1 |
|
$this->reg->setPattern($this->pattern); |
|
210
|
1 |
|
$this->reg->setReplace($this->replace); |
|
211
|
1 |
|
$this->reg->setModifiers($this->modifiers); |
|
|
|
|
|
|
212
|
1 |
|
$this->reg->setIgnoreCase(!$this->caseSensitive); |
|
213
|
1 |
|
$this->reg->setLimit($this->limit); |
|
214
|
|
|
|
|
215
|
|
|
try { |
|
216
|
1 |
|
$output = $this->reg->replace($this->subject); |
|
217
|
|
|
} catch (Exception $e) { |
|
218
|
|
|
$output = $this->defaultValue; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
1 |
|
return $output; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return string |
|
226
|
|
|
* |
|
227
|
|
|
* @throws BuildException |
|
228
|
|
|
*/ |
|
229
|
2 |
|
protected function doSelect() |
|
230
|
|
|
{ |
|
231
|
2 |
|
$this->reg->setPattern($this->pattern); |
|
232
|
2 |
|
$this->reg->setModifiers($this->modifiers); |
|
|
|
|
|
|
233
|
2 |
|
$this->reg->setIgnoreCase(!$this->caseSensitive); |
|
234
|
|
|
|
|
235
|
2 |
|
$output = $this->defaultValue; |
|
236
|
|
|
|
|
237
|
|
|
try { |
|
238
|
2 |
|
if ($this->reg->matches($this->subject)) { |
|
239
|
2 |
|
$output = $this->reg->getGroup((int) ltrim($this->match, '$')); |
|
240
|
|
|
} |
|
241
|
|
|
} catch (Exception $e) { |
|
242
|
|
|
throw new BuildException($e); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
2 |
|
return $output; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @throws BuildException |
|
250
|
|
|
*/ |
|
251
|
3 |
|
protected function validate() |
|
252
|
|
|
{ |
|
253
|
3 |
|
if ($this->pattern === null) { |
|
254
|
|
|
throw new BuildException('No match expression specified.'); |
|
255
|
|
|
} |
|
256
|
3 |
|
if ($this->replace === null && $this->match === null) { |
|
257
|
|
|
throw new BuildException( |
|
258
|
|
|
'You must specify either a preg_replace or preg_match pattern' |
|
259
|
|
|
); |
|
260
|
|
|
} |
|
261
|
3 |
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @throws BuildException |
|
265
|
|
|
*/ |
|
266
|
3 |
|
public function main() |
|
267
|
|
|
{ |
|
268
|
3 |
|
$this->validate(); |
|
269
|
|
|
|
|
270
|
3 |
|
$output = $this->match; |
|
|
|
|
|
|
271
|
|
|
|
|
272
|
3 |
|
if ($this->replace !== null) { |
|
273
|
1 |
|
$output = $this->doReplace(); |
|
274
|
|
|
} else { |
|
275
|
2 |
|
$output = $this->doSelect(); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
3 |
|
if ($output !== null) { |
|
|
|
|
|
|
279
|
3 |
|
$this->setPropertyValue($output); |
|
280
|
|
|
} |
|
281
|
3 |
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|