]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/oyejorge/less.php/lib/Less/Exception/Parser.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / oyejorge / less.php / lib / Less / Exception / Parser.php
1 <?php\r
2 \r
3 /**\r
4  * Parser Exception\r
5  *\r
6  * @package Less\r
7  * @subpackage exception\r
8  */\r
9 class Less_Exception_Parser extends Exception{\r
10 \r
11         /**\r
12          * The current file\r
13          *\r
14          * @var Less_ImportedFile\r
15          */\r
16         public $currentFile;\r
17 \r
18         /**\r
19          * The current parser index\r
20          *\r
21          * @var integer\r
22          */\r
23         public $index;\r
24 \r
25         protected $input;\r
26 \r
27         protected $details = array();\r
28 \r
29 \r
30         /**\r
31          * Constructor\r
32          *\r
33          * @param string $message\r
34          * @param Exception $previous Previous exception\r
35          * @param integer $index The current parser index\r
36          * @param Less_FileInfo|string $currentFile The file\r
37          * @param integer $code The exception code\r
38          */\r
39         public function __construct($message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0){\r
40 \r
41                 if (PHP_VERSION_ID < 50300) {\r
42                         $this->previous = $previous;\r
43                         parent::__construct($message, $code);\r
44                 } else {\r
45                         parent::__construct($message, $code, $previous);\r
46                 }\r
47 \r
48                 $this->currentFile = $currentFile;\r
49                 $this->index = $index;\r
50 \r
51                 $this->genMessage();\r
52         }\r
53 \r
54 \r
55         protected function getInput(){\r
56 \r
57                 if( !$this->input && $this->currentFile && $this->currentFile['filename'] && file_exists($this->currentFile['filename']) ){\r
58                         $this->input = file_get_contents( $this->currentFile['filename'] );\r
59                 }\r
60         }\r
61 \r
62 \r
63 \r
64         /**\r
65          * Converts the exception to string\r
66          *\r
67          * @return string\r
68          */\r
69         public function genMessage(){\r
70 \r
71                 if( $this->currentFile && $this->currentFile['filename'] ){\r
72                         $this->message .= ' in '.basename($this->currentFile['filename']);\r
73                 }\r
74 \r
75                 if( $this->index !== null ){\r
76                         $this->getInput();\r
77                         if( $this->input ){\r
78                                 $line = self::getLineNumber();\r
79                                 $this->message .= ' on line '.$line.', column '.self::getColumn();\r
80 \r
81                                 $lines = explode("\n",$this->input);\r
82 \r
83                                 $count = count($lines);\r
84                                 $start_line = max(0, $line-3);\r
85                                 $last_line = min($count, $start_line+6);\r
86                                 $num_len = strlen($last_line);\r
87                                 for( $i = $start_line; $i < $last_line; $i++ ){\r
88                                         $this->message .= "\n".str_pad($i+1,$num_len,'0',STR_PAD_LEFT).'| '.$lines[$i];\r
89                                 }\r
90                         }\r
91                 }\r
92 \r
93         }\r
94 \r
95         /**\r
96          * Returns the line number the error was encountered\r
97          *\r
98          * @return integer\r
99          */\r
100         public function getLineNumber(){\r
101                 if( $this->index ){\r
102                         // https://bugs.php.net/bug.php?id=49790\r
103                         if (ini_get("mbstring.func_overload")) {\r
104                                 return substr_count(substr($this->input, 0, $this->index), "\n") + 1;\r
105                         } else {\r
106                                 return substr_count($this->input, "\n", 0, $this->index) + 1;\r
107                         }\r
108                 }\r
109                 return 1;\r
110         }\r
111 \r
112 \r
113         /**\r
114          * Returns the column the error was encountered\r
115          *\r
116          * @return integer\r
117          */\r
118         public function getColumn(){\r
119 \r
120                 $part = substr($this->input, 0, $this->index);\r
121                 $pos = strrpos($part,"\n");\r
122                 return $this->index - $pos;\r
123         }\r
124 \r
125 }\r