| 1 |
<?php |
| 2 |
/** |
| 3 |
* @private |
| 4 |
*/ |
| 5 |
class Less_Tree_Variable extends Less_Tree { |
| 6 |
|
| 7 |
/** @var string */ |
| 8 |
public $name; |
| 9 |
/** @var int|null */ |
| 10 |
public $index; |
| 11 |
/** @var array|null */ |
| 12 |
public $currentFileInfo; |
| 13 |
/** @var bool */ |
| 14 |
public $evaluating = false; |
| 15 |
|
| 16 |
/** |
| 17 |
* @param string $name |
| 18 |
*/ |
| 19 |
public function __construct( $name, $index = null, $currentFileInfo = null ) { |
| 20 |
$this->name = $name; |
| 21 |
$this->index = $index; |
| 22 |
$this->currentFileInfo = $currentFileInfo; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param Less_Environment $env |
| 27 |
* @return Less_Tree|Less_Tree_Keyword|Less_Tree_Quoted |
| 28 |
* @see less-3.13.1.js#Variable.prototype.eval |
| 29 |
*/ |
| 30 |
public function compile( $env ) { |
| 31 |
// Optimization: Less.js checks if string starts with @@, we only check if second char is @ |
| 32 |
if ( $this->name[1] === '@' ) { |
| 33 |
$v = new self( substr( $this->name, 1 ), $this->index + 1, $this->currentFileInfo ); |
| 34 |
// While some Less_Tree nodes have no 'value', we know these can't occur after a |
| 35 |
// variable assignment (would have been a ParseError). |
| 36 |
$name = '@' . $v->compile( $env )->value; |
| 37 |
} else { |
| 38 |
$name = $this->name; |
| 39 |
} |
| 40 |
|
| 41 |
if ( $this->evaluating ) { |
| 42 |
throw new Less_Exception_Compiler( |
| 43 |
"Recursive variable definition for " . $name, |
| 44 |
null, |
| 45 |
$this->index, |
| 46 |
$this->currentFileInfo |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
$this->evaluating = true; |
| 51 |
$variable = null; |
| 52 |
foreach ( $env->frames as $frame ) { |
| 53 |
/** @var Less_Tree_Ruleset $frame */ |
| 54 |
$v = $frame->variable( $name ); |
| 55 |
if ( $v ) { |
| 56 |
if ( isset( $v->important ) && $v->important ) { |
| 57 |
$importantScopeLength = count( $env->importantScope ); |
| 58 |
$env->importantScope[ $importantScopeLength - 1 ]['important'] = $v->important; |
| 59 |
} |
| 60 |
// If in calc, wrap vars in a function call to cascade evaluate args first |
| 61 |
if ( $env->inCalc ) { |
| 62 |
$call = new Less_Tree_Call( '_SELF', [ $v->value ], $this->index, $this->currentFileInfo ); |
| 63 |
$variable = $call->compile( $env ); |
| 64 |
break; |
| 65 |
} else { |
| 66 |
$variable = $v->value->compile( $env ); |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
if ( $variable ) { |
| 72 |
$this->evaluating = false; |
| 73 |
return $variable; |
| 74 |
} |
| 75 |
|
| 76 |
throw new Less_Exception_Compiler( |
| 77 |
"variable " . $name . " is undefined in file " . $this->currentFileInfo["filename"], |
| 78 |
null, |
| 79 |
$this->index, |
| 80 |
$this->currentFileInfo |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
} |