1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Properties\Filter\PropertyStreamFilter |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @author Bernhard Wick <[email protected]> |
16
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
17
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
18
|
|
|
* @link http://github.com/appserver-io/properties |
19
|
|
|
* @link http://www.appserver.io |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace AppserverIo\Properties\Filter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* A stream filter implementation that replaces the variables defined in a stream with |
26
|
|
|
* the content of the properties file passed to the constructor. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @author Bernhard Wick <[email protected]> |
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link http://github.com/appserver-io/properties |
33
|
|
|
* @link http://www.appserver.io |
34
|
|
|
*/ |
35
|
|
|
class PropertyStreamFilter extends \php_user_filter |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The unique filter name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
const NAME = 'appserver-io.properties.filter.property-stream-filter'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The filter parameters. |
47
|
|
|
* |
48
|
|
|
* @var \AppserverIo\Properties\StreamFilterParams |
49
|
|
|
*/ |
50
|
|
|
public $params; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the params. |
54
|
|
|
* |
55
|
|
|
* @return \AppserverIo\Properties\StreamFilterParams The params |
56
|
|
|
*/ |
57
|
2 |
|
public function getParams() |
58
|
|
|
{ |
59
|
2 |
|
return $this->params; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This method is called whenever data is read from or written to the attached |
64
|
|
|
* stream (such as with fread() or fwrite()). |
65
|
|
|
* |
66
|
|
|
* @param resource $in A resource pointing to a bucket brigade which contains one or more bucket objects containing data to be filtered |
67
|
|
|
* @param resource $out A resource pointing to a second bucket brigade into which your modified buckets should be placed |
68
|
|
|
* @param integer $consumed Should be incremented by the length of the data which your filter reads in and alters |
69
|
|
|
* @param boolean $closing If the stream is in the process of closing, the closing parameter will be set to TRUE |
70
|
|
|
* |
71
|
|
|
* @return integer Whether the filter succeeded or not |
72
|
|
|
* @see php_user_filter::filter() |
73
|
|
|
*/ |
74
|
2 |
|
public function filter($in, $out, &$consumed, $closing) |
75
|
|
|
{ |
76
|
|
|
|
77
|
|
|
// load the properties from the params |
78
|
2 |
|
$pattern = $this->getParams()->getPattern(); |
79
|
2 |
|
$properties = $this->getParams()->getProperties(); |
80
|
|
|
|
81
|
|
|
// stop processing if we can't find the properties |
82
|
2 |
|
if ($properties == null) { |
83
|
|
|
return PSFS_ERR_FATAL; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// while we can read from the stream |
87
|
2 |
|
while ($bucket = stream_bucket_make_writeable($in)) { |
88
|
|
|
// try to find and replace the variables |
89
|
2 |
|
foreach ($properties->getKeys() as $propertyName) { |
90
|
2 |
|
$bucket->data = str_replace( |
91
|
2 |
|
sprintf($pattern, $propertyName), |
92
|
2 |
|
$properties->getProperty($propertyName), |
93
|
2 |
|
$bucket->data |
94
|
2 |
|
); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
// update the stream |
98
|
2 |
|
$consumed += $bucket->datalen; |
99
|
2 |
|
stream_bucket_append($out, $bucket); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
// continue applying filters |
103
|
2 |
|
return PSFS_PASS_ON; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|