|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riimu\Kit\PHPEncoder; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A highly customisable library for generating PHP code from variables. |
|
7
|
|
|
* |
|
8
|
|
|
* PHPEncoder provides a functionality similar to var_export(), but allows more |
|
9
|
|
|
* customisation and wider range of features. Better customisation options make |
|
10
|
|
|
* it easier to generate static PHP files when you need the file to formatted |
|
11
|
|
|
* in specific way. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) 2014-2020 Riikka Kalliomäki |
|
15
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
16
|
|
|
*/ |
|
17
|
|
|
class PHPEncoder |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Encoder\Encoder[] List of value encoders */ |
|
20
|
|
|
private $encoders; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array List of defined encoder option values. */ |
|
23
|
|
|
private $options; |
|
24
|
|
|
|
|
25
|
|
|
/** @var array Default values for options in the encoder */ |
|
26
|
|
|
private static $defaultOptions = [ |
|
27
|
|
|
'whitespace' => true, |
|
28
|
|
|
'recursion.detect' => true, |
|
29
|
|
|
'recursion.ignore' => false, |
|
30
|
|
|
'recursion.max' => false, |
|
31
|
|
|
'hex.capitalize' => false, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Creates a new PHPEncoder instance. |
|
36
|
|
|
* |
|
37
|
|
|
* The constructor allows you to provide the list of default encoding options |
|
38
|
|
|
* used by the encoder. Note that if you are using custom value encoders, you |
|
39
|
|
|
* must provide them in the constructor if you are providing options for them |
|
40
|
|
|
* or otherwise the options will be considered invalid. |
|
41
|
|
|
* |
|
42
|
|
|
* Using the second parameter you can also provide a list of value encoders used |
|
43
|
|
|
* by the encoder. If null is provided, the list of default value encoders will |
|
44
|
|
|
* be used instead. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $options List of encoder options |
|
47
|
|
|
* @param Encoder\Encoder[]|null $encoders List of encoders to use or null for defaults |
|
48
|
|
|
* @throws InvalidOptionException If any of the encoder options are invalid |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(array $options = [], array $encoders = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->options = self::$defaultOptions; |
|
53
|
|
|
|
|
54
|
|
|
if ($encoders === null) { |
|
55
|
|
|
$this->encoders = [ |
|
56
|
|
|
new Encoder\NullEncoder(), |
|
57
|
|
|
new Encoder\BooleanEncoder(), |
|
58
|
|
|
new Encoder\IntegerEncoder(), |
|
59
|
|
|
new Encoder\FloatEncoder(), |
|
60
|
|
|
new Encoder\StringEncoder(), |
|
61
|
|
|
new Encoder\ArrayEncoder(), |
|
62
|
|
|
new Encoder\GMPEncoder(), |
|
63
|
|
|
new Encoder\ObjectEncoder(), |
|
64
|
|
|
]; |
|
65
|
|
|
} else { |
|
66
|
|
|
$this->encoders = []; |
|
67
|
|
|
array_map([$this, 'addEncoder'], $encoders); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
array_map([$this, 'setOption'], array_keys($options), $options); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds a new encoder. |
|
75
|
|
|
* |
|
76
|
|
|
* Values are always encoded by the first encoder that supports encoding |
|
77
|
|
|
* that type of value. By setting the second optional parameter to true, |
|
78
|
|
|
* you can prepend the encoder to the list to ensure that it will be tested |
|
79
|
|
|
* first. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Encoder\Encoder $encoder Encoder for encoding values |
|
82
|
|
|
* @param bool $prepend True to prepend the encoder to the list, false to add it as last |
|
83
|
|
|
*/ |
|
84
|
|
|
public function addEncoder(Encoder\Encoder $encoder, $prepend = false) |
|
85
|
|
|
{ |
|
86
|
|
|
$prepend ? array_unshift($this->encoders, $encoder) : array_push($this->encoders, $encoder); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Sets the value for an encoder option. |
|
91
|
|
|
* @param string $option Name of the option |
|
92
|
|
|
* @param mixed $value Value for the option |
|
93
|
|
|
* @throws InvalidOptionException If the provided encoder option is invalid |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setOption($option, $value) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->isValidOption($option)) { |
|
98
|
|
|
throw new InvalidOptionException(sprintf("Invalid encoder option '%s'", $option)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->options[$option] = $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Tells if the given string is a valid option name. |
|
106
|
|
|
* @param string $option Option name to validate |
|
107
|
|
|
* @return bool True if the name is a valid option name, false if not |
|
108
|
|
|
*/ |
|
109
|
|
|
private function isValidOption($option) |
|
110
|
|
|
{ |
|
111
|
|
|
if (\array_key_exists($option, $this->options)) { |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
foreach ($this->encoders as $encoder) { |
|
116
|
|
|
if (\array_key_exists($option, $encoder->getDefaultOptions())) { |
|
117
|
|
|
return true; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Generates the PHP code for the given value. |
|
126
|
|
|
* @param mixed $variable Value to encode as PHP |
|
127
|
|
|
* @param array $options List of encoder options |
|
128
|
|
|
* @return string The PHP code that represents the given value |
|
129
|
|
|
* @throws InvalidOptionException If any of the encoder options are invalid |
|
130
|
|
|
* @throws \InvalidArgumentException If the provided value contains an unsupported value type |
|
131
|
|
|
* @throws \RuntimeException If max depth is reached or a recursive value is detected |
|
132
|
|
|
*/ |
|
133
|
|
|
public function encode($variable, array $options = []) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->generate($variable, 0, $this->getAllOptions($options)); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns a list of all encoder options. |
|
140
|
|
|
* @param array $overrides Options to override in the returned array |
|
141
|
|
|
* @return array List of encoder options |
|
142
|
|
|
* @throws InvalidOptionException If any of the encoder option overrides are invalid |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getAllOptions(array $overrides = []) |
|
145
|
|
|
{ |
|
146
|
|
|
$options = $this->options; |
|
147
|
|
|
|
|
148
|
|
|
foreach ($this->encoders as $encoder) { |
|
149
|
|
|
$options += $encoder->getDefaultOptions(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
foreach ($overrides as $name => $value) { |
|
153
|
|
|
if (!\array_key_exists($name, $options)) { |
|
154
|
|
|
throw new InvalidOptionException(sprintf("Invalid encoder option '%s'", $name)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$options[$name] = $value; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
ksort($options); |
|
161
|
|
|
|
|
162
|
|
|
return $options; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Generates the code for the given value recursively. |
|
167
|
|
|
* @param mixed $value Value to encode |
|
168
|
|
|
* @param int $depth Current indentation depth of the output |
|
169
|
|
|
* @param array $options List of encoder options |
|
170
|
|
|
* @param array $recursion Previously encoded values for recursion detection |
|
171
|
|
|
* @return string The PHP code that represents the given value |
|
172
|
|
|
* @throws \RuntimeException If max depth is reached or a recursive value is detected |
|
173
|
|
|
*/ |
|
174
|
|
|
private function generate($value, $depth, array $options, array $recursion = []) |
|
175
|
|
|
{ |
|
176
|
|
|
if ($this->detectRecursion($value, $options, $recursion)) { |
|
177
|
|
|
$recursion[] = $value; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if ($options['recursion.max'] !== false && $depth > (int) $options['recursion.max']) { |
|
181
|
|
|
throw new \RuntimeException('Maximum encoding depth reached'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$callback = function ($value, $level = 0, array $overrides = []) use ($depth, $options, $recursion) { |
|
185
|
|
|
return $this->generate($value, $depth + (int) $level, $overrides + $options, $recursion); |
|
186
|
|
|
}; |
|
187
|
|
|
|
|
188
|
|
|
return $this->encodeValue($value, $depth, $options, $callback); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Attempts to detect circular references in values. |
|
193
|
|
|
* @param mixed $value Value to try for circular reference |
|
194
|
|
|
* @param array $options List of encoder options |
|
195
|
|
|
* @param array $recursion Upper values in the encoding tree |
|
196
|
|
|
* @return bool True if values should be recorded, false if not |
|
197
|
|
|
* @throws \RuntimeException If a recursive value is detected |
|
198
|
|
|
*/ |
|
199
|
|
|
private function detectRecursion(&$value, array $options, array $recursion) |
|
200
|
|
|
{ |
|
201
|
|
|
if ($options['recursion.detect']) { |
|
202
|
|
|
if (array_search($value, $recursion, true) !== false) { |
|
203
|
|
|
if ($options['recursion.ignore']) { |
|
204
|
|
|
$value = null; |
|
205
|
|
|
} else { |
|
206
|
|
|
throw new \RuntimeException('A recursive value was detected'); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Encodes the value using one of the encoders that supports the value type. |
|
218
|
|
|
* @param mixed $value Value to encode |
|
219
|
|
|
* @param int $depth Current indentation depth of the output |
|
220
|
|
|
* @param array $options List of encoder options |
|
221
|
|
|
* @param callable $encode Callback used to encode values |
|
222
|
|
|
* @return string The PHP code that represents the given value |
|
223
|
|
|
* @throws \InvalidArgumentException If the provided value contains an unsupported value type |
|
224
|
|
|
*/ |
|
225
|
|
|
private function encodeValue($value, $depth, array $options, callable $encode) |
|
226
|
|
|
{ |
|
227
|
|
|
foreach ($this->encoders as $encoder) { |
|
228
|
|
|
if ($encoder->supports($value)) { |
|
229
|
|
|
return $encoder->encode($value, $depth, $options, $encode); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
throw new \InvalidArgumentException(sprintf("Unsupported value type '%s'", \gettype($value))); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|