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