ParseException.php 3.5 KB

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