Variable.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Variable
  4. *
  5. * @package Less
  6. * @subpackage tree
  7. */
  8. class Less_Tree_Variable extends Less_Tree {
  9. public $name;
  10. public $index;
  11. public $currentFileInfo;
  12. public $evaluating = false;
  13. public $type = 'Variable';
  14. /**
  15. * @param string $name
  16. */
  17. public function __construct( $name, $index = null, $currentFileInfo = null ) {
  18. $this->name = $name;
  19. $this->index = $index;
  20. $this->currentFileInfo = $currentFileInfo;
  21. }
  22. public function compile( $env ) {
  23. if ( $this->name[1] === '@' ) {
  24. $v = new Less_Tree_Variable( substr( $this->name, 1 ), $this->index + 1, $this->currentFileInfo );
  25. $name = '@' . $v->compile( $env )->value;
  26. } else {
  27. $name = $this->name;
  28. }
  29. if ( $this->evaluating ) {
  30. throw new Less_Exception_Compiler( "Recursive variable definition for " . $name, null, $this->index, $this->currentFileInfo );
  31. }
  32. $this->evaluating = true;
  33. foreach ( $env->frames as $frame ) {
  34. if ( $v = $frame->variable( $name ) ) {
  35. $r = $v->value->compile( $env );
  36. $this->evaluating = false;
  37. return $r;
  38. }
  39. }
  40. throw new Less_Exception_Compiler( "variable " . $name . " is undefined in file ".$this->currentFileInfo["filename"], null, $this->index, $this->currentFileInfo );
  41. }
  42. }