aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/assets/LESSCompiler.php
blob: ce05ee446c237f76067094af5c4e0242a80916d3 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Assets;

use Assets;
use StudipCacheFactory;

/**
 * LESS Compiler for assets.
 *
 * Uses less.php by wikimedia <https://github.com/wikimedia/less.php>.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.4
 */
class LESSCompiler implements Compiler
{
    const CACHE_KEY = '/assets/less-prefix';

    private static $instance = null;

    /**
     * Returns an instance of the compiler
     * @return Assets\LESSCompiler instance
     */
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Private constructor to enforce singleton.
     */
    private function __construct()
    {
    }

    /**
     * Compiles a less string. This method will add all neccessary imports
     * and variables for Stud.IP so almost all mixins and variables of the
     * core system can be used. This includes colors and icons.
     *
     * @param string $input      LESS content to compile
     * @param array  $variables Additional variables for the LESS compilation
     * @return string containing the generated CSS
     */
    public function compile($input, array $variables = []): string
    {
        $less = $this->getPrefix() . $input;

        $variables['image-path'] = '"' . Assets::url('images') . '"';

        $parser = new \Less_Parser([
            'strictMath' => true,
            'compress' => \Studip\ENV === 'production',
            'sourceMap' => \Studip\ENV !== 'production',
        ]);
        $parser->SetImportDirs(["{$GLOBALS['STUDIP_BASE_PATH']}/resources/" => '']);
        $parser->ModifyVars($variables);
        $parser->parse($less);
        return $parser->getCSS();
    }

    /**
     * Generates the less prefix containing the variables and mixins of the
     * Stud.IP core system.
     * This prefix will be cached in Stud.IP's cache in order to minimize
     * disk accesses.
     *
     * @return String containing the neccessary prefix
     */
    private function getPrefix()
    {
        $cache = StudipCacheFactory::getCache();

        $prefix = $cache->read(self::CACHE_KEY);

        if ($prefix === false) {
            $prefix = '';

            // Load mixins and change relative to absolute filenames
            $mixin_file = $GLOBALS['STUDIP_BASE_PATH'] . '/resources/assets/stylesheets/mixins.less';
            foreach (file($mixin_file) as $mixin) {
                if (!preg_match('/@import(.*?) "(.*)";/', $mixin, $match)) {
                    continue;
                }

                $core_file = "{$GLOBALS['STUDIP_BASE_PATH']}/resources/assets/stylesheets/{$match[2]}";
                $prefix .= sprintf('@import%s "%s";' . "\n", $match[1], $core_file);
            }

            // Add adjusted image paths
            $prefix .= sprintf('@image-path: "%s";', Assets::url('images')) . "\n";
            $prefix .= '@icon-path: "@{image-path}/icons/16";' . "\n";

            $cache->write(self::CACHE_KEY, $prefix);
        }
        return $prefix;
    }
}