1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * This file is part of Twig. |
---|
5 | * |
---|
6 | * (c) 2009 Fabien Potencier |
---|
7 | * |
---|
8 | * For the full copyright and license information, please view the LICENSE |
---|
9 | * file that was distributed with this source code. |
---|
10 | */ |
---|
11 | |
---|
12 | /** |
---|
13 | * Twig base exception. |
---|
14 | * |
---|
15 | * @package twig |
---|
16 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
17 | */ |
---|
18 | class Twig_Error extends Exception |
---|
19 | { |
---|
20 | protected $lineno; |
---|
21 | protected $filename; |
---|
22 | protected $rawMessage; |
---|
23 | protected $previous; |
---|
24 | |
---|
25 | /** |
---|
26 | * Constructor. |
---|
27 | * |
---|
28 | * @param string $message The error message |
---|
29 | * @param integer $lineno The template line where the error occurred |
---|
30 | * @param string $filename The template file name where the error occurred |
---|
31 | * @param Exception $previous The previous exception |
---|
32 | */ |
---|
33 | public function __construct($message, $lineno = -1, $filename = null, Exception $previous = null) |
---|
34 | { |
---|
35 | $this->lineno = $lineno; |
---|
36 | $this->filename = $filename; |
---|
37 | $this->rawMessage = $message; |
---|
38 | |
---|
39 | $this->updateRepr(); |
---|
40 | |
---|
41 | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
---|
42 | $this->previous = $previous; |
---|
43 | parent::__construct($this->message); |
---|
44 | } else { |
---|
45 | parent::__construct($this->message, 0, $previous); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Gets the filename where the error occurred. |
---|
51 | * |
---|
52 | * @return string The filename |
---|
53 | */ |
---|
54 | public function getTemplateFile() |
---|
55 | { |
---|
56 | return $this->filename; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Sets the filename where the error occurred. |
---|
61 | * |
---|
62 | * @param string $filename The filename |
---|
63 | */ |
---|
64 | public function setTemplateFile($filename) |
---|
65 | { |
---|
66 | $this->filename = $filename; |
---|
67 | |
---|
68 | $this->updateRepr(); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Gets the template line where the error occurred. |
---|
73 | * |
---|
74 | * @return integer The template line |
---|
75 | */ |
---|
76 | public function getTemplateLine() |
---|
77 | { |
---|
78 | return $this->lineno; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Sets the template line where the error occurred. |
---|
83 | * |
---|
84 | * @param integer $lineno The template line |
---|
85 | */ |
---|
86 | public function setTemplateLine($lineno) |
---|
87 | { |
---|
88 | $this->lineno = $lineno; |
---|
89 | |
---|
90 | $this->updateRepr(); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * For PHP < 5.3.0, provides access to the getPrevious() method. |
---|
95 | * |
---|
96 | * @param string $method The method name |
---|
97 | * @param array $arguments The parameters to be passed to the method |
---|
98 | * |
---|
99 | * @return Exception The previous exception or null |
---|
100 | */ |
---|
101 | public function __call($method, $arguments) |
---|
102 | { |
---|
103 | if ('getprevious' == strtolower($method)) { |
---|
104 | return $this->previous; |
---|
105 | } |
---|
106 | |
---|
107 | throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method)); |
---|
108 | } |
---|
109 | |
---|
110 | protected function updateRepr() |
---|
111 | { |
---|
112 | $this->message = $this->rawMessage; |
---|
113 | |
---|
114 | if (null !== $this->filename) { |
---|
115 | $this->message .= sprintf(' in %s', json_encode($this->filename)); |
---|
116 | } |
---|
117 | |
---|
118 | if ($this->lineno >= 0) { |
---|
119 | $this->message .= sprintf(' at line %d', $this->lineno); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|