Parser.php 32 KB

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