summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Less/LightModeDefinition.php
blob: 929e95c07c4a3a517a0f3f7d020f02347165aeaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */

namespace Icinga\Less;

use Less_Environment;
use Less_Exception_Compiler;
use Less_Tree_DetachedRuleset;
use Less_Tree_Ruleset;

/**
 * Register the environment in which the light mode is defined
 */
class LightModeDefinition extends Less_Tree_DetachedRuleset
{
    use LightModeTrait;

    /** @var string */
    protected $name;

    /**
     * @param Less_Tree_DetachedRuleset $drs
     *
     * @return static
     */
    public static function fromDetachedRuleset(Less_Tree_DetachedRuleset $drs)
    {
        return new static($drs->ruleset, $drs->frames);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @param Less_Environment $env
     *
     * @return Less_Tree_DetachedRuleset
     */
    public function compile($env)
    {
        $drs = parent::compile($env);

        /** @var $frame Less_Tree_Ruleset */
        foreach ($env->frames as $frame) {
            if ($frame->variable($this->getName())) {
                if (! empty($frame->first_oelements) && ! isset($frame->first_oelements['.icinga-module'])) {
                    throw new Less_Exception_Compiler('Light mode definition not allowed in selectors');
                }

                break;
            }
        }

        $this->getLightMode()->setEnv($this->getName(), $env->copyEvalEnv($env->frames));

        return $drs;
    }
}