Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.41% |
27 / 34 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
AbstractCssPreCompiler | |
79.41% |
27 / 34 |
|
62.50% |
5 / 8 |
19.52 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
processTheme | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
setBasePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setTempPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
compile | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
getAllFiles | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
getAllThemes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
makeRelative | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
logMessage | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 |
1 | <?php |
2 | |
3 | /** |
4 | * Abstract base class to precompile CSS within a theme. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2020. |
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 | |
30 | namespace VuFindTheme; |
31 | |
32 | use Symfony\Component\Console\Output\OutputInterface; |
33 | |
34 | /** |
35 | * Abstract base class to precompile 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 | */ |
43 | abstract class AbstractCssPreCompiler |
44 | { |
45 | /** |
46 | * Key in theme.config.php that lists all files |
47 | * |
48 | * @var string |
49 | */ |
50 | protected $themeConfigKey; |
51 | |
52 | /** |
53 | * Base path of VuFind. |
54 | * |
55 | * @var string |
56 | */ |
57 | protected $basePath; |
58 | |
59 | /** |
60 | * Temporary directory for cached files. |
61 | * |
62 | * @var string |
63 | */ |
64 | protected $tempPath; |
65 | |
66 | /** |
67 | * Fake base path used for generating absolute paths in CSS. |
68 | * |
69 | * @var string |
70 | */ |
71 | protected $fakePath = '/zzzz_basepath_zzzz/'; |
72 | |
73 | /** |
74 | * Output object (set for logging) |
75 | * |
76 | * @var OutputInterface |
77 | */ |
78 | protected $output; |
79 | |
80 | /** |
81 | * Constructor |
82 | * |
83 | * @param OutputInterface $output Output interface for logging (optional) |
84 | */ |
85 | public function __construct(OutputInterface $output = null) |
86 | { |
87 | $this->basePath = realpath(__DIR__ . '/../../../../'); |
88 | $this->tempPath = sys_get_temp_dir(); |
89 | $this->output = $output; |
90 | } |
91 | |
92 | /** |
93 | * Compile scripts for the specified theme. |
94 | * |
95 | * @param string $theme Theme name |
96 | * |
97 | * @return void |
98 | */ |
99 | abstract protected function processTheme($theme); |
100 | |
101 | /** |
102 | * Set base path |
103 | * |
104 | * @param string $path Path to set |
105 | * |
106 | * @return void |
107 | */ |
108 | public function setBasePath($path) |
109 | { |
110 | $this->basePath = $path; |
111 | } |
112 | |
113 | /** |
114 | * Set temporary directory |
115 | * |
116 | * @param string $path Path to set |
117 | * |
118 | * @return void |
119 | */ |
120 | public function setTempPath($path) |
121 | { |
122 | $this->tempPath = rtrim($path, '/'); |
123 | } |
124 | |
125 | /** |
126 | * Compile the scripts. |
127 | * |
128 | * @param array $themes Array of themes to process (empty for ALL themes). |
129 | * |
130 | * @return void |
131 | */ |
132 | public function compile(array $themes) |
133 | { |
134 | if (empty($themes)) { |
135 | $themes = $this->getAllThemes(); |
136 | } |
137 | |
138 | foreach ($themes as $theme) { |
139 | $this->processTheme($theme); |
140 | } |
141 | } |
142 | |
143 | /** |
144 | * Get all less files that might exist in a theme. |
145 | * |
146 | * @param string $theme Theme to retrieve files from |
147 | * |
148 | * @return array |
149 | */ |
150 | protected function getAllFiles($theme) |
151 | { |
152 | $config = $this->basePath . '/themes/' . $theme . '/theme.config.php'; |
153 | if (!file_exists($config)) { |
154 | return []; |
155 | } |
156 | $configArr = include $config; |
157 | $base = (isset($configArr['extends'])) |
158 | ? $this->getAllFiles($configArr['extends']) |
159 | : []; |
160 | $current = $configArr[$this->themeConfigKey] ?? []; |
161 | return array_merge($base, $current); |
162 | } |
163 | |
164 | /** |
165 | * Get a list of all available themes. |
166 | * |
167 | * @return array |
168 | */ |
169 | protected function getAllThemes() |
170 | { |
171 | $baseDir = $this->basePath . '/themes/'; |
172 | $dir = opendir($baseDir); |
173 | $list = []; |
174 | while ($line = readdir($dir)) { |
175 | if ( |
176 | is_dir($baseDir . $line) |
177 | && file_exists($baseDir . $line . '/theme.config.php') |
178 | ) { |
179 | $list[] = $line; |
180 | } |
181 | } |
182 | closedir($dir); |
183 | return $list; |
184 | } |
185 | |
186 | /** |
187 | * Convert fake absolute paths to working relative paths. |
188 | * |
189 | * @param string $css Generated CSS |
190 | * @param string $less Relative LESS filename |
191 | * |
192 | * @return string |
193 | * |
194 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
195 | */ |
196 | protected function makeRelative($css, $less) |
197 | { |
198 | // Figure out how deep the LESS file is nested -- this will |
199 | // affect our relative path. Note: we don't actually need |
200 | // to use $matches for anything, but some versions of PHP |
201 | // seem to be unhappy if we omit the parameter. |
202 | $depth = preg_match_all('|/|', $less, $matches); |
203 | $relPath = '../../../'; |
204 | for ($i = 0; $i < $depth; $i++) { |
205 | $relPath .= '/../'; |
206 | } |
207 | return str_replace($this->fakePath, $relPath, $css); |
208 | } |
209 | |
210 | /** |
211 | * Log a message to the console |
212 | * |
213 | * @param string $str message string |
214 | * |
215 | * @return void |
216 | */ |
217 | protected function logMessage($str) |
218 | { |
219 | if ($this->output) { |
220 | $this->output->writeln($str); |
221 | } |
222 | } |
223 | } |