SassContext.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /* SVN FILE: $Id: SassContext.php 118 2010-09-21 09:45:11Z chris.l.yates@gmail.com $ */
  3. /**
  4. * SassContext 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. * SassContext class.
  13. * Defines the context that the parser is operating in and so allows variables
  14. * to be scoped.
  15. * A new context is created for Mixins and imported files.
  16. * @package PHamlP
  17. * @subpackage Sass.tree
  18. */
  19. class SassContext {
  20. /**
  21. * @var SassContext enclosing context
  22. */
  23. protected $parent;
  24. /**
  25. * @var array mixins defined in this context
  26. */
  27. protected $mixins = array();
  28. /**
  29. * @var array variables defined in this context
  30. */
  31. protected $variables = array();
  32. /**
  33. * @var SassNode the node being processed
  34. */
  35. public $node;
  36. /**
  37. * SassContext constructor.
  38. * @param SassContext - the enclosing context
  39. * @return SassContext
  40. */
  41. public function __construct($parent = null) {
  42. $this->parent = $parent;
  43. }
  44. /**
  45. * Adds a mixin
  46. * @param string name of mixin
  47. * @return SassMixinDefinitionNode the mixin
  48. */
  49. public function addMixin($name, $mixin) {
  50. $this->mixins[$name] = $mixin;
  51. return $this;
  52. }
  53. /**
  54. * Returns a mixin
  55. * @param string name of mixin to return
  56. * @return SassMixinDefinitionNode the mixin
  57. * @throws SassContextException if mixin not defined in this context
  58. */
  59. public function getMixin($name) {
  60. if (isset($this->mixins[$name])) {
  61. return $this->mixins[$name];
  62. }
  63. elseif (!empty($this->parent)) {
  64. return $this->parent->getMixin($name);
  65. }
  66. throw new SassContextException('Undefined {what}: {name}', array('{what}'=>'Mixin', '{name}'=>$name), $this->node);
  67. }
  68. /**
  69. * Returns a variable defined in this context
  70. * @param string name of variable to return
  71. * @return string the variable
  72. * @throws SassContextException if variable not defined in this context
  73. */
  74. public function getVariable($name) {
  75. if (isset($this->variables[$name])) {
  76. return $this->variables[$name];
  77. }
  78. elseif (!empty($this->parent)) {
  79. return $this->parent->getVariable($name);
  80. }
  81. else {
  82. throw new SassContextException('Undefined {what}: {name}', array('{what}'=>'Variable', '{name}'=>$name), $this->node);
  83. }
  84. }
  85. /**
  86. * Returns a value indicating if the variable exists in this context
  87. * @param string name of variable to test
  88. * @return boolean true if the variable exists in this context, false if not
  89. */
  90. public function hasVariable($name) {
  91. return isset($this->variables[$name]);
  92. }
  93. /**
  94. * Sets a variable to the given value
  95. * @param string name of variable
  96. * @param sassLiteral value of variable
  97. */
  98. public function setVariable($name, $value) {
  99. $this->variables[$name] = $value;
  100. return $this;
  101. }
  102. /**
  103. * Makes variables and mixins from this context available in the parent context.
  104. * Note that if there are variables or mixins with the same name in the two
  105. * contexts they will be set to that defined in this context.
  106. */
  107. public function merge() {
  108. $this->parent->variables =
  109. array_merge($this->parent->variables, $this->variables);
  110. $this->parent->mixins = array_merge($this->parent->mixins, $this->mixins);
  111. }
  112. }