Parser.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Yaml;
  11. use Symfony\Component\Yaml\Exception\ParseException;
  12. /**
  13. * Parser parses YAML strings to convert them to PHP arrays.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Parser
  18. {
  19. const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?';
  20. // BC - wrongly named
  21. const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
  22. private $offset = 0;
  23. private $totalNumberOfLines;
  24. private $lines = array();
  25. private $currentLineNb = -1;
  26. private $currentLine = '';
  27. private $refs = array();
  28. private $skippedLineNumbers = array();
  29. private $locallySkippedLineNumbers = array();
  30. /**
  31. * @param int $offset The offset of YAML document (used for line numbers in error messages)
  32. * @param int|null $totalNumberOfLines The overall number of lines being parsed
  33. * @param int[] $skippedLineNumbers Number of comment lines that have been skipped by the parser
  34. */
  35. public function __construct($offset = 0, $totalNumberOfLines = null, array $skippedLineNumbers = array())
  36. {
  37. $this->offset = $offset;
  38. $this->totalNumberOfLines = $totalNumberOfLines;
  39. $this->skippedLineNumbers = $skippedLineNumbers;
  40. }
  41. /**
  42. * Parses a YAML string to a PHP value.
  43. *
  44. * @param string $value A YAML string
  45. * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types (a PHP resource or object), false otherwise
  46. * @param bool $objectSupport True if object support is enabled, false otherwise
  47. * @param bool $objectForMap True if maps should return a stdClass instead of array()
  48. *
  49. * @return mixed A PHP value
  50. *
  51. * @throws ParseException If the YAML is not valid
  52. */
  53. public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
  54. {
  55. if (false === preg_match('//u', $value)) {
  56. throw new ParseException('The YAML value does not appear to be valid UTF-8.');
  57. }
  58. $this->refs = array();
  59. $mbEncoding = null;
  60. $e = null;
  61. $data = null;
  62. if (2 /* MB_OVERLOAD_STRING */ & (int) ini_get('mbstring.func_overload')) {
  63. $mbEncoding = mb_internal_encoding();
  64. mb_internal_encoding('UTF-8');
  65. }
  66. try {
  67. $data = $this->doParse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  68. } catch (\Exception $e) {
  69. } catch (\Throwable $e) {
  70. }
  71. if (null !== $mbEncoding) {
  72. mb_internal_encoding($mbEncoding);
  73. }
  74. $this->lines = array();
  75. $this->currentLine = '';
  76. $this->refs = array();
  77. $this->skippedLineNumbers = array();
  78. $this->locallySkippedLineNumbers = array();
  79. if (null !== $e) {
  80. throw $e;
  81. }
  82. return $data;
  83. }
  84. private function doParse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
  85. {
  86. $this->currentLineNb = -1;
  87. $this->currentLine = '';
  88. $value = $this->cleanup($value);
  89. $this->lines = explode("\n", $value);
  90. $this->locallySkippedLineNumbers = array();
  91. if (null === $this->totalNumberOfLines) {
  92. $this->totalNumberOfLines = count($this->lines);
  93. }
  94. $data = array();
  95. $context = null;
  96. $allowOverwrite = false;
  97. while ($this->moveToNextLine()) {
  98. if ($this->isCurrentLineEmpty()) {
  99. continue;
  100. }
  101. // tab?
  102. if ("\t" === $this->currentLine[0]) {
  103. throw new ParseException('A YAML file cannot contain tabs as indentation.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  104. }
  105. $isRef = $mergeNode = false;
  106. if (self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
  107. if ($context && 'mapping' == $context) {
  108. throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  109. }
  110. $context = 'sequence';
  111. if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  112. $isRef = $matches['ref'];
  113. $values['value'] = $matches['value'];
  114. }
  115. // array
  116. if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  117. $data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
  118. } else {
  119. if (isset($values['leadspaces'])
  120. && self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+))?$#u', rtrim($values['value']), $matches)
  121. ) {
  122. // this is a compact notation element, add to next block and parse
  123. $block = $values['value'];
  124. if ($this->isNextLineIndented()) {
  125. $block .= "\n".$this->getNextEmbedBlock($this->getCurrentLineIndentation() + strlen($values['leadspaces']) + 1);
  126. }
  127. $data[] = $this->parseBlock($this->getRealCurrentLineNb(), $block, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  128. } else {
  129. $data[] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
  130. }
  131. }
  132. if ($isRef) {
  133. $this->refs[$isRef] = end($data);
  134. }
  135. } elseif (
  136. self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
  137. && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
  138. ) {
  139. if ($context && 'sequence' == $context) {
  140. throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
  141. }
  142. $context = 'mapping';
  143. // force correct settings
  144. Inline::parse(null, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  145. try {
  146. $key = Inline::parseScalar($values['key']);
  147. } catch (ParseException $e) {
  148. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  149. $e->setSnippet($this->currentLine);
  150. throw $e;
  151. }
  152. // Convert float keys to strings, to avoid being converted to integers by PHP
  153. if (is_float($key)) {
  154. $key = (string) $key;
  155. }
  156. if ('<<' === $key && (!isset($values['value']) || !self::preg_match('#^&(?P<ref>[^ ]+)#u', $values['value'], $refMatches))) {
  157. $mergeNode = true;
  158. $allowOverwrite = true;
  159. if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
  160. $refName = substr($values['value'], 1);
  161. if (!array_key_exists($refName, $this->refs)) {
  162. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  163. }
  164. $refValue = $this->refs[$refName];
  165. if (!is_array($refValue)) {
  166. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  167. }
  168. $data += $refValue; // array union
  169. } else {
  170. if (isset($values['value']) && '' !== $values['value']) {
  171. $value = $values['value'];
  172. } else {
  173. $value = $this->getNextEmbedBlock();
  174. }
  175. $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  176. if (!is_array($parsed)) {
  177. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  178. }
  179. if (isset($parsed[0])) {
  180. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  181. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  182. // in the sequence override keys specified in later mapping nodes.
  183. foreach ($parsed as $parsedItem) {
  184. if (!is_array($parsedItem)) {
  185. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem);
  186. }
  187. $data += $parsedItem; // array union
  188. }
  189. } else {
  190. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  191. // current mapping, unless the key already exists in it.
  192. $data += $parsed; // array union
  193. }
  194. }
  195. } elseif ('<<' !== $key && isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  196. $isRef = $matches['ref'];
  197. $values['value'] = $matches['value'];
  198. }
  199. if ($mergeNode) {
  200. // Merge keys
  201. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#') || '<<' === $key) {
  202. // hash
  203. // if next line is less indented or equal, then it means that the current value is null
  204. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  205. // Spec: Keys MUST be unique; first one wins.
  206. // But overwriting is allowed when a merge node is used in current block.
  207. if ($allowOverwrite || !isset($data[$key])) {
  208. $data[$key] = null;
  209. }
  210. } else {
  211. $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport, $objectForMap);
  212. if ('<<' === $key) {
  213. $this->refs[$refMatches['ref']] = $value;
  214. $data += $value;
  215. } elseif ($allowOverwrite || !isset($data[$key])) {
  216. // Spec: Keys MUST be unique; first one wins.
  217. // But overwriting is allowed when a merge node is used in current block.
  218. $data[$key] = $value;
  219. }
  220. }
  221. } else {
  222. $value = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport, $objectForMap, $context);
  223. // Spec: Keys MUST be unique; first one wins.
  224. // But overwriting is allowed when a merge node is used in current block.
  225. if ($allowOverwrite || !isset($data[$key])) {
  226. $data[$key] = $value;
  227. }
  228. }
  229. if ($isRef) {
  230. $this->refs[$isRef] = $data[$key];
  231. }
  232. } else {
  233. // multiple documents are not supported
  234. if ('---' === $this->currentLine) {
  235. throw new ParseException('Multiple documents are not supported.', $this->currentLineNb + 1, $this->currentLine);
  236. }
  237. // 1-liner optionally followed by newline(s)
  238. if (is_string($value) && $this->lines[0] === trim($value)) {
  239. try {
  240. $value = Inline::parse($this->lines[0], $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  241. } catch (ParseException $e) {
  242. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  243. $e->setSnippet($this->currentLine);
  244. throw $e;
  245. }
  246. return $value;
  247. }
  248. throw new ParseException('Unable to parse.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  249. }
  250. }
  251. if ($objectForMap && !is_object($data) && 'mapping' === $context) {
  252. $object = new \stdClass();
  253. foreach ($data as $key => $value) {
  254. $object->$key = $value;
  255. }
  256. $data = $object;
  257. }
  258. return empty($data) ? null : $data;
  259. }
  260. private function parseBlock($offset, $yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap)
  261. {
  262. $skippedLineNumbers = $this->skippedLineNumbers;
  263. foreach ($this->locallySkippedLineNumbers as $lineNumber) {
  264. if ($lineNumber < $offset) {
  265. continue;
  266. }
  267. $skippedLineNumbers[] = $lineNumber;
  268. }
  269. $parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
  270. $parser->refs = &$this->refs;
  271. return $parser->doParse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap);
  272. }
  273. /**
  274. * Returns the current line number (takes the offset into account).
  275. *
  276. * @return int The current line number
  277. */
  278. private function getRealCurrentLineNb()
  279. {
  280. $realCurrentLineNumber = $this->currentLineNb + $this->offset;
  281. foreach ($this->skippedLineNumbers as $skippedLineNumber) {
  282. if ($skippedLineNumber > $realCurrentLineNumber) {
  283. break;
  284. }
  285. ++$realCurrentLineNumber;
  286. }
  287. return $realCurrentLineNumber;
  288. }
  289. /**
  290. * Returns the current line indentation.
  291. *
  292. * @return int The current line indentation
  293. */
  294. private function getCurrentLineIndentation()
  295. {
  296. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  297. }
  298. /**
  299. * Returns the next embed block of YAML.
  300. *
  301. * @param int $indentation The indent level at which the block is to be read, or null for default
  302. * @param bool $inSequence True if the enclosing data structure is a sequence
  303. *
  304. * @return string A YAML string
  305. *
  306. * @throws ParseException When indentation problem are detected
  307. */
  308. private function getNextEmbedBlock($indentation = null, $inSequence = false)
  309. {
  310. $oldLineIndentation = $this->getCurrentLineIndentation();
  311. $blockScalarIndentations = array();
  312. if ($this->isBlockScalarHeader()) {
  313. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  314. }
  315. if (!$this->moveToNextLine()) {
  316. return;
  317. }
  318. if (null === $indentation) {
  319. $newIndent = $this->getCurrentLineIndentation();
  320. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  321. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  322. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  323. }
  324. } else {
  325. $newIndent = $indentation;
  326. }
  327. $data = array();
  328. if ($this->getCurrentLineIndentation() >= $newIndent) {
  329. $data[] = substr($this->currentLine, $newIndent);
  330. } else {
  331. $this->moveToPreviousLine();
  332. return;
  333. }
  334. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  335. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  336. // and therefore no nested list or mapping
  337. $this->moveToPreviousLine();
  338. return;
  339. }
  340. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  341. if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
  342. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  343. }
  344. $previousLineIndentation = $this->getCurrentLineIndentation();
  345. while ($this->moveToNextLine()) {
  346. $indent = $this->getCurrentLineIndentation();
  347. // terminate all block scalars that are more indented than the current line
  348. if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && '' !== trim($this->currentLine)) {
  349. foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
  350. if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
  351. unset($blockScalarIndentations[$key]);
  352. }
  353. }
  354. }
  355. if (empty($blockScalarIndentations) && !$this->isCurrentLineComment() && $this->isBlockScalarHeader()) {
  356. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  357. }
  358. $previousLineIndentation = $indent;
  359. if ($isItUnindentedCollection && !$this->isCurrentLineEmpty() && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  360. $this->moveToPreviousLine();
  361. break;
  362. }
  363. if ($this->isCurrentLineBlank()) {
  364. $data[] = substr($this->currentLine, $newIndent);
  365. continue;
  366. }
  367. // we ignore "comment" lines only when we are not inside a scalar block
  368. if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
  369. // remember ignored comment lines (they are used later in nested
  370. // parser calls to determine real line numbers)
  371. //
  372. // CAUTION: beware to not populate the global property here as it
  373. // will otherwise influence the getRealCurrentLineNb() call here
  374. // for consecutive comment lines and subsequent embedded blocks
  375. $this->locallySkippedLineNumbers[] = $this->getRealCurrentLineNb();
  376. continue;
  377. }
  378. if ($indent >= $newIndent) {
  379. $data[] = substr($this->currentLine, $newIndent);
  380. } elseif (0 == $indent) {
  381. $this->moveToPreviousLine();
  382. break;
  383. } else {
  384. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  385. }
  386. }
  387. return implode("\n", $data);
  388. }
  389. /**
  390. * Moves the parser to the next line.
  391. *
  392. * @return bool
  393. */
  394. private function moveToNextLine()
  395. {
  396. if ($this->currentLineNb >= count($this->lines) - 1) {
  397. return false;
  398. }
  399. $this->currentLine = $this->lines[++$this->currentLineNb];
  400. return true;
  401. }
  402. /**
  403. * Moves the parser to the previous line.
  404. *
  405. * @return bool
  406. */
  407. private function moveToPreviousLine()
  408. {
  409. if ($this->currentLineNb < 1) {
  410. return false;
  411. }
  412. $this->currentLine = $this->lines[--$this->currentLineNb];
  413. return true;
  414. }
  415. /**
  416. * Parses a YAML value.
  417. *
  418. * @param string $value A YAML value
  419. * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise
  420. * @param bool $objectSupport True if object support is enabled, false otherwise
  421. * @param bool $objectForMap True if maps should return a stdClass instead of array()
  422. * @param string $context The parser context (either sequence or mapping)
  423. *
  424. * @return mixed A PHP value
  425. *
  426. * @throws ParseException When reference does not exist
  427. */
  428. private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context)
  429. {
  430. if (0 === strpos($value, '*')) {
  431. if (false !== $pos = strpos($value, '#')) {
  432. $value = substr($value, 1, $pos - 2);
  433. } else {
  434. $value = substr($value, 1);
  435. }
  436. if (!array_key_exists($value, $this->refs)) {
  437. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
  438. }
  439. return $this->refs[$value];
  440. }
  441. if (self::preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  442. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  443. return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
  444. }
  445. try {
  446. $parsedValue = Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs);
  447. if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  448. @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);
  449. // to be thrown in 3.0
  450. // throw new ParseException('A colon cannot be used in an unquoted mapping value.');
  451. }
  452. return $parsedValue;
  453. } catch (ParseException $e) {
  454. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  455. $e->setSnippet($this->currentLine);
  456. throw $e;
  457. }
  458. }
  459. /**
  460. * Parses a block scalar.
  461. *
  462. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  463. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  464. * @param int $indentation The indentation indicator that was used to begin this block scalar
  465. *
  466. * @return string The text value
  467. */
  468. private function parseBlockScalar($style, $chomping = '', $indentation = 0)
  469. {
  470. $notEOF = $this->moveToNextLine();
  471. if (!$notEOF) {
  472. return '';
  473. }
  474. $isCurrentLineBlank = $this->isCurrentLineBlank();
  475. $blockLines = array();
  476. // leading blank lines are consumed before determining indentation
  477. while ($notEOF && $isCurrentLineBlank) {
  478. // newline only if not EOF
  479. if ($notEOF = $this->moveToNextLine()) {
  480. $blockLines[] = '';
  481. $isCurrentLineBlank = $this->isCurrentLineBlank();
  482. }
  483. }
  484. // determine indentation if not specified
  485. if (0 === $indentation) {
  486. if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
  487. $indentation = strlen($matches[0]);
  488. }
  489. }
  490. if ($indentation > 0) {
  491. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  492. while (
  493. $notEOF && (
  494. $isCurrentLineBlank ||
  495. self::preg_match($pattern, $this->currentLine, $matches)
  496. )
  497. ) {
  498. if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
  499. $blockLines[] = substr($this->currentLine, $indentation);
  500. } elseif ($isCurrentLineBlank) {
  501. $blockLines[] = '';
  502. } else {
  503. $blockLines[] = $matches[1];
  504. }
  505. // newline only if not EOF
  506. if ($notEOF = $this->moveToNextLine()) {
  507. $isCurrentLineBlank = $this->isCurrentLineBlank();
  508. }
  509. }
  510. } elseif ($notEOF) {
  511. $blockLines[] = '';
  512. }
  513. if ($notEOF) {
  514. $blockLines[] = '';
  515. $this->moveToPreviousLine();
  516. } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
  517. $blockLines[] = '';
  518. }
  519. // folded style
  520. if ('>' === $style) {
  521. $text = '';
  522. $previousLineIndented = false;
  523. $previousLineBlank = false;
  524. for ($i = 0, $blockLinesCount = count($blockLines); $i < $blockLinesCount; ++$i) {
  525. if ('' === $blockLines[$i]) {
  526. $text .= "\n";
  527. $previousLineIndented = false;
  528. $previousLineBlank = true;
  529. } elseif (' ' === $blockLines[$i][0]) {
  530. $text .= "\n".$blockLines[$i];
  531. $previousLineIndented = true;
  532. $previousLineBlank = false;
  533. } elseif ($previousLineIndented) {
  534. $text .= "\n".$blockLines[$i];
  535. $previousLineIndented = false;
  536. $previousLineBlank = false;
  537. } elseif ($previousLineBlank || 0 === $i) {
  538. $text .= $blockLines[$i];
  539. $previousLineIndented = false;
  540. $previousLineBlank = false;
  541. } else {
  542. $text .= ' '.$blockLines[$i];
  543. $previousLineIndented = false;
  544. $previousLineBlank = false;
  545. }
  546. }
  547. } else {
  548. $text = implode("\n", $blockLines);
  549. }
  550. // deal with trailing newlines
  551. if ('' === $chomping) {
  552. $text = preg_replace('/\n+$/', "\n", $text);
  553. } elseif ('-' === $chomping) {
  554. $text = preg_replace('/\n+$/', '', $text);
  555. }
  556. return $text;
  557. }
  558. /**
  559. * Returns true if the next line is indented.
  560. *
  561. * @return bool Returns true if the next line is indented, false otherwise
  562. */
  563. private function isNextLineIndented()
  564. {
  565. $currentIndentation = $this->getCurrentLineIndentation();
  566. $EOF = !$this->moveToNextLine();
  567. while (!$EOF && $this->isCurrentLineEmpty()) {
  568. $EOF = !$this->moveToNextLine();
  569. }
  570. if ($EOF) {
  571. return false;
  572. }
  573. $ret = $this->getCurrentLineIndentation() > $currentIndentation;
  574. $this->moveToPreviousLine();
  575. return $ret;
  576. }
  577. /**
  578. * Returns true if the current line is blank or if it is a comment line.
  579. *
  580. * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
  581. */
  582. private function isCurrentLineEmpty()
  583. {
  584. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  585. }
  586. /**
  587. * Returns true if the current line is blank.
  588. *
  589. * @return bool Returns true if the current line is blank, false otherwise
  590. */
  591. private function isCurrentLineBlank()
  592. {
  593. return '' == trim($this->currentLine, ' ');
  594. }
  595. /**
  596. * Returns true if the current line is a comment line.
  597. *
  598. * @return bool Returns true if the current line is a comment line, false otherwise
  599. */
  600. private function isCurrentLineComment()
  601. {
  602. //checking explicitly the first char of the trim is faster than loops or strpos
  603. $ltrimmedLine = ltrim($this->currentLine, ' ');
  604. return '' !== $ltrimmedLine && '#' === $ltrimmedLine[0];
  605. }
  606. private function isCurrentLineLastLineInDocument()
  607. {
  608. return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
  609. }
  610. /**
  611. * Cleanups a YAML string to be parsed.
  612. *
  613. * @param string $value The input YAML string
  614. *
  615. * @return string A cleaned up YAML string
  616. */
  617. private function cleanup($value)
  618. {
  619. $value = str_replace(array("\r\n", "\r"), "\n", $value);
  620. // strip YAML header
  621. $count = 0;
  622. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
  623. $this->offset += $count;
  624. // remove leading comments
  625. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  626. if (1 == $count) {
  627. // items have been removed, update the offset
  628. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  629. $value = $trimmedValue;
  630. }
  631. // remove start of the document marker (---)
  632. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  633. if (1 == $count) {
  634. // items have been removed, update the offset
  635. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  636. $value = $trimmedValue;
  637. // remove end of the document marker (...)
  638. $value = preg_replace('#\.\.\.\s*$#', '', $value);
  639. }
  640. return $value;
  641. }
  642. /**
  643. * Returns true if the next line starts unindented collection.
  644. *
  645. * @return bool Returns true if the next line starts unindented collection, false otherwise
  646. */
  647. private function isNextLineUnIndentedCollection()
  648. {
  649. $currentIndentation = $this->getCurrentLineIndentation();
  650. $notEOF = $this->moveToNextLine();
  651. while ($notEOF && $this->isCurrentLineEmpty()) {
  652. $notEOF = $this->moveToNextLine();
  653. }
  654. if (false === $notEOF) {
  655. return false;
  656. }
  657. $ret = $this->getCurrentLineIndentation() === $currentIndentation && $this->isStringUnIndentedCollectionItem();
  658. $this->moveToPreviousLine();
  659. return $ret;
  660. }
  661. /**
  662. * Returns true if the string is un-indented collection item.
  663. *
  664. * @return bool Returns true if the string is un-indented collection item, false otherwise
  665. */
  666. private function isStringUnIndentedCollectionItem()
  667. {
  668. return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
  669. }
  670. /**
  671. * Tests whether or not the current line is the header of a block scalar.
  672. *
  673. * @return bool
  674. */
  675. private function isBlockScalarHeader()
  676. {
  677. return (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
  678. }
  679. /**
  680. * A local wrapper for `preg_match` which will throw a ParseException if there
  681. * is an internal error in the PCRE engine.
  682. *
  683. * This avoids us needing to check for "false" every time PCRE is used
  684. * in the YAML engine
  685. *
  686. * @throws ParseException on a PCRE internal error
  687. *
  688. * @see preg_last_error()
  689. *
  690. * @internal
  691. */
  692. public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
  693. {
  694. if (false === $ret = preg_match($pattern, $subject, $matches, $flags, $offset)) {
  695. switch (preg_last_error()) {
  696. case PREG_INTERNAL_ERROR:
  697. $error = 'Internal PCRE error.';
  698. break;
  699. case PREG_BACKTRACK_LIMIT_ERROR:
  700. $error = 'pcre.backtrack_limit reached.';
  701. break;
  702. case PREG_RECURSION_LIMIT_ERROR:
  703. $error = 'pcre.recursion_limit reached.';
  704. break;
  705. case PREG_BAD_UTF8_ERROR:
  706. $error = 'Malformed UTF-8 data.';
  707. break;
  708. case PREG_BAD_UTF8_OFFSET_ERROR:
  709. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  710. break;
  711. default:
  712. $error = 'Error.';
  713. }
  714. throw new ParseException($error);
  715. }
  716. return $ret;
  717. }
  718. }