ParseException.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, \Throwable $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. public function setSnippet(string $snippet)
  51. {
  52. $this->snippet = $snippet;
  53. $this->updateRepr();
  54. }
  55. /**
  56. * Gets the filename where the error occurred.
  57. *
  58. * This method returns null if a string is parsed.
  59. *
  60. * @return string The filename
  61. */
  62. public function getParsedFile()
  63. {
  64. return $this->parsedFile;
  65. }
  66. /**
  67. * Sets the filename where the error occurred.
  68. */
  69. public function setParsedFile(string $parsedFile)
  70. {
  71. $this->parsedFile = $parsedFile;
  72. $this->updateRepr();
  73. }
  74. /**
  75. * Gets the line where the error occurred.
  76. *
  77. * @return int The file line
  78. */
  79. public function getParsedLine()
  80. {
  81. return $this->parsedLine;
  82. }
  83. /**
  84. * Sets the line where the error occurred.
  85. */
  86. public function setParsedLine(int $parsedLine)
  87. {
  88. $this->parsedLine = $parsedLine;
  89. $this->updateRepr();
  90. }
  91. private function updateRepr()
  92. {
  93. $this->message = $this->rawMessage;
  94. $dot = false;
  95. if ('.' === substr($this->message, -1)) {
  96. $this->message = substr($this->message, 0, -1);
  97. $dot = true;
  98. }
  99. if (null !== $this->parsedFile) {
  100. $this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
  101. }
  102. if ($this->parsedLine >= 0) {
  103. $this->message .= sprintf(' at line %d', $this->parsedLine);
  104. }
  105. if ($this->snippet) {
  106. $this->message .= sprintf(' (near "%s")', $this->snippet);
  107. }
  108. if ($dot) {
  109. $this->message .= '.';
  110. }
  111. }
  112. }