PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/yaml/Parser.php

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