2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Yaml ;
use Symfony\Component\Yaml\Exception\ParseException ;
/**
* Parser parses YAML strings to convert them to PHP arrays .
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class Parser
{
2015-11-17 21:42:33 +00:00
const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?' ;
// BC - wrongly named
const FOLDED_SCALAR_PATTERN = self :: BLOCK_SCALAR_HEADER_PATTERN ;
2015-08-18 00:00:26 +00:00
private $offset = 0 ;
2017-02-03 00:28:38 +00:00
private $totalNumberOfLines ;
2015-08-18 00:00:26 +00:00
private $lines = array ();
private $currentLineNb = - 1 ;
private $currentLine = '' ;
private $refs = array ();
2017-02-03 00:28:38 +00:00
private $skippedLineNumbers = array ();
private $locallySkippedLineNumbers = array ();
2015-08-18 00:00:26 +00:00
/**
* Constructor .
*
2017-02-03 00:28:38 +00:00
* @ param int $offset The offset of YAML document ( used for line numbers in error messages )
* @ param int | null $totalNumberOfLines The overall number of lines being parsed
* @ param int [] $skippedLineNumbers Number of comment lines that have been skipped by the parser
2015-08-18 00:00:26 +00:00
*/
2017-02-03 00:28:38 +00:00
public function __construct ( $offset = 0 , $totalNumberOfLines = null , array $skippedLineNumbers = array ())
2015-08-18 00:00:26 +00:00
{
$this -> offset = $offset ;
2017-02-03 00:28:38 +00:00
$this -> totalNumberOfLines = $totalNumberOfLines ;
$this -> skippedLineNumbers = $skippedLineNumbers ;
2015-08-18 00:00:26 +00:00
}
/**
* Parses a YAML string to a PHP value .
*
* @ param string $value A YAML string
* @ param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types ( a PHP resource or object ), false otherwise
* @ param bool $objectSupport true if object support is enabled , false otherwise
* @ param bool $objectForMap true if maps should return a stdClass instead of array ()
*
* @ return mixed A PHP value
*
* @ throws ParseException If the YAML is not valid
*/
public function parse ( $value , $exceptionOnInvalidType = false , $objectSupport = false , $objectForMap = false )
{
2017-04-13 14:53:35 +00:00
if ( false === preg_match ( '//u' , $value )) {
2015-08-18 00:00:26 +00:00
throw new ParseException ( 'The YAML value does not appear to be valid UTF-8.' );
}
2017-07-03 15:47:07 +00:00
$this -> refs = array ();
$mbEncoding = null ;
$e = null ;
$data = null ;
if ( 2 /* MB_OVERLOAD_STRING */ & ( int ) ini_get ( 'mbstring.func_overload' )) {
$mbEncoding = mb_internal_encoding ();
mb_internal_encoding ( 'UTF-8' );
}
try {
$data = $this -> doParse ( $value , $exceptionOnInvalidType , $objectSupport , $objectForMap );
} catch ( \Exception $e ) {
} catch ( \Throwable $e ) {
}
if ( null !== $mbEncoding ) {
mb_internal_encoding ( $mbEncoding );
}
$this -> lines = array ();
$this -> currentLine = '' ;
$this -> refs = array ();
$this -> skippedLineNumbers = array ();
$this -> locallySkippedLineNumbers = array ();
if ( null !== $e ) {
throw $e ;
}
return $data ;
}
private function doParse ( $value , $exceptionOnInvalidType = false , $objectSupport = false , $objectForMap = false )
{
2015-08-18 00:00:26 +00:00
$this -> currentLineNb = - 1 ;
$this -> currentLine = '' ;
$value = $this -> cleanup ( $value );
$this -> lines = explode ( " \n " , $value );
2017-07-03 15:47:07 +00:00
$this -> locallySkippedLineNumbers = array ();
2015-08-18 00:00:26 +00:00
2017-02-03 00:28:38 +00:00
if ( null === $this -> totalNumberOfLines ) {
$this -> totalNumberOfLines = count ( $this -> lines );
}
2015-08-18 00:00:26 +00:00
$data = array ();
$context = null ;
$allowOverwrite = false ;
2017-07-03 15:47:07 +00:00
2015-08-18 00:00:26 +00:00
while ( $this -> moveToNextLine ()) {
if ( $this -> isCurrentLineEmpty ()) {
continue ;
}
// tab?
if ( " \t " === $this -> currentLine [ 0 ]) {
throw new ParseException ( 'A YAML file cannot contain tabs as indentation.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
$isRef = $mergeNode = false ;
2017-04-13 14:53:35 +00:00
if ( self :: preg_match ( '#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u' , rtrim ( $this -> currentLine ), $values )) {
2015-08-18 00:00:26 +00:00
if ( $context && 'mapping' == $context ) {
2017-02-03 00:28:38 +00:00
throw new ParseException ( 'You cannot define a sequence item when in a mapping' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
2015-08-18 00:00:26 +00:00
}
$context = 'sequence' ;
2017-04-13 14:53:35 +00:00
if ( isset ( $values [ 'value' ]) && self :: preg_match ( '#^&(?P<ref>[^ ]+) *(?P<value>.*)#u' , $values [ 'value' ], $matches )) {
2015-08-18 00:00:26 +00:00
$isRef = $matches [ 'ref' ];
$values [ 'value' ] = $matches [ 'value' ];
}
// array
if ( ! isset ( $values [ 'value' ]) || '' == trim ( $values [ 'value' ], ' ' ) || 0 === strpos ( ltrim ( $values [ 'value' ], ' ' ), '#' )) {
2017-02-03 00:28:38 +00:00
$data [] = $this -> parseBlock ( $this -> getRealCurrentLineNb () + 1 , $this -> getNextEmbedBlock ( null , true ), $exceptionOnInvalidType , $objectSupport , $objectForMap );
2015-08-18 00:00:26 +00:00
} else {
if ( isset ( $values [ 'leadspaces' ])
2017-04-13 14:53:35 +00:00
&& self :: preg_match ( '#^(?P<key>' . Inline :: REGEX_QUOTED_STRING . '|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+))?$#u' , rtrim ( $values [ 'value' ]), $matches )
2015-08-18 00:00:26 +00:00
) {
// this is a compact notation element, add to next block and parse
$block = $values [ 'value' ];
if ( $this -> isNextLineIndented ()) {
$block .= " \n " . $this -> getNextEmbedBlock ( $this -> getCurrentLineIndentation () + strlen ( $values [ 'leadspaces' ]) + 1 );
}
2017-02-03 00:28:38 +00:00
$data [] = $this -> parseBlock ( $this -> getRealCurrentLineNb (), $block , $exceptionOnInvalidType , $objectSupport , $objectForMap );
2015-08-18 00:00:26 +00:00
} else {
2016-04-20 16:56:34 +00:00
$data [] = $this -> parseValue ( $values [ 'value' ], $exceptionOnInvalidType , $objectSupport , $objectForMap , $context );
2015-08-18 00:00:26 +00:00
}
}
2015-10-08 18:40:12 +00:00
if ( $isRef ) {
$this -> refs [ $isRef ] = end ( $data );
}
2017-04-13 14:53:35 +00:00
} elseif (
self :: preg_match ( '#^(?P<key>' . Inline :: REGEX_QUOTED_STRING . '|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+))?$#u' , rtrim ( $this -> currentLine ), $values )
&& ( false === strpos ( $values [ 'key' ], ' #' ) || in_array ( $values [ 'key' ][ 0 ], array ( '"' , " ' " )))
) {
2015-08-18 00:00:26 +00:00
if ( $context && 'sequence' == $context ) {
2017-02-03 00:28:38 +00:00
throw new ParseException ( 'You cannot define a mapping item when in a sequence' , $this -> currentLineNb + 1 , $this -> currentLine );
2015-08-18 00:00:26 +00:00
}
$context = 'mapping' ;
// force correct settings
Inline :: parse ( null , $exceptionOnInvalidType , $objectSupport , $objectForMap , $this -> refs );
try {
$key = Inline :: parseScalar ( $values [ 'key' ]);
} catch ( ParseException $e ) {
$e -> setParsedLine ( $this -> getRealCurrentLineNb () + 1 );
$e -> setSnippet ( $this -> currentLine );
throw $e ;
}
2015-10-08 18:40:12 +00:00
// Convert float keys to strings, to avoid being converted to integers by PHP
if ( is_float ( $key )) {
$key = ( string ) $key ;
}
2015-08-18 00:00:26 +00:00
if ( '<<' === $key ) {
$mergeNode = true ;
$allowOverwrite = true ;
if ( isset ( $values [ 'value' ]) && 0 === strpos ( $values [ 'value' ], '*' )) {
$refName = substr ( $values [ 'value' ], 1 );
if ( ! array_key_exists ( $refName , $this -> refs )) {
throw new ParseException ( sprintf ( 'Reference "%s" does not exist.' , $refName ), $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
$refValue = $this -> refs [ $refName ];
if ( ! is_array ( $refValue )) {
throw new ParseException ( 'YAML merge keys used with a scalar value instead of an array.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
2017-04-13 14:53:35 +00:00
$data += $refValue ; // array union
2015-08-18 00:00:26 +00:00
} else {
if ( isset ( $values [ 'value' ]) && $values [ 'value' ] !== '' ) {
$value = $values [ 'value' ];
} else {
$value = $this -> getNextEmbedBlock ();
}
2017-02-03 00:28:38 +00:00
$parsed = $this -> parseBlock ( $this -> getRealCurrentLineNb () + 1 , $value , $exceptionOnInvalidType , $objectSupport , $objectForMap );
2015-08-18 00:00:26 +00:00
if ( ! is_array ( $parsed )) {
throw new ParseException ( 'YAML merge keys used with a scalar value instead of an array.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
if ( isset ( $parsed [ 0 ])) {
// If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
// and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
// in the sequence override keys specified in later mapping nodes.
foreach ( $parsed as $parsedItem ) {
if ( ! is_array ( $parsedItem )) {
throw new ParseException ( 'Merge items must be arrays.' , $this -> getRealCurrentLineNb () + 1 , $parsedItem );
}
2017-04-13 14:53:35 +00:00
$data += $parsedItem ; // array union
2015-08-18 00:00:26 +00:00
}
} else {
// If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
// current mapping, unless the key already exists in it.
2017-04-13 14:53:35 +00:00
$data += $parsed ; // array union
2015-08-18 00:00:26 +00:00
}
}
2017-04-13 14:53:35 +00:00
} elseif ( isset ( $values [ 'value' ]) && self :: preg_match ( '#^&(?P<ref>[^ ]+) *(?P<value>.*)#u' , $values [ 'value' ], $matches )) {
2015-08-18 00:00:26 +00:00
$isRef = $matches [ 'ref' ];
$values [ 'value' ] = $matches [ 'value' ];
}
if ( $mergeNode ) {
// Merge keys
} elseif ( ! isset ( $values [ 'value' ]) || '' == trim ( $values [ 'value' ], ' ' ) || 0 === strpos ( ltrim ( $values [ 'value' ], ' ' ), '#' )) {
// hash
// if next line is less indented or equal, then it means that the current value is null
if ( ! $this -> isNextLineIndented () && ! $this -> isNextLineUnIndentedCollection ()) {
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ( $allowOverwrite || ! isset ( $data [ $key ])) {
$data [ $key ] = null ;
}
} else {
2017-02-03 00:28:38 +00:00
$value = $this -> parseBlock ( $this -> getRealCurrentLineNb () + 1 , $this -> getNextEmbedBlock (), $exceptionOnInvalidType , $objectSupport , $objectForMap );
2015-08-18 00:00:26 +00:00
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ( $allowOverwrite || ! isset ( $data [ $key ])) {
$data [ $key ] = $value ;
}
}
} else {
2016-04-20 16:56:34 +00:00
$value = $this -> parseValue ( $values [ 'value' ], $exceptionOnInvalidType , $objectSupport , $objectForMap , $context );
2015-08-18 00:00:26 +00:00
// Spec: Keys MUST be unique; first one wins.
// But overwriting is allowed when a merge node is used in current block.
if ( $allowOverwrite || ! isset ( $data [ $key ])) {
$data [ $key ] = $value ;
}
}
2015-10-08 18:40:12 +00:00
if ( $isRef ) {
$this -> refs [ $isRef ] = $data [ $key ];
}
2015-08-18 00:00:26 +00:00
} else {
// multiple documents are not supported
if ( '---' === $this -> currentLine ) {
2017-02-03 00:28:38 +00:00
throw new ParseException ( 'Multiple documents are not supported.' , $this -> currentLineNb + 1 , $this -> currentLine );
2015-08-18 00:00:26 +00:00
}
// 1-liner optionally followed by newline(s)
2015-09-04 20:20:09 +00:00
if ( is_string ( $value ) && $this -> lines [ 0 ] === trim ( $value )) {
2015-08-18 00:00:26 +00:00
try {
$value = Inline :: parse ( $this -> lines [ 0 ], $exceptionOnInvalidType , $objectSupport , $objectForMap , $this -> refs );
} catch ( ParseException $e ) {
$e -> setParsedLine ( $this -> getRealCurrentLineNb () + 1 );
$e -> setSnippet ( $this -> currentLine );
throw $e ;
}
return $value ;
}
2017-04-13 14:53:35 +00:00
throw new ParseException ( 'Unable to parse.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
2015-08-18 00:00:26 +00:00
}
}
2016-04-20 16:56:34 +00:00
if ( $objectForMap && ! is_object ( $data ) && 'mapping' === $context ) {
$object = new \stdClass ();
foreach ( $data as $key => $value ) {
$object -> $key = $value ;
}
$data = $object ;
}
2015-08-18 00:00:26 +00:00
return empty ( $data ) ? null : $data ;
}
2017-02-03 00:28:38 +00:00
private function parseBlock ( $offset , $yaml , $exceptionOnInvalidType , $objectSupport , $objectForMap )
{
$skippedLineNumbers = $this -> skippedLineNumbers ;
foreach ( $this -> locallySkippedLineNumbers as $lineNumber ) {
if ( $lineNumber < $offset ) {
continue ;
}
$skippedLineNumbers [] = $lineNumber ;
}
$parser = new self ( $offset , $this -> totalNumberOfLines , $skippedLineNumbers );
$parser -> refs = & $this -> refs ;
2017-07-03 15:47:07 +00:00
return $parser -> doParse ( $yaml , $exceptionOnInvalidType , $objectSupport , $objectForMap );
2017-02-03 00:28:38 +00:00
}
2015-08-18 00:00:26 +00:00
/**
* Returns the current line number ( takes the offset into account ) .
*
* @ return int The current line number
*/
private function getRealCurrentLineNb ()
{
2017-02-03 00:28:38 +00:00
$realCurrentLineNumber = $this -> currentLineNb + $this -> offset ;
foreach ( $this -> skippedLineNumbers as $skippedLineNumber ) {
if ( $skippedLineNumber > $realCurrentLineNumber ) {
break ;
}
++ $realCurrentLineNumber ;
}
return $realCurrentLineNumber ;
2015-08-18 00:00:26 +00:00
}
/**
* Returns the current line indentation .
*
* @ return int The current line indentation
*/
private function getCurrentLineIndentation ()
{
return strlen ( $this -> currentLine ) - strlen ( ltrim ( $this -> currentLine , ' ' ));
}
/**
* Returns the next embed block of YAML .
*
* @ param int $indentation The indent level at which the block is to be read , or null for default
* @ param bool $inSequence True if the enclosing data structure is a sequence
*
* @ return string A YAML string
*
* @ throws ParseException When indentation problem are detected
*/
private function getNextEmbedBlock ( $indentation = null , $inSequence = false )
{
$oldLineIndentation = $this -> getCurrentLineIndentation ();
2016-04-20 16:56:34 +00:00
$blockScalarIndentations = array ();
if ( $this -> isBlockScalarHeader ()) {
$blockScalarIndentations [] = $this -> getCurrentLineIndentation ();
}
2015-08-18 00:00:26 +00:00
if ( ! $this -> moveToNextLine ()) {
return ;
}
if ( null === $indentation ) {
$newIndent = $this -> getCurrentLineIndentation ();
2016-04-20 16:56:34 +00:00
$unindentedEmbedBlock = $this -> isStringUnIndentedCollectionItem ();
2015-08-18 00:00:26 +00:00
if ( ! $this -> isCurrentLineEmpty () && 0 === $newIndent && ! $unindentedEmbedBlock ) {
throw new ParseException ( 'Indentation problem.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
} else {
$newIndent = $indentation ;
}
$data = array ();
if ( $this -> getCurrentLineIndentation () >= $newIndent ) {
$data [] = substr ( $this -> currentLine , $newIndent );
} else {
$this -> moveToPreviousLine ();
return ;
}
2015-09-04 20:20:09 +00:00
if ( $inSequence && $oldLineIndentation === $newIndent && isset ( $data [ 0 ][ 0 ]) && '-' === $data [ 0 ][ 0 ]) {
2015-08-18 00:00:26 +00:00
// the previous line contained a dash but no item content, this line is a sequence item with the same indentation
// and therefore no nested list or mapping
$this -> moveToPreviousLine ();
return ;
}
2016-04-20 16:56:34 +00:00
$isItUnindentedCollection = $this -> isStringUnIndentedCollectionItem ();
if ( empty ( $blockScalarIndentations ) && $this -> isBlockScalarHeader ()) {
$blockScalarIndentations [] = $this -> getCurrentLineIndentation ();
}
2015-08-18 00:00:26 +00:00
2016-04-20 16:56:34 +00:00
$previousLineIndentation = $this -> getCurrentLineIndentation ();
2015-08-18 00:00:26 +00:00
while ( $this -> moveToNextLine ()) {
$indent = $this -> getCurrentLineIndentation ();
2016-04-20 16:56:34 +00:00
// terminate all block scalars that are more indented than the current line
if ( ! empty ( $blockScalarIndentations ) && $indent < $previousLineIndentation && trim ( $this -> currentLine ) !== '' ) {
foreach ( $blockScalarIndentations as $key => $blockScalarIndentation ) {
if ( $blockScalarIndentation >= $this -> getCurrentLineIndentation ()) {
unset ( $blockScalarIndentations [ $key ]);
}
}
}
if ( empty ( $blockScalarIndentations ) && ! $this -> isCurrentLineComment () && $this -> isBlockScalarHeader ()) {
$blockScalarIndentations [] = $this -> getCurrentLineIndentation ();
2015-08-18 00:00:26 +00:00
}
2016-04-20 16:56:34 +00:00
$previousLineIndentation = $indent ;
2017-02-03 00:28:38 +00:00
if ( $isItUnindentedCollection && ! $this -> isCurrentLineEmpty () && ! $this -> isStringUnIndentedCollectionItem () && $newIndent === $indent ) {
2015-08-18 00:00:26 +00:00
$this -> moveToPreviousLine ();
break ;
}
if ( $this -> isCurrentLineBlank ()) {
$data [] = substr ( $this -> currentLine , $newIndent );
continue ;
}
2016-04-20 16:56:34 +00:00
// we ignore "comment" lines only when we are not inside a scalar block
if ( empty ( $blockScalarIndentations ) && $this -> isCurrentLineComment ()) {
2017-02-03 00:28:38 +00:00
// remember ignored comment lines (they are used later in nested
// parser calls to determine real line numbers)
//
// CAUTION: beware to not populate the global property here as it
// will otherwise influence the getRealCurrentLineNb() call here
// for consecutive comment lines and subsequent embedded blocks
$this -> locallySkippedLineNumbers [] = $this -> getRealCurrentLineNb ();
2015-08-18 00:00:26 +00:00
continue ;
}
if ( $indent >= $newIndent ) {
$data [] = substr ( $this -> currentLine , $newIndent );
} elseif ( 0 == $indent ) {
$this -> moveToPreviousLine ();
break ;
} else {
throw new ParseException ( 'Indentation problem.' , $this -> getRealCurrentLineNb () + 1 , $this -> currentLine );
}
}
return implode ( " \n " , $data );
}
/**
* Moves the parser to the next line .
*
* @ return bool
*/
private function moveToNextLine ()
{
if ( $this -> currentLineNb >= count ( $this -> lines ) - 1 ) {
return false ;
}
$this -> currentLine = $this -> lines [ ++ $this -> currentLineNb ];
return true ;
}
/**
* Moves the parser to the previous line .
2017-02-03 00:28:38 +00:00
*
* @ return bool
2015-08-18 00:00:26 +00:00
*/
private function moveToPreviousLine ()
{
2017-02-03 00:28:38 +00:00
if ( $this -> currentLineNb < 1 ) {
return false ;
}
2015-08-18 00:00:26 +00:00
$this -> currentLine = $this -> lines [ -- $this -> currentLineNb ];
2017-02-03 00:28:38 +00:00
return true ;
2015-08-18 00:00:26 +00:00
}
/**
* Parses a YAML value .
*
* @ param string $value A YAML value
* @ param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
* @ param bool $objectSupport True if object support is enabled , false otherwise
* @ param bool $objectForMap true if maps should return a stdClass instead of array ()
2016-04-20 16:56:34 +00:00
* @ param string $context The parser context ( either sequence or mapping )
2015-08-18 00:00:26 +00:00
*
* @ return mixed A PHP value
*
* @ throws ParseException When reference does not exist
*/
2016-04-20 16:56:34 +00:00
private function parseValue ( $value , $exceptionOnInvalidType , $objectSupport , $objectForMap , $context )
2015-08-18 00:00:26 +00:00
{
if ( 0 === strpos ( $value , '*' )) {
if ( false !== $pos = strpos ( $value , '#' )) {
$value = substr ( $value , 1 , $pos - 2 );
} else {
$value = substr ( $value , 1 );
}
if ( ! array_key_exists ( $value , $this -> refs )) {
2017-02-03 00:28:38 +00:00
throw new ParseException ( sprintf ( 'Reference "%s" does not exist.' , $value ), $this -> currentLineNb + 1 , $this -> currentLine );
2015-08-18 00:00:26 +00:00
}
return $this -> refs [ $value ];
}
2017-04-13 14:53:35 +00:00
if ( self :: preg_match ( '/^' . self :: BLOCK_SCALAR_HEADER_PATTERN . '$/' , $value , $matches )) {
2015-08-18 00:00:26 +00:00
$modifiers = isset ( $matches [ 'modifiers' ]) ? $matches [ 'modifiers' ] : '' ;
2015-11-17 21:42:33 +00:00
return $this -> parseBlockScalar ( $matches [ 'separator' ], preg_replace ( '#\d+#' , '' , $modifiers ), ( int ) abs ( $modifiers ));
2015-08-18 00:00:26 +00:00
}
try {
2016-04-20 16:56:34 +00:00
$parsedValue = Inline :: parse ( $value , $exceptionOnInvalidType , $objectSupport , $objectForMap , $this -> refs );
if ( 'mapping' === $context && '"' !== $value [ 0 ] && " ' " !== $value [ 0 ] && '[' !== $value [ 0 ] && '{' !== $value [ 0 ] && '!' !== $value [ 0 ] && false !== strpos ( $parsedValue , ': ' )) {
@ trigger_error ( sprintf ( 'Using a colon in the unquoted mapping value "%s" in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.' , $value , $this -> getRealCurrentLineNb () + 1 ), E_USER_DEPRECATED );
// to be thrown in 3.0
// throw new ParseException('A colon cannot be used in an unquoted mapping value.');
}
return $parsedValue ;
2015-08-18 00:00:26 +00:00
} catch ( ParseException $e ) {
$e -> setParsedLine ( $this -> getRealCurrentLineNb () + 1 );
$e -> setSnippet ( $this -> currentLine );
throw $e ;
}
}
/**
2015-11-17 21:42:33 +00:00
* Parses a block scalar .
2015-08-18 00:00:26 +00:00
*
2015-11-17 21:42:33 +00:00
* @ param string $style The style indicator that was used to begin this block scalar ( | or > )
* @ param string $chomping The chomping indicator that was used to begin this block scalar ( + or - )
* @ param int $indentation The indentation indicator that was used to begin this block scalar
2015-08-18 00:00:26 +00:00
*
* @ return string The text value
*/
2015-11-17 21:42:33 +00:00
private function parseBlockScalar ( $style , $chomping = '' , $indentation = 0 )
2015-08-18 00:00:26 +00:00
{
$notEOF = $this -> moveToNextLine ();
if ( ! $notEOF ) {
return '' ;
}
$isCurrentLineBlank = $this -> isCurrentLineBlank ();
2016-04-20 16:56:34 +00:00
$blockLines = array ();
2015-08-18 00:00:26 +00:00
// leading blank lines are consumed before determining indentation
while ( $notEOF && $isCurrentLineBlank ) {
// newline only if not EOF
if ( $notEOF = $this -> moveToNextLine ()) {
2016-04-20 16:56:34 +00:00
$blockLines [] = '' ;
2015-08-18 00:00:26 +00:00
$isCurrentLineBlank = $this -> isCurrentLineBlank ();
}
}
// determine indentation if not specified
if ( 0 === $indentation ) {
2017-04-13 14:53:35 +00:00
if ( self :: preg_match ( '/^ +/' , $this -> currentLine , $matches )) {
2015-08-18 00:00:26 +00:00
$indentation = strlen ( $matches [ 0 ]);
}
}
if ( $indentation > 0 ) {
$pattern = sprintf ( '/^ {%d}(.*)$/' , $indentation );
while (
$notEOF && (
$isCurrentLineBlank ||
2017-04-13 14:53:35 +00:00
self :: preg_match ( $pattern , $this -> currentLine , $matches )
2015-08-18 00:00:26 +00:00
)
) {
2016-04-20 16:56:34 +00:00
if ( $isCurrentLineBlank && strlen ( $this -> currentLine ) > $indentation ) {
$blockLines [] = substr ( $this -> currentLine , $indentation );
} elseif ( $isCurrentLineBlank ) {
$blockLines [] = '' ;
2015-08-18 00:00:26 +00:00
} else {
2016-04-20 16:56:34 +00:00
$blockLines [] = $matches [ 1 ];
2015-08-18 00:00:26 +00:00
}
// newline only if not EOF
if ( $notEOF = $this -> moveToNextLine ()) {
$isCurrentLineBlank = $this -> isCurrentLineBlank ();
}
}
} elseif ( $notEOF ) {
2016-04-20 16:56:34 +00:00
$blockLines [] = '' ;
2015-08-18 00:00:26 +00:00
}
if ( $notEOF ) {
2016-04-20 16:56:34 +00:00
$blockLines [] = '' ;
2015-08-18 00:00:26 +00:00
$this -> moveToPreviousLine ();
2017-02-03 00:28:38 +00:00
} elseif ( ! $notEOF && ! $this -> isCurrentLineLastLineInDocument ()) {
$blockLines [] = '' ;
2015-08-18 00:00:26 +00:00
}
2015-11-17 21:42:33 +00:00
// folded style
if ( '>' === $style ) {
2016-04-20 16:56:34 +00:00
$text = '' ;
$previousLineIndented = false ;
$previousLineBlank = false ;
2017-04-13 14:53:35 +00:00
for ( $i = 0 , $blockLinesCount = count ( $blockLines ); $i < $blockLinesCount ; ++ $i ) {
2016-04-20 16:56:34 +00:00
if ( '' === $blockLines [ $i ]) {
$text .= " \n " ;
$previousLineIndented = false ;
$previousLineBlank = true ;
} elseif ( ' ' === $blockLines [ $i ][ 0 ]) {
$text .= " \n " . $blockLines [ $i ];
$previousLineIndented = true ;
$previousLineBlank = false ;
} elseif ( $previousLineIndented ) {
$text .= " \n " . $blockLines [ $i ];
$previousLineIndented = false ;
$previousLineBlank = false ;
} elseif ( $previousLineBlank || 0 === $i ) {
$text .= $blockLines [ $i ];
$previousLineIndented = false ;
$previousLineBlank = false ;
} else {
$text .= ' ' . $blockLines [ $i ];
$previousLineIndented = false ;
$previousLineBlank = false ;
}
}
} else {
$text = implode ( " \n " , $blockLines );
2015-08-18 00:00:26 +00:00
}
2015-11-17 21:42:33 +00:00
// deal with trailing newlines
if ( '' === $chomping ) {
2015-08-27 19:03:05 +00:00
$text = preg_replace ( '/\n+$/' , " \n " , $text );
2015-11-17 21:42:33 +00:00
} elseif ( '-' === $chomping ) {
2015-08-27 19:03:05 +00:00
$text = preg_replace ( '/\n+$/' , '' , $text );
2015-08-18 00:00:26 +00:00
}
return $text ;
}
/**
* Returns true if the next line is indented .
*
* @ return bool Returns true if the next line is indented , false otherwise
*/
private function isNextLineIndented ()
{
$currentIndentation = $this -> getCurrentLineIndentation ();
$EOF = ! $this -> moveToNextLine ();
while ( ! $EOF && $this -> isCurrentLineEmpty ()) {
$EOF = ! $this -> moveToNextLine ();
}
if ( $EOF ) {
return false ;
}
2017-04-13 14:53:35 +00:00
$ret = $this -> getCurrentLineIndentation () > $currentIndentation ;
2015-08-18 00:00:26 +00:00
$this -> moveToPreviousLine ();
return $ret ;
}
/**
* Returns true if the current line is blank or if it is a comment line .
*
* @ return bool Returns true if the current line is empty or if it is a comment line , false otherwise
*/
private function isCurrentLineEmpty ()
{
return $this -> isCurrentLineBlank () || $this -> isCurrentLineComment ();
}
/**
* Returns true if the current line is blank .
*
* @ return bool Returns true if the current line is blank , false otherwise
*/
private function isCurrentLineBlank ()
{
return '' == trim ( $this -> currentLine , ' ' );
}
/**
* Returns true if the current line is a comment line .
*
* @ return bool Returns true if the current line is a comment line , false otherwise
*/
private function isCurrentLineComment ()
{
//checking explicitly the first char of the trim is faster than loops or strpos
$ltrimmedLine = ltrim ( $this -> currentLine , ' ' );
2016-04-20 16:56:34 +00:00
return '' !== $ltrimmedLine && $ltrimmedLine [ 0 ] === '#' ;
2015-08-18 00:00:26 +00:00
}
2017-02-03 00:28:38 +00:00
private function isCurrentLineLastLineInDocument ()
{
return ( $this -> offset + $this -> currentLineNb ) >= ( $this -> totalNumberOfLines - 1 );
}
2015-08-18 00:00:26 +00:00
/**
* Cleanups a YAML string to be parsed .
*
* @ param string $value The input YAML string
*
* @ return string A cleaned up YAML string
*/
private function cleanup ( $value )
{
$value = str_replace ( array ( " \r \n " , " \r " ), " \n " , $value );
// strip YAML header
$count = 0 ;
$value = preg_replace ( '#^\%YAML[: ][\d\.]+.*\n#u' , '' , $value , - 1 , $count );
$this -> offset += $count ;
// remove leading comments
$trimmedValue = preg_replace ( '#^(\#.*?\n)+#s' , '' , $value , - 1 , $count );
if ( $count == 1 ) {
// items have been removed, update the offset
$this -> offset += substr_count ( $value , " \n " ) - substr_count ( $trimmedValue , " \n " );
$value = $trimmedValue ;
}
// remove start of the document marker (---)
$trimmedValue = preg_replace ( '#^\-\-\-.*?\n#s' , '' , $value , - 1 , $count );
if ( $count == 1 ) {
// items have been removed, update the offset
$this -> offset += substr_count ( $value , " \n " ) - substr_count ( $trimmedValue , " \n " );
$value = $trimmedValue ;
// remove end of the document marker (...)
2015-08-27 19:03:05 +00:00
$value = preg_replace ( '#\.\.\.\s*$#' , '' , $value );
2015-08-18 00:00:26 +00:00
}
return $value ;
}
/**
* Returns true if the next line starts unindented collection .
*
* @ return bool Returns true if the next line starts unindented collection , false otherwise
*/
private function isNextLineUnIndentedCollection ()
{
$currentIndentation = $this -> getCurrentLineIndentation ();
$notEOF = $this -> moveToNextLine ();
while ( $notEOF && $this -> isCurrentLineEmpty ()) {
$notEOF = $this -> moveToNextLine ();
}
if ( false === $notEOF ) {
return false ;
}
2017-04-13 14:53:35 +00:00
$ret = $this -> getCurrentLineIndentation () === $currentIndentation && $this -> isStringUnIndentedCollectionItem ();
2015-08-18 00:00:26 +00:00
$this -> moveToPreviousLine ();
return $ret ;
}
/**
* Returns true if the string is un - indented collection item .
*
* @ return bool Returns true if the string is un - indented collection item , false otherwise
*/
private function isStringUnIndentedCollectionItem ()
{
2017-02-03 00:28:38 +00:00
return '-' === rtrim ( $this -> currentLine ) || 0 === strpos ( $this -> currentLine , '- ' );
2016-04-20 16:56:34 +00:00
}
/**
* Tests whether or not the current line is the header of a block scalar .
*
* @ return bool
*/
private function isBlockScalarHeader ()
{
2017-04-13 14:53:35 +00:00
return ( bool ) self :: preg_match ( '~' . self :: BLOCK_SCALAR_HEADER_PATTERN . '$~' , $this -> currentLine );
}
/**
* A local wrapper for `preg_match` which will throw a ParseException if there
* is an internal error in the PCRE engine .
*
* This avoids us needing to check for " false " every time PCRE is used
* in the YAML engine
*
* @ throws ParseException on a PCRE internal error
*
* @ see preg_last_error ()
*
* @ internal
*/
public static function preg_match ( $pattern , $subject , & $matches = null , $flags = 0 , $offset = 0 )
{
if ( false === $ret = preg_match ( $pattern , $subject , $matches , $flags , $offset )) {
switch ( preg_last_error ()) {
case PREG_INTERNAL_ERROR :
$error = 'Internal PCRE error.' ;
break ;
case PREG_BACKTRACK_LIMIT_ERROR :
$error = 'pcre.backtrack_limit reached.' ;
break ;
case PREG_RECURSION_LIMIT_ERROR :
$error = 'pcre.recursion_limit reached.' ;
break ;
case PREG_BAD_UTF8_ERROR :
$error = 'Malformed UTF-8 data.' ;
break ;
case PREG_BAD_UTF8_OFFSET_ERROR :
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.' ;
break ;
default :
$error = 'Error.' ;
}
throw new ParseException ( $error );
}
return $ret ;
2015-08-18 00:00:26 +00:00
}
}