Parser.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. $key = Inline::parseScalar($values['key']);
  138. } catch (ParseException $e) {
  139. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  140. $e->setSnippet($this->currentLine);
  141. throw $e;
  142. }
  143. // Convert float keys to strings, to avoid being converted to integers by PHP
  144. if (is_float($key)) {
  145. $key = (string) $key;
  146. }
  147. if ('<<' === $key) {
  148. $mergeNode = true;
  149. $allowOverwrite = true;
  150. if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
  151. $refName = substr($values['value'], 1);
  152. if (!array_key_exists($refName, $this->refs)) {
  153. throw new ParseException(sprintf('Reference "%s" does not exist.', $refName), $this->getRealCurrentLineNb() + 1, $this->currentLine);
  154. }
  155. $refValue = $this->refs[$refName];
  156. if (!is_array($refValue)) {
  157. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  158. }
  159. foreach ($refValue as $key => $value) {
  160. if (!isset($data[$key])) {
  161. $data[$key] = $value;
  162. }
  163. }
  164. } else {
  165. if (isset($values['value']) && $values['value'] !== '') {
  166. $value = $values['value'];
  167. } else {
  168. $value = $this->getNextEmbedBlock();
  169. }
  170. $parsed = $this->parseBlock($this->getRealCurrentLineNb() + 1, $value, $flags);
  171. if (!is_array($parsed)) {
  172. throw new ParseException('YAML merge keys used with a scalar value instead of an array.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  173. }
  174. if (isset($parsed[0])) {
  175. // If the value associated with the merge key is a sequence, then this sequence is expected to contain mapping nodes
  176. // and each of these nodes is merged in turn according to its order in the sequence. Keys in mapping nodes earlier
  177. // in the sequence override keys specified in later mapping nodes.
  178. foreach ($parsed as $parsedItem) {
  179. if (!is_array($parsedItem)) {
  180. throw new ParseException('Merge items must be arrays.', $this->getRealCurrentLineNb() + 1, $parsedItem);
  181. }
  182. foreach ($parsedItem as $key => $value) {
  183. if (!isset($data[$key])) {
  184. $data[$key] = $value;
  185. }
  186. }
  187. }
  188. } else {
  189. // If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the
  190. // current mapping, unless the key already exists in it.
  191. foreach ($parsed as $key => $value) {
  192. if (!isset($data[$key])) {
  193. $data[$key] = $value;
  194. }
  195. }
  196. }
  197. }
  198. } elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
  199. $isRef = $matches['ref'];
  200. $values['value'] = $matches['value'];
  201. }
  202. if ($mergeNode) {
  203. // Merge keys
  204. } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
  205. // hash
  206. // if next line is less indented or equal, then it means that the current value is null
  207. if (!$this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
  208. // Spec: Keys MUST be unique; first one wins.
  209. // But overwriting is allowed when a merge node is used in current block.
  210. if ($allowOverwrite || !isset($data[$key])) {
  211. $data[$key] = null;
  212. }
  213. } else {
  214. $value = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(), $flags);
  215. // Spec: Keys MUST be unique; first one wins.
  216. // But overwriting is allowed when a merge node is used in current block.
  217. if ($allowOverwrite || !isset($data[$key])) {
  218. $data[$key] = $value;
  219. }
  220. }
  221. } else {
  222. $value = $this->parseValue($values['value'], $flags, $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], $flags, $this->refs);
  241. } catch (ParseException $e) {
  242. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  243. $e->setSnippet($this->currentLine);
  244. throw $e;
  245. }
  246. if (isset($mbEncoding)) {
  247. mb_internal_encoding($mbEncoding);
  248. }
  249. return $value;
  250. }
  251. switch (preg_last_error()) {
  252. case PREG_INTERNAL_ERROR:
  253. $error = 'Internal PCRE error.';
  254. break;
  255. case PREG_BACKTRACK_LIMIT_ERROR:
  256. $error = 'pcre.backtrack_limit reached.';
  257. break;
  258. case PREG_RECURSION_LIMIT_ERROR:
  259. $error = 'pcre.recursion_limit reached.';
  260. break;
  261. case PREG_BAD_UTF8_ERROR:
  262. $error = 'Malformed UTF-8 data.';
  263. break;
  264. case PREG_BAD_UTF8_OFFSET_ERROR:
  265. $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
  266. break;
  267. default:
  268. $error = 'Unable to parse.';
  269. }
  270. throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
  271. }
  272. }
  273. if (isset($mbEncoding)) {
  274. mb_internal_encoding($mbEncoding);
  275. }
  276. if (Yaml::PARSE_OBJECT_FOR_MAP & $flags && !is_object($data) && 'mapping' === $context) {
  277. $object = new \stdClass();
  278. foreach ($data as $key => $value) {
  279. $object->$key = $value;
  280. }
  281. $data = $object;
  282. }
  283. return empty($data) ? null : $data;
  284. }
  285. private function parseBlock($offset, $yaml, $flags)
  286. {
  287. $skippedLineNumbers = $this->skippedLineNumbers;
  288. foreach ($this->locallySkippedLineNumbers as $lineNumber) {
  289. if ($lineNumber < $offset) {
  290. continue;
  291. }
  292. $skippedLineNumbers[] = $lineNumber;
  293. }
  294. $parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers);
  295. $parser->refs = &$this->refs;
  296. return $parser->parse($yaml, $flags);
  297. }
  298. /**
  299. * Returns the current line number (takes the offset into account).
  300. *
  301. * @return int The current line number
  302. */
  303. private function getRealCurrentLineNb()
  304. {
  305. $realCurrentLineNumber = $this->currentLineNb + $this->offset;
  306. foreach ($this->skippedLineNumbers as $skippedLineNumber) {
  307. if ($skippedLineNumber > $realCurrentLineNumber) {
  308. break;
  309. }
  310. ++$realCurrentLineNumber;
  311. }
  312. return $realCurrentLineNumber;
  313. }
  314. /**
  315. * Returns the current line indentation.
  316. *
  317. * @return int The current line indentation
  318. */
  319. private function getCurrentLineIndentation()
  320. {
  321. return strlen($this->currentLine) - strlen(ltrim($this->currentLine, ' '));
  322. }
  323. /**
  324. * Returns the next embed block of YAML.
  325. *
  326. * @param int $indentation The indent level at which the block is to be read, or null for default
  327. * @param bool $inSequence True if the enclosing data structure is a sequence
  328. *
  329. * @return string A YAML string
  330. *
  331. * @throws ParseException When indentation problem are detected
  332. */
  333. private function getNextEmbedBlock($indentation = null, $inSequence = false)
  334. {
  335. $oldLineIndentation = $this->getCurrentLineIndentation();
  336. $blockScalarIndentations = array();
  337. if ($this->isBlockScalarHeader()) {
  338. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  339. }
  340. if (!$this->moveToNextLine()) {
  341. return;
  342. }
  343. if (null === $indentation) {
  344. $newIndent = $this->getCurrentLineIndentation();
  345. $unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem();
  346. if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
  347. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  348. }
  349. } else {
  350. $newIndent = $indentation;
  351. }
  352. $data = array();
  353. if ($this->getCurrentLineIndentation() >= $newIndent) {
  354. $data[] = substr($this->currentLine, $newIndent);
  355. } else {
  356. $this->moveToPreviousLine();
  357. return;
  358. }
  359. if ($inSequence && $oldLineIndentation === $newIndent && isset($data[0][0]) && '-' === $data[0][0]) {
  360. // the previous line contained a dash but no item content, this line is a sequence item with the same indentation
  361. // and therefore no nested list or mapping
  362. $this->moveToPreviousLine();
  363. return;
  364. }
  365. $isItUnindentedCollection = $this->isStringUnIndentedCollectionItem();
  366. if (empty($blockScalarIndentations) && $this->isBlockScalarHeader()) {
  367. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  368. }
  369. $previousLineIndentation = $this->getCurrentLineIndentation();
  370. while ($this->moveToNextLine()) {
  371. $indent = $this->getCurrentLineIndentation();
  372. // terminate all block scalars that are more indented than the current line
  373. if (!empty($blockScalarIndentations) && $indent < $previousLineIndentation && trim($this->currentLine) !== '') {
  374. foreach ($blockScalarIndentations as $key => $blockScalarIndentation) {
  375. if ($blockScalarIndentation >= $this->getCurrentLineIndentation()) {
  376. unset($blockScalarIndentations[$key]);
  377. }
  378. }
  379. }
  380. if (empty($blockScalarIndentations) && !$this->isCurrentLineComment() && $this->isBlockScalarHeader()) {
  381. $blockScalarIndentations[] = $this->getCurrentLineIndentation();
  382. }
  383. $previousLineIndentation = $indent;
  384. if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem() && $newIndent === $indent) {
  385. $this->moveToPreviousLine();
  386. break;
  387. }
  388. if ($this->isCurrentLineBlank()) {
  389. $data[] = substr($this->currentLine, $newIndent);
  390. continue;
  391. }
  392. // we ignore "comment" lines only when we are not inside a scalar block
  393. if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
  394. // remember ignored comment lines (they are used later in nested
  395. // parser calls to determine real line numbers)
  396. //
  397. // CAUTION: beware to not populate the global property here as it
  398. // will otherwise influence the getRealCurrentLineNb() call here
  399. // for consecutive comment lines and subsequent embedded blocks
  400. $this->locallySkippedLineNumbers[] = $this->getRealCurrentLineNb();
  401. continue;
  402. }
  403. if ($indent >= $newIndent) {
  404. $data[] = substr($this->currentLine, $newIndent);
  405. } elseif (0 == $indent) {
  406. $this->moveToPreviousLine();
  407. break;
  408. } else {
  409. throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
  410. }
  411. }
  412. return implode("\n", $data);
  413. }
  414. /**
  415. * Moves the parser to the next line.
  416. *
  417. * @return bool
  418. */
  419. private function moveToNextLine()
  420. {
  421. if ($this->currentLineNb >= count($this->lines) - 1) {
  422. return false;
  423. }
  424. $this->currentLine = $this->lines[++$this->currentLineNb];
  425. return true;
  426. }
  427. /**
  428. * Moves the parser to the previous line.
  429. *
  430. * @return bool
  431. */
  432. private function moveToPreviousLine()
  433. {
  434. if ($this->currentLineNb < 1) {
  435. return false;
  436. }
  437. $this->currentLine = $this->lines[--$this->currentLineNb];
  438. return true;
  439. }
  440. /**
  441. * Parses a YAML value.
  442. *
  443. * @param string $value A YAML value
  444. * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior
  445. * @param string $context The parser context (either sequence or mapping)
  446. *
  447. * @return mixed A PHP value
  448. *
  449. * @throws ParseException When reference does not exist
  450. */
  451. private function parseValue($value, $flags, $context)
  452. {
  453. if (0 === strpos($value, '*')) {
  454. if (false !== $pos = strpos($value, '#')) {
  455. $value = substr($value, 1, $pos - 2);
  456. } else {
  457. $value = substr($value, 1);
  458. }
  459. if (!array_key_exists($value, $this->refs)) {
  460. throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine);
  461. }
  462. return $this->refs[$value];
  463. }
  464. if (preg_match('/^'.self::TAG_PATTERN.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
  465. $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
  466. $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
  467. if (isset($matches['tag']) && '!!binary' === $matches['tag']) {
  468. return Inline::evaluateBinaryScalar($data);
  469. }
  470. return $data;
  471. }
  472. try {
  473. $parsedValue = Inline::parse($value, $flags, $this->refs);
  474. if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) {
  475. throw new ParseException('A colon cannot be used in an unquoted mapping value.');
  476. }
  477. return $parsedValue;
  478. } catch (ParseException $e) {
  479. $e->setParsedLine($this->getRealCurrentLineNb() + 1);
  480. $e->setSnippet($this->currentLine);
  481. throw $e;
  482. }
  483. }
  484. /**
  485. * Parses a block scalar.
  486. *
  487. * @param string $style The style indicator that was used to begin this block scalar (| or >)
  488. * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -)
  489. * @param int $indentation The indentation indicator that was used to begin this block scalar
  490. *
  491. * @return string The text value
  492. */
  493. private function parseBlockScalar($style, $chomping = '', $indentation = 0)
  494. {
  495. $notEOF = $this->moveToNextLine();
  496. if (!$notEOF) {
  497. return '';
  498. }
  499. $isCurrentLineBlank = $this->isCurrentLineBlank();
  500. $blockLines = array();
  501. // leading blank lines are consumed before determining indentation
  502. while ($notEOF && $isCurrentLineBlank) {
  503. // newline only if not EOF
  504. if ($notEOF = $this->moveToNextLine()) {
  505. $blockLines[] = '';
  506. $isCurrentLineBlank = $this->isCurrentLineBlank();
  507. }
  508. }
  509. // determine indentation if not specified
  510. if (0 === $indentation) {
  511. if (preg_match('/^ +/', $this->currentLine, $matches)) {
  512. $indentation = strlen($matches[0]);
  513. }
  514. }
  515. if ($indentation > 0) {
  516. $pattern = sprintf('/^ {%d}(.*)$/', $indentation);
  517. while (
  518. $notEOF && (
  519. $isCurrentLineBlank ||
  520. preg_match($pattern, $this->currentLine, $matches)
  521. )
  522. ) {
  523. if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
  524. $blockLines[] = substr($this->currentLine, $indentation);
  525. } elseif ($isCurrentLineBlank) {
  526. $blockLines[] = '';
  527. } else {
  528. $blockLines[] = $matches[1];
  529. }
  530. // newline only if not EOF
  531. if ($notEOF = $this->moveToNextLine()) {
  532. $isCurrentLineBlank = $this->isCurrentLineBlank();
  533. }
  534. }
  535. } elseif ($notEOF) {
  536. $blockLines[] = '';
  537. }
  538. if ($notEOF) {
  539. $blockLines[] = '';
  540. $this->moveToPreviousLine();
  541. } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
  542. $blockLines[] = '';
  543. }
  544. // folded style
  545. if ('>' === $style) {
  546. $text = '';
  547. $previousLineIndented = false;
  548. $previousLineBlank = false;
  549. for ($i = 0; $i < count($blockLines); ++$i) {
  550. if ('' === $blockLines[$i]) {
  551. $text .= "\n";
  552. $previousLineIndented = false;
  553. $previousLineBlank = true;
  554. } elseif (' ' === $blockLines[$i][0]) {
  555. $text .= "\n".$blockLines[$i];
  556. $previousLineIndented = true;
  557. $previousLineBlank = false;
  558. } elseif ($previousLineIndented) {
  559. $text .= "\n".$blockLines[$i];
  560. $previousLineIndented = false;
  561. $previousLineBlank = false;
  562. } elseif ($previousLineBlank || 0 === $i) {
  563. $text .= $blockLines[$i];
  564. $previousLineIndented = false;
  565. $previousLineBlank = false;
  566. } else {
  567. $text .= ' '.$blockLines[$i];
  568. $previousLineIndented = false;
  569. $previousLineBlank = false;
  570. }
  571. }
  572. } else {
  573. $text = implode("\n", $blockLines);
  574. }
  575. // deal with trailing newlines
  576. if ('' === $chomping) {
  577. $text = preg_replace('/\n+$/', "\n", $text);
  578. } elseif ('-' === $chomping) {
  579. $text = preg_replace('/\n+$/', '', $text);
  580. }
  581. return $text;
  582. }
  583. /**
  584. * Returns true if the next line is indented.
  585. *
  586. * @return bool Returns true if the next line is indented, false otherwise
  587. */
  588. private function isNextLineIndented()
  589. {
  590. $currentIndentation = $this->getCurrentLineIndentation();
  591. $EOF = !$this->moveToNextLine();
  592. while (!$EOF && $this->isCurrentLineEmpty()) {
  593. $EOF = !$this->moveToNextLine();
  594. }
  595. if ($EOF) {
  596. return false;
  597. }
  598. $ret = false;
  599. if ($this->getCurrentLineIndentation() > $currentIndentation) {
  600. $ret = true;
  601. }
  602. $this->moveToPreviousLine();
  603. return $ret;
  604. }
  605. /**
  606. * Returns true if the current line is blank or if it is a comment line.
  607. *
  608. * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise
  609. */
  610. private function isCurrentLineEmpty()
  611. {
  612. return $this->isCurrentLineBlank() || $this->isCurrentLineComment();
  613. }
  614. /**
  615. * Returns true if the current line is blank.
  616. *
  617. * @return bool Returns true if the current line is blank, false otherwise
  618. */
  619. private function isCurrentLineBlank()
  620. {
  621. return '' == trim($this->currentLine, ' ');
  622. }
  623. /**
  624. * Returns true if the current line is a comment line.
  625. *
  626. * @return bool Returns true if the current line is a comment line, false otherwise
  627. */
  628. private function isCurrentLineComment()
  629. {
  630. //checking explicitly the first char of the trim is faster than loops or strpos
  631. $ltrimmedLine = ltrim($this->currentLine, ' ');
  632. return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
  633. }
  634. private function isCurrentLineLastLineInDocument()
  635. {
  636. return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
  637. }
  638. /**
  639. * Cleanups a YAML string to be parsed.
  640. *
  641. * @param string $value The input YAML string
  642. *
  643. * @return string A cleaned up YAML string
  644. */
  645. private function cleanup($value)
  646. {
  647. $value = str_replace(array("\r\n", "\r"), "\n", $value);
  648. // strip YAML header
  649. $count = 0;
  650. $value = preg_replace('#^\%YAML[: ][\d\.]+.*\n#u', '', $value, -1, $count);
  651. $this->offset += $count;
  652. // remove leading comments
  653. $trimmedValue = preg_replace('#^(\#.*?\n)+#s', '', $value, -1, $count);
  654. if ($count == 1) {
  655. // items have been removed, update the offset
  656. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  657. $value = $trimmedValue;
  658. }
  659. // remove start of the document marker (---)
  660. $trimmedValue = preg_replace('#^\-\-\-.*?\n#s', '', $value, -1, $count);
  661. if ($count == 1) {
  662. // items have been removed, update the offset
  663. $this->offset += substr_count($value, "\n") - substr_count($trimmedValue, "\n");
  664. $value = $trimmedValue;
  665. // remove end of the document marker (...)
  666. $value = preg_replace('#\.\.\.\s*$#', '', $value);
  667. }
  668. return $value;
  669. }
  670. /**
  671. * Returns true if the next line starts unindented collection.
  672. *
  673. * @return bool Returns true if the next line starts unindented collection, false otherwise
  674. */
  675. private function isNextLineUnIndentedCollection()
  676. {
  677. $currentIndentation = $this->getCurrentLineIndentation();
  678. $notEOF = $this->moveToNextLine();
  679. while ($notEOF && $this->isCurrentLineEmpty()) {
  680. $notEOF = $this->moveToNextLine();
  681. }
  682. if (false === $notEOF) {
  683. return false;
  684. }
  685. $ret = false;
  686. if (
  687. $this->getCurrentLineIndentation() == $currentIndentation
  688. &&
  689. $this->isStringUnIndentedCollectionItem()
  690. ) {
  691. $ret = true;
  692. }
  693. $this->moveToPreviousLine();
  694. return $ret;
  695. }
  696. /**
  697. * Returns true if the string is un-indented collection item.
  698. *
  699. * @return bool Returns true if the string is un-indented collection item, false otherwise
  700. */
  701. private function isStringUnIndentedCollectionItem()
  702. {
  703. return '-' === rtrim($this->currentLine) || 0 === strpos($this->currentLine, '- ');
  704. }
  705. /**
  706. * Tests whether or not the current line is the header of a block scalar.
  707. *
  708. * @return bool
  709. */
  710. private function isBlockScalarHeader()
  711. {
  712. return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
  713. }
  714. }