Skip to content

Commit bd25351

Browse files
committed
fix Yaml parser for PHP 8.1
with code from FriendsOfSymfony1/symfony1#266 to bring Propel sfYaml*.php files in sync
1 parent 336461a commit bd25351

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

runtime/lib/parser/yaml/sfYamlInline.php

+26-14
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ public static function load($value)
3333
{
3434
$value = trim($value);
3535

36-
if (0 == strlen($value)) {
36+
if ('' === $value) {
3737
return '';
3838
}
3939

40-
$mbEncoding = mb_internal_encoding();
41-
mb_internal_encoding('ASCII');
40+
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
41+
$mbEncoding = mb_internal_encoding();
42+
mb_internal_encoding('ASCII');
43+
}
4244

4345
switch ($value[0]) {
4446
case '[':
@@ -77,7 +79,8 @@ public static function dump($value)
7779

7880
switch (true) {
7981
case is_resource($value):
80-
throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
82+
return stream_get_contents($value);
83+
// throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
8184
case is_object($value):
8285
return '!!php/object:'.serialize($value);
8386
case is_array($value):
@@ -88,10 +91,10 @@ public static function dump($value)
8891
return 'true';
8992
case false === $value:
9093
return 'false';
91-
case ctype_digit($value):
94+
case (is_string($value) && ctype_digit($value)):
9295
return is_string($value) ? "'$value'" : (int) $value;
93-
case is_numeric($value):
94-
return is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : (is_string($value) ? "'$value'" : $value);
96+
case is_numeric($value) && false === strpbrk($value, "\f\n\r\t\v"):
97+
return is_infinite($value) ? str_ireplace('INF', '.Inf', (string) $value) : (is_string($value) ? "'$value'" : $value);
9598
case false !== strpos($value, "\n") || false !== strpos($value, "\r"):
9699
return sprintf('"%s"', str_replace(array('"', "\n", "\r"), array('\\"', '\n', '\r'), $value));
97100
case preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x', $value):
@@ -122,8 +125,11 @@ protected static function dumpArray($value)
122125
{
123126
// array
124127
$keys = array_keys($value);
125-
126-
if (count($value) > 0 && array_values($value) === $value)
128+
129+
if (
130+
(1 == count($keys) && '0' == $keys[0])
131+
||
132+
(count($keys) > 1 && array_sum(array_map('intval', $keys)) == count($keys) * (count($keys) - 1) / 2))
127133
{
128134
$output = array();
129135
foreach ($value as $val) {
@@ -353,27 +359,33 @@ protected static function evaluateScalar($scalar)
353359
case 0 === strpos($scalar, '!str'):
354360
return (string) substr($scalar, 5);
355361
case 0 === strpos($scalar, '! '):
356-
return intval(self::parseScalar(substr($scalar, 2)));
362+
return (int) self::parseScalar(substr($scalar, 2));
357363
case 0 === strpos($scalar, '!!php/object:'):
358364
return unserialize(substr($scalar, 13));
359365
case ctype_digit($scalar):
360366
$raw = $scalar;
361-
$cast = intval($scalar);
367+
$cast = (int) $scalar;
362368

363369
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
364370
case in_array(strtolower($scalar), $trueValues):
365371
return true;
366372
case in_array(strtolower($scalar), $falseValues):
367373
return false;
374+
case 0 === strpos($scalar, '0x'):
375+
return hexdec($scalar);
368376
case is_numeric($scalar):
369-
return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
377+
return floatval($scalar);
370378
case 0 == strcasecmp($scalar, '.inf'):
371379
case 0 == strcasecmp($scalar, '.NaN'):
372380
return -log(0);
373381
case 0 == strcasecmp($scalar, '-.inf'):
374382
return log(0);
375-
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
376-
return floatval(str_replace(',', '', $scalar));
383+
case preg_match('/^(-|\+)?[0-9,]+(\.\d+)?$/', $scalar):
384+
$replaced = str_replace(',', '', $scalar);
385+
$replaced = str_replace('+', '', $replaced);
386+
$floatval = floatval($replaced);
387+
$intval = intval($replaced);
388+
return $floatval == $intval ? $intval : $floatval;
377389
case preg_match(self::getTimestampRegex(), $scalar):
378390
return strtotime($scalar);
379391
default:

runtime/lib/parser/yaml/sfYamlParser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ protected function parseFoldedScalar($separator, $indicator = '', $indentation =
371371
$this->moveToNextLine();
372372

373373
if (preg_match('#^(?P<indent> {'.strlen($textIndent).',})(?P<text>.+)$#u', $this->currentLine, $matches)) {
374-
if (' ' == $separator && $previousIndent != $matches['indent']) {
374+
if (' ' == $separator && ($previousIndent && $previousIndent != $matches['indent'])) {
375375
$text = substr($text, 0, -1)."\n";
376376
}
377377
$previousIndent = $matches['indent'];

0 commit comments

Comments
 (0)