Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Yaml; Chris@0: Chris@0: use Symfony\Component\Yaml\Exception\ParseException; Chris@14: use Symfony\Component\Yaml\Tag\TaggedValue; Chris@0: Chris@0: /** Chris@0: * Parser parses YAML strings to convert them to PHP arrays. Chris@0: * Chris@0: * @author Fabien Potencier Chris@14: * Chris@14: * @final since version 3.4 Chris@0: */ Chris@0: class Parser Chris@0: { Chris@14: const TAG_PATTERN = '(?P![\w!.\/:-]+)'; Chris@0: const BLOCK_SCALAR_HEADER_PATTERN = '(?P\||>)(?P\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P +#.*)?'; Chris@0: Chris@14: private $filename; Chris@0: private $offset = 0; Chris@0: private $totalNumberOfLines; Chris@17: private $lines = []; Chris@0: private $currentLineNb = -1; Chris@0: private $currentLine = ''; Chris@17: private $refs = []; Chris@17: private $skippedLineNumbers = []; Chris@17: private $locallySkippedLineNumbers = []; Chris@17: private $refsBeingParsed = []; Chris@0: Chris@14: public function __construct() Chris@14: { Chris@17: if (\func_num_args() > 0) { Chris@14: @trigger_error(sprintf('The constructor arguments $offset, $totalNumberOfLines, $skippedLineNumbers of %s are deprecated and will be removed in 4.0', self::class), E_USER_DEPRECATED); Chris@14: Chris@14: $this->offset = func_get_arg(0); Chris@17: if (\func_num_args() > 1) { Chris@14: $this->totalNumberOfLines = func_get_arg(1); Chris@14: } Chris@17: if (\func_num_args() > 2) { Chris@14: $this->skippedLineNumbers = func_get_arg(2); Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@0: /** Chris@14: * Parses a YAML file into a PHP value. Chris@0: * Chris@14: * @param string $filename The path to the YAML file to be parsed Chris@14: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@14: * Chris@14: * @return mixed The YAML converted to a PHP value Chris@14: * Chris@14: * @throws ParseException If the file could not be read or the YAML is not valid Chris@0: */ Chris@14: public function parseFile($filename, $flags = 0) Chris@0: { Chris@14: if (!is_file($filename)) { Chris@14: throw new ParseException(sprintf('File "%s" does not exist.', $filename)); Chris@14: } Chris@14: Chris@14: if (!is_readable($filename)) { Chris@14: throw new ParseException(sprintf('File "%s" cannot be read.', $filename)); Chris@14: } Chris@14: Chris@14: $this->filename = $filename; Chris@14: Chris@14: try { Chris@14: return $this->parse(file_get_contents($filename), $flags); Chris@14: } finally { Chris@14: $this->filename = null; Chris@14: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML string to a PHP value. Chris@0: * Chris@0: * @param string $value A YAML string Chris@0: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@0: * Chris@0: * @return mixed A PHP value Chris@0: * Chris@0: * @throws ParseException If the YAML is not valid Chris@0: */ Chris@0: public function parse($value, $flags = 0) Chris@0: { Chris@17: if (\is_bool($flags)) { Chris@14: @trigger_error('Passing a boolean flag to toggle exception handling is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if ($flags) { Chris@0: $flags = Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE; Chris@0: } else { Chris@0: $flags = 0; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 3) { Chris@14: @trigger_error('Passing a boolean flag to toggle object support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(2)) { Chris@0: $flags |= Yaml::PARSE_OBJECT; Chris@0: } Chris@0: } Chris@0: Chris@17: if (\func_num_args() >= 4) { Chris@14: @trigger_error('Passing a boolean flag to toggle object for map support is deprecated since Symfony 3.1 and will be removed in 4.0. Use the Yaml::PARSE_OBJECT_FOR_MAP flag instead.', E_USER_DEPRECATED); Chris@0: Chris@0: if (func_get_arg(3)) { Chris@0: $flags |= Yaml::PARSE_OBJECT_FOR_MAP; Chris@0: } Chris@0: } Chris@0: Chris@14: if (Yaml::PARSE_KEYS_AS_STRINGS & $flags) { Chris@14: @trigger_error('Using the Yaml::PARSE_KEYS_AS_STRINGS flag is deprecated since Symfony 3.4 as it will be removed in 4.0. Quote your keys when they are evaluable instead.', E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@0: if (false === preg_match('//u', $value)) { Chris@14: throw new ParseException('The YAML value does not appear to be valid UTF-8.', -1, null, $this->filename); Chris@0: } Chris@0: Chris@17: $this->refs = []; Chris@0: Chris@0: $mbEncoding = null; Chris@0: $e = null; Chris@0: $data = null; Chris@0: Chris@0: if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) { Chris@0: $mbEncoding = mb_internal_encoding(); Chris@0: mb_internal_encoding('UTF-8'); Chris@0: } Chris@0: Chris@0: try { Chris@0: $data = $this->doParse($value, $flags); Chris@0: } catch (\Exception $e) { Chris@0: } catch (\Throwable $e) { Chris@0: } Chris@0: Chris@0: if (null !== $mbEncoding) { Chris@0: mb_internal_encoding($mbEncoding); Chris@0: } Chris@0: Chris@17: $this->lines = []; Chris@0: $this->currentLine = ''; Chris@17: $this->refs = []; Chris@17: $this->skippedLineNumbers = []; Chris@17: $this->locallySkippedLineNumbers = []; Chris@0: Chris@0: if (null !== $e) { Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: private function doParse($value, $flags) Chris@0: { Chris@0: $this->currentLineNb = -1; Chris@0: $this->currentLine = ''; Chris@0: $value = $this->cleanup($value); Chris@0: $this->lines = explode("\n", $value); Chris@17: $this->locallySkippedLineNumbers = []; Chris@0: Chris@0: if (null === $this->totalNumberOfLines) { Chris@17: $this->totalNumberOfLines = \count($this->lines); Chris@0: } Chris@0: Chris@14: if (!$this->moveToNextLine()) { Chris@14: return null; Chris@14: } Chris@14: Chris@17: $data = []; Chris@0: $context = null; Chris@0: $allowOverwrite = false; Chris@0: Chris@14: while ($this->isCurrentLineEmpty()) { Chris@14: if (!$this->moveToNextLine()) { Chris@14: return null; Chris@14: } Chris@14: } Chris@14: Chris@14: // Resolves the tag and returns if end of the document Chris@14: if (null !== ($tag = $this->getLineTag($this->currentLine, $flags, false)) && !$this->moveToNextLine()) { Chris@14: return new TaggedValue($tag, ''); Chris@14: } Chris@14: Chris@14: do { Chris@0: if ($this->isCurrentLineEmpty()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: // tab? Chris@0: if ("\t" === $this->currentLine[0]) { Chris@14: throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: Chris@14: Inline::initialize($flags, $this->getRealCurrentLineNb(), $this->filename); Chris@14: Chris@0: $isRef = $mergeNode = false; Chris@0: if (self::preg_match('#^\-((?P\s+)(?P.+))?$#u', rtrim($this->currentLine), $values)) { Chris@0: if ($context && 'mapping' == $context) { Chris@14: throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: $context = 'sequence'; Chris@0: Chris@0: if (isset($values['value']) && self::preg_match('#^&(?P[^ ]+) *(?P.*)#u', $values['value'], $matches)) { Chris@0: $isRef = $matches['ref']; Chris@17: $this->refsBeingParsed[] = $isRef; Chris@0: $values['value'] = $matches['value']; Chris@0: } Chris@0: Chris@14: if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) { Chris@14: @trigger_error($this->getDeprecationMessage('Starting an unquoted string with a question mark followed by a space is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.'), E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@0: // array Chris@0: if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) { Chris@0: $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags); Chris@14: } elseif (null !== $subTag = $this->getLineTag(ltrim($values['value'], ' '), $flags)) { Chris@14: $data[] = new TaggedValue( Chris@14: $subTag, Chris@14: $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $flags) Chris@14: ); Chris@0: } else { Chris@0: if (isset($values['leadspaces']) Chris@14: && self::preg_match('#^(?P'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P.+?))?\s*$#u', $this->trimTag($values['value']), $matches) Chris@0: ) { Chris@0: // this is a compact notation element, add to next block and parse Chris@0: $block = $values['value']; Chris@0: if ($this->isNextLineIndented()) { Chris@17: $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + \strlen($values['leadspaces']) + 1); Chris@0: } Chris@0: Chris@0: $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $flags); Chris@0: } else { Chris@0: $data[] = $this->parseValue($values['value'], $flags, $context); Chris@0: } Chris@0: } Chris@0: if ($isRef) { Chris@0: $this->refs[$isRef] = end($data); Chris@17: array_pop($this->refsBeingParsed); Chris@0: } Chris@0: } elseif ( Chris@14: self::preg_match('#^(?P(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P.+))?$#u', rtrim($this->currentLine), $values) Chris@17: && (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"])) Chris@0: ) { Chris@0: if ($context && 'sequence' == $context) { Chris@14: throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: $context = 'mapping'; Chris@0: Chris@0: try { Chris@14: $i = 0; Chris@14: $evaluateKey = !(Yaml::PARSE_KEYS_AS_STRINGS & $flags); Chris@14: Chris@14: // constants in key will be evaluated anyway Chris@14: if (isset($values['key'][0]) && '!' === $values['key'][0] && Yaml::PARSE_CONSTANT & $flags) { Chris@14: $evaluateKey = true; Chris@14: } Chris@14: Chris@14: $key = Inline::parseScalar($values['key'], 0, null, $i, $evaluateKey); Chris@0: } catch (ParseException $e) { Chris@0: $e->setParsedLine($this->getRealCurrentLineNb() + 1); Chris@0: $e->setSnippet($this->currentLine); Chris@0: Chris@0: throw $e; Chris@0: } Chris@0: Chris@17: if (!\is_string($key) && !\is_int($key)) { Chris@14: $keyType = is_numeric($key) ? 'numeric key' : 'non-string key'; Chris@14: @trigger_error($this->getDeprecationMessage(sprintf('Implicit casting of %s to string is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType)), E_USER_DEPRECATED); Chris@14: } Chris@14: Chris@0: // Convert float keys to strings, to avoid being converted to integers by PHP Chris@17: if (\is_float($key)) { Chris@0: $key = (string) $key; Chris@0: } Chris@0: Chris@14: if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P[^ ]+)#u', $values['value'], $refMatches))) { Chris@0: $mergeNode = true; Chris@0: $allowOverwrite = true; Chris@14: if (isset($values['value'][0]) && '*' === $values['value'][0]) { Chris@14: $refName = substr(rtrim($values['value']), 1); Chris@18: if (!\array_key_exists($refName, $this->refs)) { Chris@17: if (false !== $pos = array_search($refName, $this->refsBeingParsed, true)) { Chris@17: throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $refName, $refName), $this->currentLineNb + 1, $this->currentLine, $this->filename); Chris@17: } Chris@17: Chris@14: throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: Chris@0: $refValue = $this->refs[$refName]; Chris@0: Chris@14: if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $refValue instanceof \stdClass) { Chris@14: $refValue = (array) $refValue; Chris@14: } Chris@14: Chris@17: if (!\is_array($refValue)) { Chris@14: throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: Chris@0: $data += $refValue; // array union Chris@0: } else { Chris@14: if (isset($values['value']) && '' !== $values['value']) { Chris@0: $value = $values['value']; Chris@0: } else { Chris@0: $value = $this->getNextEmbedBlock(); Chris@0: } Chris@0: $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags); Chris@0: Chris@14: if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsed instanceof \stdClass) { Chris@14: $parsed = (array) $parsed; Chris@14: } Chris@14: Chris@17: if (!\is_array($parsed)) { Chris@14: throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: Chris@0: if (isset($parsed[0])) { Chris@0: // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes Chris@0: // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier Chris@0: // in the sequence override keys specified in later mapping nodes. Chris@0: foreach ($parsed as $parsedItem) { Chris@14: if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $parsedItem instanceof \stdClass) { Chris@14: $parsedItem = (array) $parsedItem; Chris@14: } Chris@14: Chris@17: if (!\is_array($parsedItem)) { Chris@14: throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem, $this->filename); Chris@0: } Chris@0: Chris@0: $data += $parsedItem; // array union Chris@0: } Chris@0: } else { Chris@0: // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the Chris@0: // current mapping, unless the key already exists in it. Chris@0: $data += $parsed; // array union Chris@0: } Chris@0: } Chris@14: } elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P[^ ]++) *+(?P.*)#u', $values['value'], $matches)) { Chris@0: $isRef = $matches['ref']; Chris@17: $this->refsBeingParsed[] = $isRef; Chris@0: $values['value'] = $matches['value']; Chris@0: } Chris@0: Chris@14: $subTag = null; Chris@0: if ($mergeNode) { Chris@0: // Merge keys Chris@14: } elseif (!isset($values['value']) || '' === $values['value'] || 0 === strpos($values['value'], '#') || (null !== $subTag = $this->getLineTag($values['value'], $flags)) || '<<' === $key) { Chris@0: // hash Chris@0: // if next line is less indented or equal, then it means that the current value is null Chris@0: if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) { Chris@0: // Spec: Keys MUST be unique; first one wins. Chris@0: // But overwriting is allowed when a merge node is used in current block. Chris@0: if ($allowOverwrite || !isset($data[$key])) { Chris@14: if (null !== $subTag) { Chris@14: $data[$key] = new TaggedValue($subTag, ''); Chris@14: } else { Chris@14: $data[$key] = null; Chris@14: } Chris@0: } else { Chris@14: @trigger_error($this->getDeprecationMessage(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since Symfony 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key)), E_USER_DEPRECATED); Chris@0: } Chris@0: } else { Chris@0: $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags); Chris@14: if ('<<' === $key) { Chris@14: $this->refs[$refMatches['ref']] = $value; Chris@14: Chris@14: if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && $value instanceof \stdClass) { Chris@14: $value = (array) $value; Chris@14: } Chris@14: Chris@14: $data += $value; Chris@14: } elseif ($allowOverwrite || !isset($data[$key])) { Chris@14: // Spec: Keys MUST be unique; first one wins. Chris@14: // But overwriting is allowed when a merge node is used in current block. Chris@14: if (null !== $subTag) { Chris@14: $data[$key] = new TaggedValue($subTag, $value); Chris@14: } else { Chris@14: $data[$key] = $value; Chris@14: } Chris@0: } else { Chris@14: @trigger_error($this->getDeprecationMessage(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since Symfony 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key)), E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: } else { Chris@14: $value = $this->parseValue(rtrim($values['value']), $flags, $context); Chris@0: // Spec: Keys MUST be unique; first one wins. Chris@0: // But overwriting is allowed when a merge node is used in current block. Chris@0: if ($allowOverwrite || !isset($data[$key])) { Chris@0: $data[$key] = $value; Chris@0: } else { Chris@14: @trigger_error($this->getDeprecationMessage(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since Symfony 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key)), E_USER_DEPRECATED); Chris@0: } Chris@0: } Chris@0: if ($isRef) { Chris@0: $this->refs[$isRef] = $data[$key]; Chris@17: array_pop($this->refsBeingParsed); Chris@0: } Chris@0: } else { Chris@0: // multiple documents are not supported Chris@0: if ('---' === $this->currentLine) { Chris@14: throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine, $this->filename); Chris@14: } Chris@14: Chris@14: if ($deprecatedUsage = (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1])) { Chris@14: @trigger_error($this->getDeprecationMessage('Starting an unquoted string with a question mark followed by a space is deprecated since Symfony 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.'), E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: // 1-liner optionally followed by newline(s) Chris@17: if (\is_string($value) && $this->lines[0] === trim($value)) { Chris@0: try { Chris@0: $value = Inline::parse($this->lines[0], $flags, $this->refs); Chris@0: } catch (ParseException $e) { Chris@0: $e->setParsedLine($this->getRealCurrentLineNb() + 1); Chris@0: $e->setSnippet($this->currentLine); Chris@0: Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@14: // try to parse the value as a multi-line string as a last resort Chris@14: if (0 === $this->currentLineNb) { Chris@14: $previousLineWasNewline = false; Chris@14: $previousLineWasTerminatedWithBackslash = false; Chris@14: $value = ''; Chris@14: Chris@14: foreach ($this->lines as $line) { Chris@14: // If the indentation is not consistent at offset 0, it is to be considered as a ParseError Chris@14: if (0 === $this->offset && !$deprecatedUsage && isset($line[0]) && ' ' === $line[0]) { Chris@14: throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@14: } Chris@14: if ('' === trim($line)) { Chris@14: $value .= "\n"; Chris@14: } elseif (!$previousLineWasNewline && !$previousLineWasTerminatedWithBackslash) { Chris@14: $value .= ' '; Chris@14: } Chris@14: Chris@14: if ('' !== trim($line) && '\\' === substr($line, -1)) { Chris@14: $value .= ltrim(substr($line, 0, -1)); Chris@14: } elseif ('' !== trim($line)) { Chris@14: $value .= trim($line); Chris@14: } Chris@14: Chris@14: if ('' === trim($line)) { Chris@14: $previousLineWasNewline = true; Chris@14: $previousLineWasTerminatedWithBackslash = false; Chris@14: } elseif ('\\' === substr($line, -1)) { Chris@14: $previousLineWasNewline = false; Chris@14: $previousLineWasTerminatedWithBackslash = true; Chris@14: } else { Chris@14: $previousLineWasNewline = false; Chris@14: $previousLineWasTerminatedWithBackslash = false; Chris@14: } Chris@14: } Chris@14: Chris@14: try { Chris@14: return Inline::parse(trim($value)); Chris@14: } catch (ParseException $e) { Chris@14: // fall-through to the ParseException thrown below Chris@14: } Chris@14: } Chris@14: Chris@14: throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@14: } while ($this->moveToNextLine()); Chris@14: Chris@14: if (null !== $tag) { Chris@14: $data = new TaggedValue($tag, $data); Chris@0: } Chris@0: Chris@17: if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !\is_object($data) && 'mapping' === $context) { Chris@0: $object = new \stdClass(); Chris@0: Chris@0: foreach ($data as $key => $value) { Chris@0: $object->$key = $value; Chris@0: } Chris@0: Chris@0: $data = $object; Chris@0: } Chris@0: Chris@0: return empty($data) ? null : $data; Chris@0: } Chris@0: Chris@0: private function parseBlock($offset, $yaml, $flags) Chris@0: { Chris@0: $skippedLineNumbers = $this->skippedLineNumbers; Chris@0: Chris@0: foreach ($this->locallySkippedLineNumbers as $lineNumber) { Chris@0: if ($lineNumber < $offset) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $skippedLineNumbers[] = $lineNumber; Chris@0: } Chris@0: Chris@14: $parser = new self(); Chris@14: $parser->offset = $offset; Chris@14: $parser->totalNumberOfLines = $this->totalNumberOfLines; Chris@14: $parser->skippedLineNumbers = $skippedLineNumbers; Chris@0: $parser->refs = &$this->refs; Chris@17: $parser->refsBeingParsed = $this->refsBeingParsed; Chris@0: Chris@0: return $parser->doParse($yaml, $flags); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current line number (takes the offset into account). Chris@0: * Chris@14: * @internal Chris@14: * Chris@0: * @return int The current line number Chris@0: */ Chris@14: public function getRealCurrentLineNb() Chris@0: { Chris@0: $realCurrentLineNumber = $this->currentLineNb + $this->offset; Chris@0: Chris@0: foreach ($this->skippedLineNumbers as $skippedLineNumber) { Chris@0: if ($skippedLineNumber > $realCurrentLineNumber) { Chris@0: break; Chris@0: } Chris@0: Chris@0: ++$realCurrentLineNumber; Chris@0: } Chris@0: Chris@0: return $realCurrentLineNumber; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the current line indentation. Chris@0: * Chris@0: * @return int The current line indentation Chris@0: */ Chris@0: private function getCurrentLineIndentation() Chris@0: { Chris@17: return \strlen($this->currentLine) - \strlen(ltrim($this->currentLine, ' ')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the next embed block of YAML. Chris@0: * Chris@0: * @param int $indentation The indent level at which the block is to be read, or null for default Chris@0: * @param bool $inSequence True if the enclosing data structure is a sequence Chris@0: * Chris@0: * @return string A YAML string Chris@0: * Chris@0: * @throws ParseException When indentation problem are detected Chris@0: */ Chris@0: private function getNextEmbedBlock($indentation = null, $inSequence = false) Chris@0: { Chris@0: $oldLineIndentation = $this->getCurrentLineIndentation(); Chris@0: Chris@0: if (!$this->moveToNextLine()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: if (null === $indentation) { Chris@14: $newIndent = null; Chris@14: $movements = 0; Chris@14: Chris@14: do { Chris@14: $EOF = false; Chris@14: Chris@14: // empty and comment-like lines do not influence the indentation depth Chris@14: if ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { Chris@14: $EOF = !$this->moveToNextLine(); Chris@14: Chris@14: if (!$EOF) { Chris@14: ++$movements; Chris@14: } Chris@14: } else { Chris@14: $newIndent = $this->getCurrentLineIndentation(); Chris@14: } Chris@14: } while (!$EOF && null === $newIndent); Chris@14: Chris@14: for ($i = 0; $i < $movements; ++$i) { Chris@14: $this->moveToPreviousLine(); Chris@14: } Chris@0: Chris@0: $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem(); Chris@0: Chris@0: if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) { Chris@14: throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: } else { Chris@0: $newIndent = $indentation; Chris@0: } Chris@0: Chris@17: $data = []; Chris@0: if ($this->getCurrentLineIndentation() >= $newIndent) { Chris@0: $data[] = substr($this->currentLine, $newIndent); Chris@14: } elseif ($this->isCurrentLineEmpty() || $this->isCurrentLineComment()) { Chris@14: $data[] = $this->currentLine; Chris@0: } else { Chris@0: $this->moveToPreviousLine(); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) { Chris@0: // the previous line contained a dash but no item content, this line is a sequence item with the same indentation Chris@0: // and therefore no nested list or mapping Chris@0: $this->moveToPreviousLine(); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem(); Chris@0: Chris@0: while ($this->moveToNextLine()) { Chris@0: $indent = $this->getCurrentLineIndentation(); Chris@0: Chris@0: if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) { Chris@0: $this->moveToPreviousLine(); Chris@0: break; Chris@0: } Chris@0: Chris@0: if ($this->isCurrentLineBlank()) { Chris@0: $data[] = substr($this->currentLine, $newIndent); Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ($indent >= $newIndent) { Chris@0: $data[] = substr($this->currentLine, $newIndent); Chris@14: } elseif ($this->isCurrentLineComment()) { Chris@14: $data[] = $this->currentLine; Chris@0: } elseif (0 == $indent) { Chris@0: $this->moveToPreviousLine(); Chris@0: Chris@0: break; Chris@0: } else { Chris@14: throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: } Chris@0: Chris@0: return implode("\n", $data); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Moves the parser to the next line. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function moveToNextLine() Chris@0: { Chris@17: if ($this->currentLineNb >= \count($this->lines) - 1) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $this->currentLine = $this->lines[++$this->currentLineNb]; Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Moves the parser to the previous line. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function moveToPreviousLine() Chris@0: { Chris@0: if ($this->currentLineNb < 1) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $this->currentLine = $this->lines[--$this->currentLineNb]; Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a YAML value. Chris@0: * Chris@0: * @param string $value A YAML value Chris@0: * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior Chris@0: * @param string $context The parser context (either sequence or mapping) Chris@0: * Chris@0: * @return mixed A PHP value Chris@0: * Chris@0: * @throws ParseException When reference does not exist Chris@0: */ Chris@0: private function parseValue($value, $flags, $context) Chris@0: { Chris@0: if (0 === strpos($value, '*')) { Chris@0: if (false !== $pos = strpos($value, '#')) { Chris@0: $value = substr($value, 1, $pos - 2); Chris@0: } else { Chris@0: $value = substr($value, 1); Chris@0: } Chris@0: Chris@18: if (!\array_key_exists($value, $this->refs)) { Chris@17: if (false !== $pos = array_search($value, $this->refsBeingParsed, true)) { Chris@17: throw new ParseException(sprintf('Circular reference [%s, %s] detected for reference "%s".', implode(', ', \array_slice($this->refsBeingParsed, $pos)), $value, $value), $this->currentLineNb + 1, $this->currentLine, $this->filename); Chris@17: } Chris@17: Chris@14: throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine, $this->filename); Chris@0: } Chris@0: Chris@0: return $this->refs[$value]; Chris@0: } Chris@0: Chris@14: if (self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) { Chris@0: $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; Chris@0: Chris@0: $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers)); Chris@0: Chris@14: if ('' !== $matches['tag']) { Chris@14: if ('!!binary' === $matches['tag']) { Chris@14: return Inline::evaluateBinaryScalar($data); Chris@14: } elseif ('tagged' === $matches['tag']) { Chris@14: return new TaggedValue(substr($matches['tag'], 1), $data); Chris@14: } elseif ('!' !== $matches['tag']) { Chris@14: @trigger_error($this->getDeprecationMessage(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since Symfony 3.3. It will be replaced by an instance of %s in 4.0.', $matches['tag'], $data, TaggedValue::class)), E_USER_DEPRECATED); Chris@14: } Chris@0: } Chris@0: Chris@0: return $data; Chris@0: } Chris@0: Chris@0: try { Chris@0: $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null; Chris@0: Chris@0: // do not take following lines into account when the current line is a quoted single line value Chris@14: if (null !== $quotation && self::preg_match('/^'.$quotation.'.*'.$quotation.'(\s*#.*)?$/', $value)) { Chris@0: return Inline::parse($value, $flags, $this->refs); Chris@0: } Chris@0: Chris@17: $lines = []; Chris@14: Chris@0: while ($this->moveToNextLine()) { Chris@0: // unquoted strings end before the first unindented line Chris@14: if (null === $quotation && 0 === $this->getCurrentLineIndentation()) { Chris@0: $this->moveToPreviousLine(); Chris@0: Chris@0: break; Chris@0: } Chris@0: Chris@14: $lines[] = trim($this->currentLine); Chris@0: Chris@0: // quoted string values end with a line that is terminated with the quotation character Chris@0: if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@17: for ($i = 0, $linesCount = \count($lines), $previousLineBlank = false; $i < $linesCount; ++$i) { Chris@14: if ('' === $lines[$i]) { Chris@14: $value .= "\n"; Chris@14: $previousLineBlank = true; Chris@14: } elseif ($previousLineBlank) { Chris@14: $value .= $lines[$i]; Chris@14: $previousLineBlank = false; Chris@14: } else { Chris@14: $value .= ' '.$lines[$i]; Chris@14: $previousLineBlank = false; Chris@14: } Chris@14: } Chris@14: Chris@0: Inline::$parsedLineNumber = $this->getRealCurrentLineNb(); Chris@14: Chris@0: $parsedValue = Inline::parse($value, $flags, $this->refs); Chris@0: Chris@17: if ('mapping' === $context && \is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) { Chris@14: throw new ParseException('A colon cannot be used in an unquoted mapping value.', $this->getRealCurrentLineNb() + 1, $value, $this->filename); Chris@0: } Chris@0: Chris@0: return $parsedValue; Chris@0: } catch (ParseException $e) { Chris@0: $e->setParsedLine($this->getRealCurrentLineNb() + 1); Chris@0: $e->setSnippet($this->currentLine); Chris@0: Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses a block scalar. Chris@0: * Chris@0: * @param string $style The style indicator that was used to begin this block scalar (| or >) Chris@0: * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -) Chris@0: * @param int $indentation The indentation indicator that was used to begin this block scalar Chris@0: * Chris@0: * @return string The text value Chris@0: */ Chris@0: private function parseBlockScalar($style, $chomping = '', $indentation = 0) Chris@0: { Chris@0: $notEOF = $this->moveToNextLine(); Chris@0: if (!$notEOF) { Chris@0: return ''; Chris@0: } Chris@0: Chris@0: $isCurrentLineBlank = $this->isCurrentLineBlank(); Chris@17: $blockLines = []; Chris@0: Chris@0: // leading blank lines are consumed before determining indentation Chris@0: while ($notEOF && $isCurrentLineBlank) { Chris@0: // newline only if not EOF Chris@0: if ($notEOF = $this->moveToNextLine()) { Chris@0: $blockLines[] = ''; Chris@0: $isCurrentLineBlank = $this->isCurrentLineBlank(); Chris@0: } Chris@0: } Chris@0: Chris@0: // determine indentation if not specified Chris@0: if (0 === $indentation) { Chris@0: if (self::preg_match('/^ +/', $this->currentLine, $matches)) { Chris@17: $indentation = \strlen($matches[0]); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($indentation > 0) { Chris@0: $pattern = sprintf('/^ {%d}(.*)$/', $indentation); Chris@0: Chris@0: while ( Chris@0: $notEOF && ( Chris@0: $isCurrentLineBlank || Chris@0: self::preg_match($pattern, $this->currentLine, $matches) Chris@0: ) Chris@0: ) { Chris@17: if ($isCurrentLineBlank && \strlen($this->currentLine) > $indentation) { Chris@0: $blockLines[] = substr($this->currentLine, $indentation); Chris@0: } elseif ($isCurrentLineBlank) { Chris@0: $blockLines[] = ''; Chris@0: } else { Chris@0: $blockLines[] = $matches[1]; Chris@0: } Chris@0: Chris@0: // newline only if not EOF Chris@0: if ($notEOF = $this->moveToNextLine()) { Chris@0: $isCurrentLineBlank = $this->isCurrentLineBlank(); Chris@0: } Chris@0: } Chris@0: } elseif ($notEOF) { Chris@0: $blockLines[] = ''; Chris@0: } Chris@0: Chris@0: if ($notEOF) { Chris@0: $blockLines[] = ''; Chris@0: $this->moveToPreviousLine(); Chris@0: } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) { Chris@0: $blockLines[] = ''; Chris@0: } Chris@0: Chris@0: // folded style Chris@0: if ('>' === $style) { Chris@0: $text = ''; Chris@0: $previousLineIndented = false; Chris@0: $previousLineBlank = false; Chris@0: Chris@17: for ($i = 0, $blockLinesCount = \count($blockLines); $i < $blockLinesCount; ++$i) { Chris@0: if ('' === $blockLines[$i]) { Chris@0: $text .= "\n"; Chris@0: $previousLineIndented = false; Chris@0: $previousLineBlank = true; Chris@0: } elseif (' ' === $blockLines[$i][0]) { Chris@0: $text .= "\n".$blockLines[$i]; Chris@0: $previousLineIndented = true; Chris@0: $previousLineBlank = false; Chris@0: } elseif ($previousLineIndented) { Chris@0: $text .= "\n".$blockLines[$i]; Chris@0: $previousLineIndented = false; Chris@0: $previousLineBlank = false; Chris@0: } elseif ($previousLineBlank || 0 === $i) { Chris@0: $text .= $blockLines[$i]; Chris@0: $previousLineIndented = false; Chris@0: $previousLineBlank = false; Chris@0: } else { Chris@0: $text .= ' '.$blockLines[$i]; Chris@0: $previousLineIndented = false; Chris@0: $previousLineBlank = false; Chris@0: } Chris@0: } Chris@0: } else { Chris@0: $text = implode("\n", $blockLines); Chris@0: } Chris@0: Chris@0: // deal with trailing newlines Chris@0: if ('' === $chomping) { Chris@0: $text = preg_replace('/\n+$/', "\n", $text); Chris@0: } elseif ('-' === $chomping) { Chris@0: $text = preg_replace('/\n+$/', '', $text); Chris@0: } Chris@0: Chris@0: return $text; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the next line is indented. Chris@0: * Chris@0: * @return bool Returns true if the next line is indented, false otherwise Chris@0: */ Chris@0: private function isNextLineIndented() Chris@0: { Chris@0: $currentIndentation = $this->getCurrentLineIndentation(); Chris@14: $movements = 0; Chris@0: Chris@14: do { Chris@0: $EOF = !$this->moveToNextLine(); Chris@14: Chris@14: if (!$EOF) { Chris@14: ++$movements; Chris@14: } Chris@14: } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment())); Chris@0: Chris@0: if ($EOF) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $ret = $this->getCurrentLineIndentation() > $currentIndentation; Chris@0: Chris@14: for ($i = 0; $i < $movements; ++$i) { Chris@14: $this->moveToPreviousLine(); Chris@14: } Chris@0: Chris@0: return $ret; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the current line is blank or if it is a comment line. Chris@0: * Chris@0: * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise Chris@0: */ Chris@0: private function isCurrentLineEmpty() Chris@0: { Chris@0: return $this->isCurrentLineBlank() || $this->isCurrentLineComment(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the current line is blank. Chris@0: * Chris@0: * @return bool Returns true if the current line is blank, false otherwise Chris@0: */ Chris@0: private function isCurrentLineBlank() Chris@0: { Chris@0: return '' == trim($this->currentLine, ' '); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the current line is a comment line. Chris@0: * Chris@0: * @return bool Returns true if the current line is a comment line, false otherwise Chris@0: */ Chris@0: private function isCurrentLineComment() Chris@0: { Chris@0: //checking explicitly the first char of the trim is faster than loops or strpos Chris@0: $ltrimmedLine = ltrim($this->currentLine, ' '); Chris@0: Chris@14: return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0]; Chris@0: } Chris@0: Chris@0: private function isCurrentLineLastLineInDocument() Chris@0: { Chris@0: return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Cleanups a YAML string to be parsed. Chris@0: * Chris@0: * @param string $value The input YAML string Chris@0: * Chris@0: * @return string A cleaned up YAML string Chris@0: */ Chris@0: private function cleanup($value) Chris@0: { Chris@17: $value = str_replace(["\r\n", "\r"], "\n", $value); Chris@0: Chris@0: // strip YAML header Chris@0: $count = 0; Chris@0: $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count); Chris@0: $this->offset += $count; Chris@0: Chris@0: // remove leading comments Chris@0: $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count); Chris@14: if (1 === $count) { Chris@0: // items have been removed, update the offset Chris@0: $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n"); Chris@0: $value = $trimmedValue; Chris@0: } Chris@0: Chris@0: // remove start of the document marker (---) Chris@0: $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count); Chris@14: if (1 === $count) { Chris@0: // items have been removed, update the offset Chris@0: $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n"); Chris@0: $value = $trimmedValue; Chris@0: Chris@0: // remove end of the document marker (...) Chris@0: $value = preg_replace('#\.\.\.\s*$#', '', $value); Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the next line starts unindented collection. Chris@0: * Chris@0: * @return bool Returns true if the next line starts unindented collection, false otherwise Chris@0: */ Chris@0: private function isNextLineUnIndentedCollection() Chris@0: { Chris@0: $currentIndentation = $this->getCurrentLineIndentation(); Chris@14: $movements = 0; Chris@0: Chris@14: do { Chris@14: $EOF = !$this->moveToNextLine(); Chris@0: Chris@14: if (!$EOF) { Chris@14: ++$movements; Chris@14: } Chris@14: } while (!$EOF && ($this->isCurrentLineEmpty() || $this->isCurrentLineComment())); Chris@14: Chris@14: if ($EOF) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem(); Chris@0: Chris@14: for ($i = 0; $i < $movements; ++$i) { Chris@14: $this->moveToPreviousLine(); Chris@14: } Chris@0: Chris@0: return $ret; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the string is un-indented collection item. Chris@0: * Chris@0: * @return bool Returns true if the string is un-indented collection item, false otherwise Chris@0: */ Chris@0: private function isStringUnIndentedCollectionItem() Chris@0: { Chris@0: return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- '); Chris@0: } Chris@0: Chris@0: /** Chris@0: * A local wrapper for `preg_match` which will throw a ParseException if there Chris@0: * is an internal error in the PCRE engine. Chris@0: * Chris@0: * This avoids us needing to check for "false" every time PCRE is used Chris@0: * in the YAML engine Chris@0: * Chris@0: * @throws ParseException on a PCRE internal error Chris@0: * Chris@0: * @see preg_last_error() Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) Chris@0: { Chris@0: if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) { Chris@0: switch (preg_last_error()) { Chris@0: case PREG_INTERNAL_ERROR: Chris@0: $error = 'Internal PCRE error.'; Chris@0: break; Chris@0: case PREG_BACKTRACK_LIMIT_ERROR: Chris@0: $error = 'pcre.backtrack_limit reached.'; Chris@0: break; Chris@0: case PREG_RECURSION_LIMIT_ERROR: Chris@0: $error = 'pcre.recursion_limit reached.'; Chris@0: break; Chris@0: case PREG_BAD_UTF8_ERROR: Chris@0: $error = 'Malformed UTF-8 data.'; Chris@0: break; Chris@0: case PREG_BAD_UTF8_OFFSET_ERROR: Chris@0: $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; Chris@0: break; Chris@0: default: Chris@0: $error = 'Error.'; Chris@0: } Chris@0: Chris@0: throw new ParseException($error); Chris@0: } Chris@0: Chris@0: return $ret; Chris@0: } Chris@14: Chris@14: /** Chris@14: * Trim the tag on top of the value. Chris@14: * Chris@14: * Prevent values such as `!foo {quz: bar}` to be considered as Chris@14: * a mapping block. Chris@14: */ Chris@14: private function trimTag($value) Chris@14: { Chris@14: if ('!' === $value[0]) { Chris@14: return ltrim(substr($value, 1, strcspn($value, " \r\n", 1)), ' '); Chris@14: } Chris@14: Chris@14: return $value; Chris@14: } Chris@14: Chris@14: private function getLineTag($value, $flags, $nextLineCheck = true) Chris@14: { Chris@14: if ('' === $value || '!' !== $value[0] || 1 !== self::preg_match('/^'.self::TAG_PATTERN.' *( +#.*)?$/', $value, $matches)) { Chris@14: return; Chris@14: } Chris@14: Chris@14: if ($nextLineCheck && !$this->isNextLineIndented()) { Chris@14: return; Chris@14: } Chris@14: Chris@14: $tag = substr($matches['tag'], 1); Chris@14: Chris@14: // Built-in tags Chris@14: if ($tag && '!' === $tag[0]) { Chris@14: throw new ParseException(sprintf('The built-in tag "!%s" is not implemented.', $tag), $this->getRealCurrentLineNb() + 1, $value, $this->filename); Chris@14: } Chris@14: Chris@14: if (Yaml::PARSE_CUSTOM_TAGS & $flags) { Chris@14: return $tag; Chris@14: } Chris@14: Chris@14: throw new ParseException(sprintf('Tags support is not enabled. You must use the flag `Yaml::PARSE_CUSTOM_TAGS` to use "%s".', $matches['tag']), $this->getRealCurrentLineNb() + 1, $value, $this->filename); Chris@14: } Chris@14: Chris@14: private function getDeprecationMessage($message) Chris@14: { Chris@14: $message = rtrim($message, '.'); Chris@14: Chris@14: if (null !== $this->filename) { Chris@14: $message .= ' in '.$this->filename; Chris@14: } Chris@14: Chris@14: $message .= ' on line '.($this->getRealCurrentLineNb() + 1); Chris@14: Chris@14: return $message.'.'; Chris@14: } Chris@0: }