diff --git a/library/Icinga/Less/ColorProp.php b/library/Icinga/Less/ColorProp.php new file mode 100644 index 000000000..284459d59 --- /dev/null +++ b/library/Icinga/Less/ColorProp.php @@ -0,0 +1,99 @@ +color = $color; + + foreach ($color as $k => $v) { + $self->$k = $v; + } + + return $self; + } + + /** + * @return int + */ + public function getIndex() + { + return $this->index; + } + + /** + * @param int $index + * + * @return $this + */ + public function setIndex($index) + { + $this->index = $index; + + return $this; + } + + /** + * @return string + */ + public function getOrigin() + { + return $this->origin; + } + + /** + * @param string $origin + * + * @return $this + */ + public function setOrigin($origin) + { + $this->origin = $origin; + + return $this; + } + + public function compile() + { + return $this; + } + + public function genCSS($output) + { + $css = (new Less_Tree_Call( + 'var', + [ + new Less_Tree_Keyword('--' . $this->getOrigin()), + $this->color + ], + $this->getIndex() + ))->toCSS(); + + $output->add($css); + } +} diff --git a/library/Icinga/Less/ColorPropOrVariable.php b/library/Icinga/Less/ColorPropOrVariable.php new file mode 100644 index 000000000..95290f09a --- /dev/null +++ b/library/Icinga/Less/ColorPropOrVariable.php @@ -0,0 +1,49 @@ +variable; + } + + /** + * @param Less_Tree_Variable $variable + * + * @return $this + */ + public function setVariable(Less_Tree_Variable $variable) + { + $this->variable = $variable; + + return $this; + } + + public function compile($env) + { + $v = $this->getVariable(); + $compiled = $v->compile($env); + + if ($compiled instanceof Less_Tree_Color) { + return ColorProp::fromColor($compiled) + ->setIndex($v->index) + ->setOrigin(substr($v->name, 1)); + } + + return $compiled; + } +} diff --git a/library/Icinga/Less/Visitor.php b/library/Icinga/Less/Visitor.php new file mode 100644 index 000000000..e1792518b --- /dev/null +++ b/library/Icinga/Less/Visitor.php @@ -0,0 +1,96 @@ +name === 'var') { + if ($this->callingVar !== false) { + throw new LogicException('Already calling var'); + } + + $this->callingVar = spl_object_hash($c); + } + + return $c; + } + + public function visitCallOut($c) + { + if ($this->callingVar !== false && $this->callingVar === spl_object_hash($c)) { + $this->callingVar = false; + } + } + + public function visitDetachedRuleset($rs) + { + // A detached ruleset is a variable definition in the first place, + // so just reset that we define a variable. + $this->definingVar = false; + + return $rs; + } + + public function visitRule($r) + { + if ($r->name[0] === '@' && $r->variable) { + if ($this->definingVar !== false) { + throw new LogicException('Already defining a variable'); + } + + $this->definingVar = spl_object_hash($r); + } + + return $r; + } + + public function visitRuleOut($r) + { + if ($this->definingVar !== false && $this->definingVar === spl_object_hash($r)) { + $this->definingVar = false; + } + } + + public function visitVariable($v) + { + if ($this->callingVar !== false || $this->definingVar !== false) { + return $v; + } + + return (new ColorPropOrVariable()) + ->setVariable($v); + } + + public function run($node) + { + return $this->visitObj($node); + } +} diff --git a/library/Icinga/Util/LessParser.php b/library/Icinga/Util/LessParser.php index 0e5cccaca..40bcd69c9 100644 --- a/library/Icinga/Util/LessParser.php +++ b/library/Icinga/Util/LessParser.php @@ -3,6 +3,7 @@ namespace Icinga\Util; +use Icinga\Less\Visitor; use Less_Tree_Anonymous; use Less_Tree_Expression; use Less_Tree_Quoted; @@ -16,6 +17,7 @@ class LessParser extends lessc public function __construct() { $this->registerFunction('extract-variable-default', [$this, 'extractVariableDefault']); + $this->setOption('plugins', [new Visitor()]); } /**