Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Yaml\Exception;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Exception class thrown when an error occurs during parsing.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 class ParseException extends RuntimeException
|
Chris@0
|
20 {
|
Chris@0
|
21 private $parsedFile;
|
Chris@0
|
22 private $parsedLine;
|
Chris@0
|
23 private $snippet;
|
Chris@0
|
24 private $rawMessage;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @param string $message The error message
|
Chris@0
|
28 * @param int $parsedLine The line where the error occurred
|
Chris@0
|
29 * @param string|null $snippet The snippet of code near the problem
|
Chris@0
|
30 * @param string|null $parsedFile The file name where the error occurred
|
Chris@0
|
31 * @param \Exception|null $previous The previous exception
|
Chris@0
|
32 */
|
Chris@0
|
33 public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
|
Chris@0
|
34 {
|
Chris@0
|
35 $this->parsedFile = $parsedFile;
|
Chris@0
|
36 $this->parsedLine = $parsedLine;
|
Chris@0
|
37 $this->snippet = $snippet;
|
Chris@0
|
38 $this->rawMessage = $message;
|
Chris@0
|
39
|
Chris@0
|
40 $this->updateRepr();
|
Chris@0
|
41
|
Chris@0
|
42 parent::__construct($this->message, 0, $previous);
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Gets the snippet of code near the error.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return string The snippet of code
|
Chris@0
|
49 */
|
Chris@0
|
50 public function getSnippet()
|
Chris@0
|
51 {
|
Chris@0
|
52 return $this->snippet;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Sets the snippet of code near the error.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param string $snippet The code snippet
|
Chris@0
|
59 */
|
Chris@0
|
60 public function setSnippet($snippet)
|
Chris@0
|
61 {
|
Chris@0
|
62 $this->snippet = $snippet;
|
Chris@0
|
63
|
Chris@0
|
64 $this->updateRepr();
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Gets the filename where the error occurred.
|
Chris@0
|
69 *
|
Chris@0
|
70 * This method returns null if a string is parsed.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return string The filename
|
Chris@0
|
73 */
|
Chris@0
|
74 public function getParsedFile()
|
Chris@0
|
75 {
|
Chris@0
|
76 return $this->parsedFile;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Sets the filename where the error occurred.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param string $parsedFile The filename
|
Chris@0
|
83 */
|
Chris@0
|
84 public function setParsedFile($parsedFile)
|
Chris@0
|
85 {
|
Chris@0
|
86 $this->parsedFile = $parsedFile;
|
Chris@0
|
87
|
Chris@0
|
88 $this->updateRepr();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Gets the line where the error occurred.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return int The file line
|
Chris@0
|
95 */
|
Chris@0
|
96 public function getParsedLine()
|
Chris@0
|
97 {
|
Chris@0
|
98 return $this->parsedLine;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Sets the line where the error occurred.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @param int $parsedLine The file line
|
Chris@0
|
105 */
|
Chris@0
|
106 public function setParsedLine($parsedLine)
|
Chris@0
|
107 {
|
Chris@0
|
108 $this->parsedLine = $parsedLine;
|
Chris@0
|
109
|
Chris@0
|
110 $this->updateRepr();
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 private function updateRepr()
|
Chris@0
|
114 {
|
Chris@0
|
115 $this->message = $this->rawMessage;
|
Chris@0
|
116
|
Chris@0
|
117 $dot = false;
|
Chris@0
|
118 if ('.' === substr($this->message, -1)) {
|
Chris@0
|
119 $this->message = substr($this->message, 0, -1);
|
Chris@0
|
120 $dot = true;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 if (null !== $this->parsedFile) {
|
Chris@0
|
124 $this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 if ($this->parsedLine >= 0) {
|
Chris@0
|
128 $this->message .= sprintf(' at line %d', $this->parsedLine);
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 if ($this->snippet) {
|
Chris@0
|
132 $this->message .= sprintf(' (near "%s")', $this->snippet);
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 if ($dot) {
|
Chris@0
|
136 $this->message .= '.';
|
Chris@0
|
137 }
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|