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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
namespace Laminas\Config\Writer;
use Laminas\Config\Exception;
use function addslashes;
use function class_exists;
use function dirname;
use function file_put_contents;
use function interface_exists;
use function is_array;
use function is_bool;
use function is_int;
use function is_object;
use function is_string;
use function preg_match;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strlen;
use function trait_exists;
use function var_export;
use const E_WARNING;
use const LOCK_EX;
class PhpArray extends AbstractWriter
{
/**
* @var string
*/
const INDENT_STRING = ' ';
/**
* @var bool
*/
protected $useBracketArraySyntax = false;
/**
* @var bool
*/
protected $useClassNameScalars = false;
/**
* processConfig(): defined by AbstractWriter.
*
* @param array $config
* @return string
*/
public function processConfig(array $config)
{
$arraySyntax = [
'open' => $this->useBracketArraySyntax ? '[' : 'array(',
'close' => $this->useBracketArraySyntax ? ']' : ')'
];
return "<?php\n" .
"return " . $arraySyntax['open'] . "\n" . $this->processIndented($config, $arraySyntax) .
$arraySyntax['close'] . ";\n";
}
/**
* Sets whether or not to use the PHP 5.4+ "[]" array syntax.
*
* @param bool $value
* @return self
*/
public function setUseBracketArraySyntax($value)
{
$this->useBracketArraySyntax = $value;
return $this;
}
/**
* Sets whether or not to render resolvable FQN strings as scalars, using PHP 5.5+ class-keyword
*
* @param boolean $value
* @return self
*/
public function setUseClassNameScalars($value)
{
$this->useClassNameScalars = $value;
return $this;
}
/**
* @return boolean
*/
public function getUseClassNameScalars()
{
return $this->useClassNameScalars;
}
/**
* toFile(): defined by Writer interface.
*
* @see WriterInterface::toFile()
* @param string $filename
* @param mixed $config
* @param bool $exclusiveLock
* @return void
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
*/
public function toFile($filename, $config, $exclusiveLock = true)
{
if (empty($filename)) {
throw new Exception\InvalidArgumentException('No file name specified');
}
$flags = 0;
if ($exclusiveLock) {
$flags |= LOCK_EX;
}
set_error_handler(
function ($error, $message = '') use ($filename) {
throw new Exception\RuntimeException(
sprintf('Error writing to "%s": %s', $filename, $message),
$error
);
},
E_WARNING
);
try {
// for Windows, paths are escaped.
$dirname = str_replace('\\', '\\\\', dirname($filename));
$string = $this->toString($config);
$string = str_replace("'" . $dirname, "__DIR__ . '", $string);
file_put_contents($filename, $string, $flags);
} catch (\Exception $e) {
restore_error_handler();
throw $e;
}
restore_error_handler();
}
/**
* Recursively processes a PHP config array structure into a readable format.
*
* @param array $config
* @param array $arraySyntax
* @param int $indentLevel
* @return string
*/
protected function processIndented(array $config, array $arraySyntax, &$indentLevel = 1)
{
$arrayString = "";
foreach ($config as $key => $value) {
$arrayString .= str_repeat(self::INDENT_STRING, $indentLevel);
$arrayString .= (is_int($key) ? $key : $this->processStringKey($key)) . ' => ';
if (is_array($value)) {
if ($value === []) {
$arrayString .= $arraySyntax['open'] . $arraySyntax['close'] . ",\n";
} else {
$indentLevel++;
$arrayString .= $arraySyntax['open'] . "\n"
. $this->processIndented($value, $arraySyntax, $indentLevel)
. str_repeat(self::INDENT_STRING, --$indentLevel) . $arraySyntax['close'] . ",\n";
}
} elseif (is_object($value)) {
$arrayString .= var_export($value, true) . ",\n";
} elseif (is_string($value)) {
$arrayString .= $this->processStringValue($value) . ",\n";
} elseif (is_bool($value)) {
$arrayString .= ($value ? 'true' : 'false') . ",\n";
} elseif ($value === null) {
$arrayString .= "null,\n";
} else {
$arrayString .= $value . ",\n";
}
}
return $arrayString;
}
/**
* Process a string configuration value
*
* @param string $value
* @return string
*/
protected function processStringValue($value)
{
if ($this->useClassNameScalars && false !== ($fqnValue = $this->fqnStringToClassNameScalar($value))) {
return $fqnValue;
}
return var_export($value, true);
}
/**
* Process a string configuration key
*
* @param string $key
* @return string
*/
protected function processStringKey($key)
{
if ($this->useClassNameScalars && false !== ($fqnKey = $this->fqnStringToClassNameScalar($key))) {
return $fqnKey;
}
return "'" . addslashes($key) . "'";
}
/**
* Attempts to convert a FQN string to class name scalar.
* Returns false if string is not a valid FQN or can not be resolved to an existing name.
*
* @param string $string
* @return bool|string
*/
protected function fqnStringToClassNameScalar($string)
{
if (strlen($string) < 1) {
return false;
}
if ($string[0] !== '\\') {
$string = '\\' . $string;
}
if ($this->checkStringIsFqn($string)) {
return $string . '::class';
}
return false;
}
/**
* Check whether a string represents a resolvable FQCN
*
* @param string $string
* @return bool
*/
protected function checkStringIsFqn($string)
{
if (! preg_match('/^(?:\x5c[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $string)) {
return false;
}
return class_exists($string) || interface_exists($string) || trait_exists($string);
}
}