1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @see https://github.com/zendframework/zend-config for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-config/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Config\Reader;
use Zend\Config\Exception;
/**
* Java-style properties config reader.
*/
class JavaProperties implements ReaderInterface
{
const DELIMITER_DEFAULT = ':';
const WHITESPACE_TRIM = true;
const WHITESPACE_KEEP = false;
/**
* Directory of the Java-style properties file
*
* @var string
*/
protected $directory;
/**
* Delimiter for key/value pairs.
*/
private $delimiter;
/*
* Whether or not to trim whitespace from discovered keys and values.
*
* @var bool
*/
private $trimWhitespace;
/**
* @param string $delimiter Delimiter to use for key/value pairs; defaults
* to self::DELIMITER_DEFAULT (':')
* @param bool $trimWhitespace
* @throws Exception\InvalidArgumentException for invalid $delimiter values.
*/
public function __construct($delimiter = self::DELIMITER_DEFAULT, $trimWhitespace = self::WHITESPACE_KEEP)
{
if (! is_string($delimiter) || '' === $delimiter) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid delimiter of type "%s"; must be a non-empty string',
is_object($delimiter) ? get_class($delimiter) : gettype($delimiter)
));
}
$this->delimiter = $delimiter;
$this->trimWhitespace = (bool) $trimWhitespace;
}
/**
* fromFile(): defined by Reader interface.
*
* @see ReaderInterface::fromFile()
* @param string $filename
* @return array
* @throws Exception\RuntimeException if the file cannot be read
*/
public function fromFile($filename)
{
if (! is_file($filename) || ! is_readable($filename)) {
throw new Exception\RuntimeException(sprintf(
"File '%s' doesn't exist or not readable",
$filename
));
}
$this->directory = dirname($filename);
$config = $this->parse(file_get_contents($filename));
return $this->process($config);
}
/**
* fromString(): defined by Reader interface.
*
* @see ReaderInterface::fromString()
* @param string $string
* @return array
* @throws Exception\RuntimeException if an @include key is found
*/
public function fromString($string)
{
if (empty($string)) {
return [];
}
$this->directory = null;
$config = $this->parse($string);
return $this->process($config);
}
/**
* Process the array for @include
*
* @param array $data
* @return array
* @throws Exception\RuntimeException if an @include key is found
*/
protected function process(array $data)
{
foreach ($data as $key => $value) {
if (trim($key) === '@include') {
if ($this->directory === null) {
throw new Exception\RuntimeException('Cannot process @include statement for a string');
}
$reader = clone $this;
unset($data[$key]);
$data = array_replace_recursive($data, $reader->fromFile($this->directory . '/' . $value));
}
}
return $data;
}
/**
* Parse Java-style properties string
*
* @todo Support use of the equals sign "key=value" as key-value delimiter
* @todo Ignore whitespace that precedes text past the first line of multiline values
*
* @param string $string
* @return array
*/
protected function parse($string)
{
$delimiter = $this->delimiter;
$delimLength = strlen($delimiter);
$result = [];
$lines = explode("\n", $string);
$key = "";
$isWaitingOtherLine = false;
foreach ($lines as $i => $line) {
// Ignore empty lines and commented lines
if (empty($line)
|| (! $isWaitingOtherLine && strpos($line, "#") === 0)
|| (! $isWaitingOtherLine && strpos($line, "!") === 0)
) {
continue;
}
// Add a new key-value pair or append value to a previous pair
if (! $isWaitingOtherLine) {
$key = substr($line, 0, strpos($line, $delimiter));
$value = substr($line, strpos($line, $delimiter) + $delimLength, strlen($line));
} else {
$value .= $line;
}
// Check if ends with single '\' (indicating another line is expected)
if (strrpos($value, "\\") === strlen($value) - strlen("\\")) {
$value = substr($value, 0, strlen($value) - 1);
$isWaitingOtherLine = true;
} else {
$isWaitingOtherLine = false;
}
$key = $this->trimWhitespace ? trim($key) : $key;
$value = $this->trimWhitespace && ! $isWaitingOtherLine
? trim($value)
: $value;
$result[$key] = stripslashes($value);
unset($lines[$i]);
}
return $result;
}
}