ParseException.php 3.5 KB

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