Merge pull request #18513 from eileenmcnaughton/export_extract
[civicrm-core.git] / Civi / Compile / Scss.php
1 <?php
2 namespace Civi\Compile;
3
4 class Scss {
5
6 /**
7 * Compile some SCSS file(s).
8 *
9 * NOTE: This function runs during 'composer install', which is a pre-boot
10 * environment. The composer autoloader has been configured, but no other
11 * Civi services are online.
12 *
13 * @param array $task
14 * With keys:
15 * - scss-includes: string[], list of paths with SCSS helper files
16 * - scss-files: array, key-value mapping with input-files and output-files
17 *
18 * @see composer.json
19 * @link https://github.com/civicrm/composer-compile-plugin/blob/master/doc/tasks.md
20 */
21 public static function build(array $task) {
22 $scssCompiler = new \ScssPhp\ScssPhp\Compiler();
23 $includes = $task['scss-includes'] ?? [];
24 foreach ($includes as $include) {
25 $scssCompiler->addImportPath($include);
26 }
27
28 if (empty($task['scss-files'])) {
29 throw new \InvalidArgumentException("Invalid task: required argument 'scss-files' is missing");
30 }
31 foreach ($task['scss-files'] as $inputFile => $outputFile) {
32 if (!file_exists($inputFile)) {
33 throw new \InvalidArgumentException("File does not exist: " . $inputFile);
34 }
35 $inputScss = file_get_contents($inputFile);
36 $css = $scssCompiler->compile($inputScss);
37 $autoprefixer = new \Padaliyajay\PHPAutoprefixer\Autoprefixer($css);
38
39 if (!file_exists(dirname($outputFile))) {
40 mkdir(dirname($outputFile), 0777, TRUE);
41 }
42 $outputCss = $autoprefixer->compile();
43 if (!file_put_contents($outputFile, $outputCss)) {
44 throw new \RuntimeException("Failed to write file: $outputFile");
45 }
46 }
47 }
48
49 }