ScssCompiler.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Admin
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Admin;
  9. use ScssPhp\ScssPhp\Compiler;
  10. use ScssPhp\ScssPhp\ValueConverter;
  11. class ScssCompiler
  12. {
  13. protected $compiler;
  14. public function compiler()
  15. {
  16. if ($this->compiler === null) {
  17. $this->reset();
  18. }
  19. return $this->compiler;
  20. }
  21. public function reset()
  22. {
  23. $this->compiler = new Compiler();
  24. return $this;
  25. }
  26. public function setVariables(array $variables)
  27. {
  28. // $parsed = ValueConverter::fromPhp($variables);
  29. $parsed = [];
  30. foreach ($variables as $key => $value) {
  31. $parsed[$key] = ValueConverter::parseValue($value);
  32. }
  33. $this->compiler()->addVariables($parsed);
  34. return $this;
  35. }
  36. public function setImportPaths(array $paths)
  37. {
  38. $this->compiler()->setImportPaths($paths);
  39. return $this;
  40. }
  41. public function compile(string $input_file, string $output_file)
  42. {
  43. $input = file_get_contents($input_file);
  44. $output = $this->compiler()->compile($input);
  45. file_put_contents($output_file, $output);
  46. return $this;
  47. }
  48. public function compileAll(array $input_paths, string $output_file)
  49. {
  50. $input = '';
  51. foreach ($input_paths as $input_file) {
  52. $input .= trim(file_get_contents($input_file)) . "\n\n";
  53. }
  54. $output = $this->compiler()->compileString($input)->getCss();
  55. file_put_contents($output_file, $output);
  56. return $this;
  57. }
  58. }