Updated Symfony\Yaml to version 6.2.5

This commit is contained in:
Netkas 2023-02-24 22:18:34 -05:00
parent d272fe74a9
commit 40497db58c
8 changed files with 97 additions and 39 deletions

View file

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated `Symfony\polyfill-uuid` to version 1.27.0
- Updated `Symfony\Process` to version 6.2.5
- Updated `Symfony\Uid` to version 6.2.5
- Updated `Symfony\Yaml` to version 6.2.5
## [1.0.1] - 2023-02-07

View file

@ -1,6 +1,12 @@
CHANGELOG
=========
6.2
---
* Add support for `!php/enum` and `!php/enum *->value`
* Deprecate the `!php/const:` tag in key which will be replaced by the `!php/const` tag (without the colon) since 3.4
6.1
---

View file

@ -50,9 +50,6 @@ class LintCommand extends Command
$this->isReadableProvider = null === $isReadableProvider ? null : $isReadableProvider(...);
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this

View file

@ -56,6 +56,8 @@ class Dumper
if ($inline <= 0 || (!\is_array($input) && !$input instanceof TaggedValue && $dumpObjectAsInlineMap) || empty($input)) {
$output .= $prefix.Inline::dump($input, $flags);
} elseif ($input instanceof TaggedValue) {
$output .= $this->dumpTaggedValue($input, $inline, $indent, $flags, $prefix);
} else {
$dumpAsMap = Inline::isHash($input);
@ -135,4 +137,28 @@ class Dumper
return $output;
}
private function dumpTaggedValue(TaggedValue $value, int $inline, int $indent, int $flags, string $prefix): string
{
$output = sprintf('%s!%s', $prefix ? $prefix.' ' : '', $value->getTag());
if (Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value->getValue()) && false !== strpos($value->getValue(), "\n") && false === strpos($value->getValue(), "\r\n")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(' |%s', $blockIndentationIndicator);
foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
}
return $output;
}
if ($inline - 1 <= 0 || null === $value->getValue() || \is_scalar($value->getValue())) {
return $output.' '.$this->dump($value->getValue(), $inline - 1, 0, $flags)."\n";
}
return $output."\n".$this->dump($value->getValue(), $inline - 1, $indent, $flags);
}
}

View file

@ -50,7 +50,7 @@ class Inline
/**
* Converts a YAML string to a PHP value.
*
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
* @param int $flags A bit field of Yaml::PARSE_* constants to customize the YAML parser behavior
* @param array $references Mapping of variable names to values
*
* @throws ParseException
@ -77,7 +77,7 @@ class Inline
++$i;
break;
default:
$result = self::parseScalar($value, $flags, null, $i, null === $tag, $references);
$result = self::parseScalar($value, $flags, null, $i, true, $references);
}
// some comments are allowed at the end
@ -112,7 +112,7 @@ class Inline
case $value instanceof \DateTimeInterface:
return $value->format('c');
case $value instanceof \UnitEnum:
return sprintf('!php/const %s::%s', \get_class($value), $value->name);
return sprintf('!php/const %s::%s', $value::class, $value->name);
case \is_object($value):
if ($value instanceof TaggedValue) {
return '!'.$value->getTag().' '.self::dump($value->getValue(), $flags);
@ -123,13 +123,7 @@ class Inline
}
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
$output = [];
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
}
return sprintf('{ %s }', implode(', ', $output));
return self::dumpHashArray($value, $flags);
}
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@ -232,7 +226,17 @@ class Inline
return sprintf('[%s]', implode(', ', $output));
}
// hash
return self::dumpHashArray($value, $flags);
}
/**
* Dumps hash array to a YAML string.
*
* @param array|\ArrayObject|\stdClass $value The hash array to dump
* @param int $flags A bit field of Yaml::DUMP_* constants to customize the dumped YAML string
*/
private static function dumpHashArray(array|\ArrayObject|\stdClass $value, int $flags): string
{
$output = [];
foreach ($value as $key => $val) {
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));
@ -432,7 +436,7 @@ class Inline
throw new ParseException('Missing mapping key.', self::$parsedLineNumber + 1, $mapping);
}
if ('!php/const' === $key) {
if ('!php/const' === $key || '!php/enum' === $key) {
$key .= ' '.self::parseScalar($mapping, $flags, [':'], $i, false);
$key = self::evaluateScalar($key, $flags);
}
@ -623,6 +627,40 @@ class Inline
throw new ParseException(sprintf('The string "%s" could not be parsed as a constant. Did you forget to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
return null;
case str_starts_with($scalar, '!php/enum'):
if (self::$constantSupport) {
if (!isset($scalar[11])) {
throw new ParseException('Missing value for tag "!php/enum".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
$i = 0;
$enum = self::parseScalar(substr($scalar, 10), 0, null, $i, false);
if ($useValue = str_ends_with($enum, '->value')) {
$enum = substr($enum, 0, -7);
}
if (!\defined($enum)) {
throw new ParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
$value = \constant($enum);
if (!$value instanceof \UnitEnum) {
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
if (!$useValue) {
return $value;
}
if (!$value instanceof \BackedEnum) {
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
return $value->value;
}
if (self::$exceptionOnInvalidType) {
throw new ParseException(sprintf('The string "%s" could not be parsed as an enum. Did you forget to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
return null;
case str_starts_with($scalar, '!!float '):
return (float) substr($scalar, 8);
@ -639,7 +677,6 @@ class Inline
}
return octdec($value);
// Optimize for returning strings.
case \in_array($scalar[0], ['+', '-', '.'], true) || is_numeric($scalar[0]):
if (Parser::preg_match('{^[+-]?[0-9][0-9_]*$}', $scalar)) {
$scalar = str_replace('_', '', $scalar);
@ -665,7 +702,7 @@ class Inline
return (float) str_replace('_', '', $scalar);
case Parser::preg_match(self::getTimestampRegex(), $scalar):
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
$time = new \DateTimeImmutable($scalar, new \DateTimeZone('UTC'));
if (Yaml::PARSE_DATETIME & $flags) {
return $time;
@ -703,7 +740,7 @@ class Inline
}
// Is followed by a scalar and is a built-in tag
if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || 'str' === $tag || 'php/const' === $tag || 'php/object' === $tag)) {
if ('' !== $tag && (!isset($value[$nextOffset]) || !\in_array($value[$nextOffset], ['[', '{'], true)) && ('!' === $tag[0] || \in_array($tag, ['str', 'php/const', 'php/enum', 'php/object'], true))) {
// Manage in {@link self::evaluateScalar()}
return null;
}

View file

@ -1,4 +1,4 @@
Copyright (c) 2004-2022 Fabien Potencier
Copyright (c) 2004-2023 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -43,7 +43,7 @@ class Parser
* Parses a YAML file into a PHP value.
*
* @param string $filename The path to the YAML file to be parsed
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
* @param int $flags A bit field of Yaml::PARSE_* constants to customize the YAML parser behavior
*
* @throws ParseException If the file could not be read or the YAML is not valid
*/
@ -70,7 +70,7 @@ class Parser
* Parses a YAML string to a PHP value.
*
* @param string $value A YAML string
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
* @param int $flags A bit field of Yaml::PARSE_* constants to customize the YAML parser behavior
*
* @throws ParseException If the YAML is not valid
*/
@ -107,10 +107,7 @@ class Parser
$this->lines = explode("\n", $value);
$this->numberOfParsedLines = \count($this->lines);
$this->locallySkippedLineNumbers = [];
if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = $this->numberOfParsedLines;
}
$this->totalNumberOfLines ??= $this->numberOfParsedLines;
if (!$this->moveToNextLine()) {
return null;
@ -201,9 +198,14 @@ class Parser
array_pop($this->refsBeingParsed);
}
} elseif (
// @todo in 7.0 remove legacy "(?:!?!php/const:)?"
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(( |\t)++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
&& (!str_contains($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
) {
if (str_starts_with($values['key'], '!php/const:')) {
trigger_deprecation('symfony/yaml', '6.2', 'YAML syntax for key "%s" is deprecated and replaced by "!php/const %s".', $values['key'], substr($values['key'], 11));
}
if ($context && 'sequence' == $context) {
throw new ParseException('You cannot define a mapping item when in a sequence.', $this->currentLineNb + 1, $this->currentLine, $this->filename);
}
@ -694,7 +696,7 @@ class Parser
* Parses a YAML value.
*
* @param string $value A YAML value
* @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
* @param int $flags A bit field of Yaml::PARSE_* constants to customize the YAML parser behavior
* @param string $context The parser context (either sequence or mapping)
*
* @throws ParseException When reference does not exist
@ -1036,23 +1038,12 @@ class Parser
*
* @throws ParseException on a PCRE internal error
*
* @see preg_last_error()
*
* @internal
*/
public static function preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int
{
if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
$error = match (preg_last_error()) {
\PREG_INTERNAL_ERROR => 'Internal PCRE error.',
\PREG_BACKTRACK_LIMIT_ERROR => 'pcre.backtrack_limit reached.',
\PREG_RECURSION_LIMIT_ERROR => 'pcre.recursion_limit reached.',
\PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data.',
\PREG_BAD_UTF8_OFFSET_ERROR => 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.',
default => 'Error.',
};
throw new ParseException($error);
throw new ParseException(preg_last_error_msg());
}
return $ret;

View file

@ -1 +1 @@
6.1.3
6.2.5