icingaweb2/library/Icinga/Less/ColorPropOrVariable.php

67 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2022-02-09 21:22:36 +01:00
/* Icinga Web 2 | (c) 2022 Icinga Development Team | GPLv2+ */
namespace Icinga\Less;
use Less_Tree;
use Less_Tree_Color;
use Less_Tree_Variable;
2021-12-06 14:59:36 +01:00
/**
* Compile a Less variable to {@link ColorProp} if it is a color
*/
class ColorPropOrVariable extends Less_Tree
{
public $type = 'Variable';
/** @var Less_Tree_Variable */
protected $variable;
/**
* @return Less_Tree_Variable
*/
public function getVariable()
{
return $this->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();
2021-12-02 15:37:56 +01:00
if ($v->name[1] === '@') {
2021-12-06 14:59:36 +01:00
// Evaluate variable variable as in Less_Tree_Variable:28.
2021-12-02 15:37:56 +01:00
$vv = new Less_Tree_Variable(substr($v->name, 1), $v->index + 1, $v->currentFileInfo);
2021-12-06 14:59:36 +01:00
// Overwrite the name so that the variable variable is not evaluated again.
2021-12-02 15:37:56 +01:00
$v->name = '@' . $vv->compile($env)->value;
}
$compiled = $v->compile($env);
2021-12-02 15:37:43 +01:00
if ($compiled instanceof ColorProp) {
// We may already have a ColorProp, which is the case with mixin calls.
return $compiled;
}
if ($compiled instanceof Less_Tree_Color) {
return ColorProp::fromColor($compiled)
->setIndex($v->index)
2021-12-06 14:59:36 +01:00
->setName(substr($v->name, 1));
}
return $compiled;
}
}