[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/lessphp/Exception/ -> Parser.php (source)

   1  <?php
   2  
   3  /**

   4   * Parser Exception

   5   *

   6   * @package Less

   7   * @subpackage exception

   8   */
   9  class Less_Exception_Parser extends Exception{
  10  
  11      /**

  12       * The current file

  13       *

  14       * @var Less_ImportedFile

  15       */
  16      public $currentFile;
  17  
  18      /**

  19       * The current parser index

  20       *

  21       * @var integer

  22       */
  23      public $index;
  24  
  25      protected $input;
  26  
  27      protected $details = array();
  28  
  29  
  30      /**

  31       * Constructor

  32       *

  33       * @param string $message

  34       * @param Exception $previous Previous exception

  35       * @param integer $index The current parser index

  36       * @param Less_FileInfo|string $currentFile The file

  37       * @param integer $code The exception code

  38       */
  39  	public function __construct($message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0){
  40  
  41          if (PHP_VERSION_ID < 50300) {
  42              $this->previous = $previous;
  43              parent::__construct($message, $code);
  44          } else {
  45              parent::__construct($message, $code, $previous);
  46          }
  47  
  48          $this->currentFile = $currentFile;
  49          $this->index = $index;
  50  
  51          $this->genMessage();
  52      }
  53  
  54  
  55  	protected function getInput(){
  56  
  57          if( !$this->input && $this->currentFile && $this->currentFile['filename'] && file_exists($this->currentFile['filename']) ){
  58              $this->input = file_get_contents( $this->currentFile['filename'] );
  59          }
  60      }
  61  
  62  
  63  
  64      /**

  65       * Converts the exception to string

  66       *

  67       * @return string

  68       */
  69  	public function genMessage(){
  70  
  71          if( $this->currentFile && $this->currentFile['filename'] ){
  72              $this->message .= ' in '.basename($this->currentFile['filename']);
  73          }
  74  
  75          if( $this->index !== null ){
  76              $this->getInput();
  77              if( $this->input ){
  78                  $line = self::getLineNumber();
  79                  $this->message .= ' on line '.$line.', column '.self::getColumn();
  80  
  81                  $lines = explode("\n",$this->input);
  82  
  83                  $count = count($lines);
  84                  $start_line = max(0, $line-3);
  85                  $last_line = min($count, $start_line+6);
  86                  $num_len = strlen($last_line);
  87                  for( $i = $start_line; $i < $last_line; $i++ ){
  88                      $this->message .= "\n".str_pad($i+1,$num_len,'0',STR_PAD_LEFT).'| '.$lines[$i];
  89                  }
  90              }
  91          }
  92  
  93      }
  94  
  95      /**

  96       * Returns the line number the error was encountered

  97       *

  98       * @return integer

  99       */
 100  	public function getLineNumber(){
 101          if( $this->index ){
 102              // https://bugs.php.net/bug.php?id=49790

 103              if (ini_get("mbstring.func_overload")) {
 104                  return substr_count(substr($this->input, 0, $this->index), "\n") + 1;
 105              } else {
 106                  return substr_count($this->input, "\n", 0, $this->index) + 1;
 107              }
 108          }
 109          return 1;
 110      }
 111  
 112  
 113      /**

 114       * Returns the column the error was encountered

 115       *

 116       * @return integer

 117       */
 118  	public function getColumn(){
 119  
 120          $part = substr($this->input, 0, $this->index);
 121          $pos = strrpos($part,"\n");
 122          return $this->index - $pos;
 123      }
 124  
 125  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1