|
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
|
|
|
* Replaces tokens in the original input with the contents of a file. |
|
22
|
|
|
* The file to be used is controlled by the name of the token which |
|
23
|
|
|
* corresponds to the basename of the file to be used together with |
|
24
|
|
|
* the optional pre and postfix strings that is possible to set. |
|
25
|
|
|
* |
|
26
|
|
|
* By default all HTML entities in the file is replaced by the |
|
27
|
|
|
* corresponding HTML entities. This behaviour can be controlled by |
|
28
|
|
|
* the "translatehtml" parameter. |
|
29
|
|
|
* |
|
30
|
|
|
* Supported parameters are: |
|
31
|
|
|
* <pre> |
|
32
|
|
|
* prefix string Text to be prefixed to token before using as filename |
|
33
|
|
|
* postfix string Text to be prefixed to token before using as filename |
|
34
|
|
|
* dir string The directory where the files should be read from |
|
35
|
|
|
* translatehtml bool If we should translate all HTML entities in the file. |
|
36
|
|
|
* </pre> |
|
37
|
|
|
* Example: |
|
38
|
|
|
* |
|
39
|
|
|
* <pre><filterreader classname="phing.filters.ReplaceTokensWithFile"> |
|
40
|
|
|
* <param name="dir" value="examples/" /> |
|
41
|
|
|
* <param name="postfix" value=".php" /> |
|
42
|
|
|
* </filterreader></pre> |
|
43
|
|
|
* |
|
44
|
|
|
* @author johan persson, [email protected] |
|
45
|
|
|
* @see ReplaceTokensWithFile |
|
46
|
|
|
* @package phing.filters |
|
47
|
|
|
*/ |
|
48
|
|
|
class ReplaceTokensWithFile extends BaseParamFilterReader implements ChainableReader |
|
49
|
|
|
{ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Default "begin token" character. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
const DEFAULT_BEGIN_TOKEN = "#@#"; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Default "end token" character. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
const DEFAULT_END_TOKEN = "#@#"; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Array to hold the token sources that make tokens from |
|
67
|
|
|
* different sources available |
|
68
|
|
|
* |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
private $tokensources = []; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Character marking the beginning of a token. |
|
75
|
|
|
* |
|
76
|
|
|
* @var string |
|
77
|
|
|
*/ |
|
78
|
|
|
private $beginToken = ReplaceTokensWithFile::DEFAULT_BEGIN_TOKEN; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Character marking the end of a token. |
|
82
|
|
|
* |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
private $endToken = ReplaceTokensWithFile::DEFAULT_END_TOKEN; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* File prefix to be inserted in front of the token to create the |
|
89
|
|
|
* file name to be used. |
|
90
|
|
|
* |
|
91
|
|
|
* @var string |
|
92
|
|
|
*/ |
|
93
|
|
|
private $prefix = ''; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* File postfix to be inserted in front of the token to create the |
|
97
|
|
|
* file name to be used. |
|
98
|
|
|
* |
|
99
|
|
|
* @var string |
|
100
|
|
|
*/ |
|
101
|
|
|
private $postfix = ''; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Directory where to look for the files. The default is to look in the |
|
105
|
|
|
* current file. |
|
106
|
|
|
* |
|
107
|
|
|
* @var string |
|
108
|
|
|
*/ |
|
109
|
|
|
private $dir = './'; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Translate all HTML entities in the file to the corresponding HTML |
|
113
|
|
|
* entities before it is used as replacements. For example all '<' |
|
114
|
|
|
* will be translated to < before the content is inserted. |
|
115
|
|
|
* |
|
116
|
|
|
* @var boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
private $translatehtml = true; |
|
119
|
|
|
|
|
120
|
2 |
|
public function setTranslateHTML(bool $translate) |
|
121
|
|
|
{ |
|
122
|
2 |
|
$this->translatehtml = $translate; |
|
123
|
2 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the drectory where to look for the files to use for token replacement |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function getTranslateHTML() |
|
129
|
|
|
{ |
|
130
|
2 |
|
return $this->translatehtml; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets the drectory where to look for the files to use for token replacement |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $dir |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function setDir($dir) |
|
139
|
|
|
{ |
|
140
|
2 |
|
$this->dir = (string) $dir; |
|
141
|
2 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns the drectory where to look for the files to use for token replacement |
|
145
|
|
|
*/ |
|
146
|
2 |
|
public function getDir() |
|
147
|
|
|
{ |
|
148
|
2 |
|
return $this->dir; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Sets the prefix that is prepended to the token in order to create the file |
|
153
|
|
|
* name. For example if the token is 01 and the prefix is "example" then |
|
154
|
|
|
* the filename to look for will be "example01" |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $prefix |
|
157
|
|
|
*/ |
|
158
|
2 |
|
public function setPrefix($prefix) |
|
159
|
|
|
{ |
|
160
|
2 |
|
$this->prefix = (string) $prefix; |
|
161
|
2 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/* |
|
164
|
|
|
* Returns the prefix that is prepended to the token in order to create the file |
|
165
|
|
|
* name. For example if the token is 01 and the prefix is "example" then |
|
166
|
|
|
* the filename to look for will be "example01" |
|
167
|
|
|
*/ |
|
168
|
|
|
/** |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function getPrefix() |
|
172
|
|
|
{ |
|
173
|
2 |
|
return $this->prefix; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Sets the postfix that is added to the token in order to create the file |
|
178
|
|
|
* name. For example if the token is 01 and the postfix is ".php" then |
|
179
|
|
|
* the filename to look for will be "01.php" |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $postfix |
|
182
|
|
|
*/ |
|
183
|
2 |
|
public function setPostfix($postfix) |
|
184
|
|
|
{ |
|
185
|
2 |
|
$this->postfix = (string) $postfix; |
|
186
|
2 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Returns the postfix that is added to the token in order to create the file |
|
190
|
|
|
* name. For example if the token is 01 and the postfix is ".php" then |
|
191
|
|
|
* the filename to look for will be "01.php" |
|
192
|
|
|
*/ |
|
193
|
2 |
|
public function getPostfix() |
|
194
|
|
|
{ |
|
195
|
2 |
|
return $this->postfix; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Sets the "begin token" character. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $beginToken the character used to denote the beginning of a token. |
|
202
|
|
|
*/ |
|
203
|
2 |
|
public function setBeginToken($beginToken) |
|
204
|
|
|
{ |
|
205
|
2 |
|
$this->beginToken = (string) $beginToken; |
|
206
|
2 |
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns the "begin token" character. |
|
210
|
|
|
* |
|
211
|
|
|
* @return string The character used to denote the beginning of a token. |
|
212
|
|
|
*/ |
|
213
|
2 |
|
public function getBeginToken() |
|
214
|
|
|
{ |
|
215
|
2 |
|
return $this->beginToken; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Sets the "end token" character. |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $endToken the character used to denote the end of a token |
|
222
|
|
|
*/ |
|
223
|
2 |
|
public function setEndToken($endToken) |
|
224
|
|
|
{ |
|
225
|
2 |
|
$this->endToken = (string) $endToken; |
|
226
|
2 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Returns the "end token" character. |
|
230
|
|
|
* |
|
231
|
|
|
* @return string the character used to denote the beginning of a token |
|
232
|
|
|
*/ |
|
233
|
2 |
|
public function getEndToken() |
|
234
|
|
|
{ |
|
235
|
2 |
|
return $this->endToken; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Replace the token found with the appropriate file contents |
|
240
|
|
|
* |
|
241
|
|
|
* @param array $matches Array of 1 el containing key to search for. |
|
242
|
|
|
* @return string Text with which to replace key or value of key if none is found. |
|
243
|
|
|
*/ |
|
244
|
2 |
|
private function replaceTokenCallback($matches) |
|
245
|
|
|
{ |
|
246
|
2 |
|
$filetoken = $matches[1]; |
|
247
|
|
|
|
|
248
|
|
|
// We look in all specified directories for the named file and use |
|
249
|
|
|
// the first directory which has the file. |
|
250
|
2 |
|
$dirs = explode(';', $this->dir); |
|
251
|
|
|
|
|
252
|
2 |
|
$ndirs = count($dirs); |
|
253
|
2 |
|
$n = 0; |
|
254
|
2 |
|
$file = $dirs[$n] . $this->prefix . $filetoken . $this->postfix; |
|
255
|
|
|
|
|
256
|
2 |
|
while ($n < $ndirs && !is_readable($file)) { |
|
257
|
|
|
++$n; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
2 |
|
if (!is_readable($file) || $n >= $ndirs) { |
|
261
|
|
|
$this->log( |
|
262
|
|
|
"Can not read or find file \"$file\". Searched in directories: {$this->dir}", |
|
263
|
|
|
Project::MSG_WARN |
|
264
|
|
|
); |
|
265
|
|
|
//return $this->_beginToken . $filetoken . $this->_endToken; |
|
266
|
|
|
return "[Phing::Filters::ReplaceTokensWithFile: Can not find file " . '"' . $filetoken . $this->postfix . '"' . "]"; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
2 |
|
$buffer = file_get_contents($file); |
|
270
|
2 |
|
if ($this->translatehtml) { |
|
271
|
2 |
|
$buffer = htmlentities($buffer); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
2 |
|
if ($buffer === null) { |
|
275
|
|
|
$buffer = $this->beginToken . $filetoken . $this->endToken; |
|
276
|
|
|
$this->log("No corresponding file found for key \"$buffer\"", Project::MSG_WARN); |
|
277
|
|
|
} else { |
|
278
|
2 |
|
$this->log( |
|
279
|
2 |
|
"Replaced \"" . $this->beginToken . $filetoken . $this->endToken . "\" with content from file \"$file\"" |
|
280
|
|
|
); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
2 |
|
return $buffer; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Returns stream with tokens having been replaced with appropriate values. |
|
288
|
|
|
* If a replacement value is not found for a token, the token is left in the stream. |
|
289
|
|
|
* |
|
290
|
|
|
* @param int $len |
|
291
|
|
|
* @return mixed filtered stream, -1 on EOF. |
|
292
|
|
|
*/ |
|
293
|
2 |
|
public function read($len = null) |
|
294
|
|
|
{ |
|
295
|
2 |
|
if (!$this->getInitialized()) { |
|
296
|
|
|
$this->initialize(); |
|
297
|
|
|
$this->setInitialized(true); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
// read from next filter up the chain |
|
301
|
2 |
|
$buffer = $this->in->read($len); |
|
302
|
|
|
|
|
303
|
2 |
|
if ($buffer === -1) { |
|
304
|
2 |
|
return -1; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
// filter buffer |
|
308
|
2 |
|
$buffer = preg_replace_callback( |
|
309
|
2 |
|
"$" . preg_quote($this->beginToken) . "([\w\.\-:\/]+?)" . preg_quote($this->endToken) . "$", |
|
310
|
2 |
|
[$this, 'replaceTokenCallback'], |
|
311
|
2 |
|
$buffer |
|
312
|
|
|
); |
|
313
|
|
|
|
|
314
|
2 |
|
return $buffer; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Creates a new ReplaceTokensWithFile using the passed in |
|
319
|
|
|
* Reader for instantiation. |
|
320
|
|
|
* |
|
321
|
|
|
* @param Reader $reader |
|
322
|
|
|
* @return ReplaceTokensWithFile A new filter based on this configuration, but filtering |
|
323
|
|
|
* the specified reader |
|
324
|
|
|
* @internal param A $object Reader object providing the underlying stream. |
|
325
|
|
|
* Must not be <code>null</code>. |
|
326
|
|
|
*/ |
|
327
|
2 |
|
public function chain(Reader $reader): Reader |
|
328
|
|
|
{ |
|
329
|
2 |
|
$newFilter = new ReplaceTokensWithFile($reader); |
|
330
|
2 |
|
$newFilter->setProject($this->getProject()); |
|
331
|
2 |
|
$newFilter->setTranslateHTML($this->getTranslateHTML()); |
|
332
|
2 |
|
$newFilter->setDir($this->getDir()); |
|
333
|
2 |
|
$newFilter->setPrefix($this->getPrefix()); |
|
334
|
2 |
|
$newFilter->setPostfix($this->getPostfix()); |
|
335
|
2 |
|
$newFilter->setBeginToken($this->getBeginToken()); |
|
336
|
2 |
|
$newFilter->setEndToken($this->getEndToken()); |
|
337
|
2 |
|
$newFilter->setInitialized(true); |
|
338
|
|
|
|
|
339
|
2 |
|
return $newFilter; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Initializes parameters |
|
344
|
|
|
* This method is only called when this filter is used through |
|
345
|
|
|
* a <filterreader> tag in build file. |
|
346
|
|
|
*/ |
|
347
|
|
|
private function initialize() |
|
348
|
|
|
{ |
|
349
|
|
|
$params = $this->getParameters(); |
|
350
|
|
|
$n = count($params); |
|
351
|
|
|
|
|
352
|
|
|
if ($params !== null) { |
|
|
|
|
|
|
353
|
|
|
for ($i = 0; $i < $n; $i++) { |
|
354
|
|
|
if ($params[$i] !== null) { |
|
355
|
|
|
$name = $params[$i]->getName(); |
|
356
|
|
|
switch ($name) { |
|
357
|
|
|
case 'begintoken': |
|
358
|
|
|
$this->beginToken = $params[$i]->getValue(); |
|
359
|
|
|
break; |
|
360
|
|
|
case 'endtoken': |
|
361
|
|
|
$this->endToken = $params[$i]->getValue(); |
|
362
|
|
|
break; |
|
363
|
|
|
case 'dir': |
|
364
|
|
|
$this->dir = $params[$i]->getValue(); |
|
365
|
|
|
break; |
|
366
|
|
|
case 'prefix': |
|
367
|
|
|
$this->prefix = $params[$i]->getValue(); |
|
368
|
|
|
break; |
|
369
|
|
|
case 'postfix': |
|
370
|
|
|
$this->postfix = $params[$i]->getValue(); |
|
371
|
|
|
break; |
|
372
|
|
|
case 'translatehtml': |
|
373
|
|
|
$this->translatehtml = $params[$i]->getValue(); |
|
374
|
|
|
break; |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|