ParseException.php 3.4 KB

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