1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Properties\PropertiesUtil |
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
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/properties |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Properties; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Properties\Filter\PropertyStreamFilter; |
24
|
|
|
use AppserverIo\Properties\Filter\PropertyStreamFilterParams; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Utility class providing some helper functionality to handle properties files. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[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 PropertiesUtil |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The singleton instance. |
40
|
|
|
* |
41
|
|
|
* @var \AppserverIo\Properties\PropertiesUtil |
42
|
|
|
*/ |
43
|
|
|
protected static $instance; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* This is a utility class, so protect it against direct |
47
|
|
|
* instantiation. |
48
|
|
|
*/ |
49
|
1 |
|
private function __construct() |
50
|
|
|
{ |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This is a utility class, so protect it against cloning. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function __clone() |
59
|
|
|
{ |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return's the singleton instance and creates a new one if necessary. |
64
|
|
|
* |
65
|
|
|
* @return \AppserverIo\Properties\PropertiesUtil The singleton instance |
66
|
|
|
*/ |
67
|
2 |
|
public static function singleton() |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
// query whether or not we've already an instance created |
71
|
2 |
|
if (PropertiesUtil::$instance == null) { |
72
|
1 |
|
PropertiesUtil::$instance = new PropertiesUtil(); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
// return the singleton instance |
76
|
2 |
|
return PropertiesUtil::$instance; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Replaces the variables declared in the properties with the |
81
|
|
|
* properties value itself. |
82
|
|
|
* |
83
|
|
|
* @param \AppserverIo\Properties\PropertiesInterface $properties The properties with the variables/values to replace |
84
|
|
|
* @param string $pattern The pattern that declares the variables (in valid sprintf format) |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
1 |
|
public function replaceProperties(PropertiesInterface $properties, $pattern = PropertyStreamFilterParams::PATTERN) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
foreach ($properties as $key => $value) { |
|
|
|
|
91
|
1 |
|
$properties->setProperty($key, $this->replacePropertiesInString($properties, $value)); |
92
|
1 |
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Replaces the variables declared by the passed token with the |
97
|
|
|
* properties and returns the content. |
98
|
|
|
* |
99
|
|
|
* @param \AppserverIo\Properties\PropertiesInterface $properties The properties with the values to replace |
100
|
|
|
* @param string $string The string to replace the variables with the properties |
101
|
|
|
* @param string $pattern The pattern that declares the variables (in valid sprintf format) |
102
|
|
|
* |
103
|
|
|
* @return string The content of the file with the replaced variables |
104
|
|
|
*/ |
105
|
2 |
|
public function replacePropertiesInString(PropertiesInterface $properties, $string, $pattern = PropertyStreamFilterParams::PATTERN) |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
// open the stream |
109
|
2 |
|
$fp = fopen('php://temp', 'r+'); |
110
|
|
|
|
111
|
|
|
// write/rewind the content into the string to allow using stream filters |
112
|
2 |
|
fputs($fp, $string); |
113
|
2 |
|
rewind($fp); |
114
|
|
|
|
115
|
|
|
// replace the properties and close the stream |
116
|
2 |
|
$replaced = $this->replacePropertiesInStream($properties, $fp, $pattern); |
117
|
2 |
|
fclose($fp); |
118
|
|
|
|
119
|
|
|
// return the string with the properties replaced |
120
|
2 |
|
return $replaced; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Replaces the variables declared by the passed token with the |
125
|
|
|
* properties and returns the content. |
126
|
|
|
* |
127
|
|
|
* @param \AppserverIo\Properties\PropertiesInterface $properties The properties with the values to replace |
128
|
|
|
* @param resource $fp The file pointer to replace the variables with the properties |
129
|
|
|
* @param string $pattern The pattern that declares the variables (in valid sprintf format) |
130
|
|
|
* |
131
|
|
|
* @return string The content of the file with the replaced variables |
132
|
|
|
*/ |
133
|
2 |
|
public function replacePropertiesInStream(PropertiesInterface $properties, $fp, $pattern = PropertyStreamFilterParams::PATTERN) |
134
|
|
|
{ |
135
|
|
|
|
136
|
|
|
// initialize the params for the stream filter |
137
|
2 |
|
$params = new PropertyStreamFilterParams($properties, $pattern); |
138
|
|
|
|
139
|
|
|
// register the filter |
140
|
2 |
|
stream_filter_register(PropertyStreamFilter::NAME, 'AppserverIo\Properties\Filter\PropertyStreamFilter'); |
141
|
2 |
|
stream_filter_append($fp, PropertyStreamFilter::NAME, STREAM_FILTER_READ, $params); |
142
|
|
|
|
143
|
|
|
// replace the properties and close the stream |
144
|
2 |
|
return stream_get_contents($fp); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.