SassVariableNode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* SVN FILE: $Id: SassVariableNode.php 118 2010-09-21 09:45:11Z chris.l.yates@gmail.com $ */
  3. /**
  4. * SassVariableNode class file.
  5. * @author Chris Yates <chris.l.yates@gmail.com>
  6. * @copyright Copyright (c) 2010 PBM Web Development
  7. * @license http://phamlp.googlecode.com/files/license.txt
  8. * @package PHamlP
  9. * @subpackage Sass.tree
  10. */
  11. /**
  12. * SassVariableNode class.
  13. * Represents a variable.
  14. * @package PHamlP
  15. * @subpackage Sass.tree
  16. */
  17. class SassVariableNode extends SassNode {
  18. const MATCH = '/^([!$])([\w-]+)\s*:?\s*((\|\|)?=)?\s*(.+?)\s*(!default)?;?$/i';
  19. const IDENTIFIER = 1;
  20. const NAME = 2;
  21. const SASS_ASSIGNMENT = 3;
  22. const SASS_DEFAULT = 4;
  23. const VALUE = 5;
  24. const SCSS_DEFAULT = 6;
  25. const SASS_IDENTIFIER = '!';
  26. const SCSS_IDENTIFIER = '$';
  27. /**
  28. * @var string name of the variable
  29. */
  30. private $name;
  31. /**
  32. * @var string value of the variable or expression to evaluate
  33. */
  34. private $value;
  35. /**
  36. * @var boolean whether the variable is optionally assigned
  37. */
  38. private $isDefault;
  39. /**
  40. * SassVariableNode constructor.
  41. * @param object source token
  42. * @return SassVariableNode
  43. */
  44. public function __construct($token) {
  45. parent::__construct($token);
  46. preg_match(self::MATCH, $token->source, $matches);
  47. if (empty($matches[self::NAME]) || ($matches[self::VALUE] === '')) {
  48. throw new SassVariableNodeException('Invalid variable definition; name and expression required', array(), $this);
  49. }
  50. $this->name = $matches[self::NAME];
  51. $this->value = $matches[self::VALUE];
  52. $this->isDefault = (!empty($matches[self::SASS_DEFAULT]) || !empty($matches[self::SCSS_DEFAULT]));
  53. // Warn about deprecated features
  54. if ($matches[self::IDENTIFIER] === self::SASS_IDENTIFIER) {
  55. $this->addWarning('Variables prefixed with "!" is deprecated; use "${name}"', array('{name}'=>$this->name));
  56. }
  57. if (!empty($matches[SassVariableNode::SASS_ASSIGNMENT])) {
  58. $this->addWarning('Setting variables with "{sassDefault}=" is deprecated; use "${name}: {value}{scssDefault}"', array('{sassDefault}'=>(!empty($matches[SassVariableNode::SASS_DEFAULT])?'||':''), '{name}'=>$this->name, '{value}'=>$this->value, '{scssDefault}'=>(!empty($matches[SassVariableNode::SASS_DEFAULT])?' !default':'')));
  59. }
  60. }
  61. /**
  62. * Parse this node.
  63. * Sets the variable in the current context.
  64. * @param SassContext the context in which this node is parsed
  65. * @return array the parsed node - an empty array
  66. */
  67. public function parse($context) {
  68. if (!$this->isDefault || !$context->hasVariable($this->name)) {
  69. $context->setVariable(
  70. $this->name, $this->evaluate($this->value, $context)
  71. );
  72. }
  73. $this->parseChildren($context); // Parse any warnings
  74. return array();
  75. }
  76. /**
  77. * Returns a value indicating if the token represents this type of node.
  78. * @param object token
  79. * @return boolean true if the token represents this type of node, false if not
  80. */
  81. public static function isa($token, $syntax = '') {
  82. return $token->source[0] === self::SASS_IDENTIFIER || $token->source[0] === self::SCSS_IDENTIFIER;
  83. }
  84. }