ParseException.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Exception;
  11. /**
  12. * Exception class thrown when an error occurs during parsing.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class ParseException extends RuntimeException
  17. {
  18. private $parsedFile;
  19. private $parsedLine;
  20. private $snippet;
  21. private $rawMessage;
  22. /**
  23. * @param string $message The error message
  24. * @param int $parsedLine The line where the error occurred
  25. * @param string|null $snippet The snippet of code near the problem
  26. * @param string|null $parsedFile The file name where the error occurred
  27. */
  28. public function __construct(string $message, int $parsedLine = -1, string $snippet = null, string $parsedFile = null, \Throwable $previous = null)
  29. {
  30. $this->parsedFile = $parsedFile;
  31. $this->parsedLine = $parsedLine;
  32. $this->snippet = $snippet;
  33. $this->rawMessage = $message;
  34. $this->updateRepr();
  35. parent::__construct($this->message, 0, $previous);
  36. }
  37. /**
  38. * Gets the snippet of code near the error.
  39. *
  40. * @return string The snippet of code
  41. */
  42. public function getSnippet()
  43. {
  44. return $this->snippet;
  45. }
  46. /**
  47. * Sets the snippet of code near the error.
  48. *
  49. * @param string $snippet The code snippet
  50. */
  51. public function setSnippet($snippet)
  52. {
  53. $this->snippet = $snippet;
  54. $this->updateRepr();
  55. }
  56. /**
  57. * Gets the filename where the error occurred.
  58. *
  59. * This method returns null if a string is parsed.
  60. *
  61. * @return string The filename
  62. */
  63. public function getParsedFile()
  64. {
  65. return $this->parsedFile;
  66. }
  67. /**
  68. * Sets the filename where the error occurred.
  69. *
  70. * @param string $parsedFile The filename
  71. */
  72. public function setParsedFile($parsedFile)
  73. {
  74. $this->parsedFile = $parsedFile;
  75. $this->updateRepr();
  76. }
  77. /**
  78. * Gets the line where the error occurred.
  79. *
  80. * @return int The file line
  81. */
  82. public function getParsedLine()
  83. {
  84. return $this->parsedLine;
  85. }
  86. /**
  87. * Sets the line where the error occurred.
  88. *
  89. * @param int $parsedLine The file line
  90. */
  91. public function setParsedLine($parsedLine)
  92. {
  93. $this->parsedLine = $parsedLine;
  94. $this->updateRepr();
  95. }
  96. private function updateRepr()
  97. {
  98. $this->message = $this->rawMessage;
  99. $dot = false;
  100. if ('.' === substr($this->message, -1)) {
  101. $this->message = substr($this->message, 0, -1);
  102. $dot = true;
  103. }
  104. if (null !== $this->parsedFile) {
  105. $this->message .= sprintf(' in %s', json_encode($this->parsedFile, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
  106. }
  107. if ($this->parsedLine >= 0) {
  108. $this->message .= sprintf(' at line %d', $this->parsedLine);
  109. }
  110. if ($this->snippet) {
  111. $this->message .= sprintf(' (near "%s")', $this->snippet);
  112. }
  113. if ($dot) {
  114. $this->message .= '.';
  115. }
  116. }
  117. }