Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScssCompiler
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
9
0.00% covered (danger)
0.00%
0 / 1
 processTheme
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
9
1<?php
2
3/**
4 * Class to compile SCSS into CSS within a theme.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2014.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Theme
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFindTheme;
31
32use function dirname;
33
34/**
35 * Class to compile SCSS into CSS within a theme.
36 *
37 * @category VuFind
38 * @package  Theme
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org Main Site
42 */
43class ScssCompiler extends AbstractCssPreCompiler
44{
45    /**
46     * Key in theme.config.php that lists all files
47     *
48     * @var string
49     */
50    protected $themeConfigKey = 'scss';
51
52    /**
53     * Compile scripts for the specified theme.
54     *
55     * @param string $theme Theme name
56     *
57     * @return void
58     */
59    protected function processTheme($theme)
60    {
61        // Get files
62        $files = $this->getAllFiles($theme);
63        if (empty($files)) {
64            $this->logMessage('No SCSS in ' . $theme);
65            return;
66        }
67
68        // Build parent stack
69        $themeInfo = new ThemeInfo($this->basePath . '/themes', $theme);
70        $importPaths = [];
71        foreach (array_keys($themeInfo->getThemeInfo()) as $currTheme) {
72            $importPaths[] = $this->basePath . '/themes/' . $currTheme . '/scss/';
73        }
74
75        // Compile
76        $scss = new \ScssPhp\ScssPhp\Compiler();
77        $scss->setImportPaths($importPaths);
78        $this->logMessage('Processing ' . $theme);
79        $finalOutDir = $this->basePath . '/themes/' . $theme . '/css/';
80        foreach ($files as $key => $file) {
81            if ($key === 'active') {
82                continue;
83            }
84
85            $this->logMessage("\t" . $file);
86
87            // Check importPaths for file
88            $exists = false;
89            foreach ($importPaths as $path) {
90                if (file_exists($path . $file)) {
91                    $exists = true;
92                    break;
93                }
94            }
95            if (!$exists) {
96                $this->logMessage("\t\tnot found; skipping.");
97                continue;
98            }
99
100            $start = microtime(true);
101            $finalFile = $finalOutDir . str_replace('.scss', '.css', $file) . '.css';
102            if (!is_dir(dirname($finalFile))) {
103                mkdir(dirname($finalFile));
104            }
105            file_put_contents(
106                $finalOutDir . str_replace('.scss', '.css', $file),
107                $scss->compile('@import "' . $file . '";')
108            );
109            $this->logMessage("\t\t" . (microtime(true) - $start) . ' sec');
110        }
111    }
112}