WhiteLabel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Grav\Plugin\Admin;
  3. use Grav\Common\Filesystem\Folder;
  4. use Grav\Common\Grav;
  5. use Grav\Framework\File\File;
  6. use RocketTheme\Toolbox\Event\Event;
  7. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  8. use Symfony\Component\Yaml\Yaml;
  9. class WhiteLabel
  10. {
  11. protected $grav;
  12. protected $scss;
  13. public function __construct()
  14. {
  15. $this->grav = Grav::instance();
  16. $this->scss = new ScssCompiler();
  17. }
  18. public function compilePresetScss($config, $options = [
  19. 'input' => 'plugin://admin/themes/grav/scss/preset.scss',
  20. 'output' => 'asset://admin-preset.css'
  21. ])
  22. {
  23. if (is_array($config)) {
  24. $color_scheme = $config['color_scheme'];
  25. } else {
  26. $color_scheme = $config->get('whitelabel.color_scheme');
  27. }
  28. if ($color_scheme) {
  29. /** @var UniformResourceLocator $locator */
  30. $locator = $this->grav['locator'];
  31. // Use ScssList object to make it easier ot handle in event
  32. $scss_list = new ScssList($locator->findResource($options['input']));
  33. $output_css = $locator->findResource(($options['output']), true, true);
  34. Folder::create(dirname($output_css));
  35. Grav::instance()->fireEvent('onAdminCompilePresetSCSS', new Event(['scss' => $scss_list]));
  36. // Convert bak to regular array now we have run the event
  37. $input_scss = $scss_list->all();
  38. $imports = [$locator->findResource('plugin://admin/themes/grav/scss')];
  39. foreach ($input_scss as $scss) {
  40. $input_path = dirname($scss);
  41. if (!in_array($input_path, $imports)) {
  42. $imports[] = $input_path;
  43. }
  44. }
  45. try {
  46. $compiler = $this->scss->reset();
  47. $compiler->setVariables($color_scheme['colors'] + $color_scheme['accents']);
  48. $compiler->setImportPaths($imports);
  49. $compiler->compileAll($input_scss, $output_css);
  50. } catch (\Exception $e) {
  51. return [false, $e->getMessage()];
  52. }
  53. return [true, 'Recompiled successfully'];
  54. }
  55. return [false, ' Could not be recompiled, missing color scheme...'];
  56. }
  57. public function exportPresetScsss($config, $location = 'asset://admin-theme-export.yaml')
  58. {
  59. if (isset($config['color_scheme'])) {
  60. $color_scheme = $config['color_scheme'];
  61. $body = Yaml::dump($color_scheme);
  62. $file = new File($location);
  63. $file->save($body);
  64. // todo: handle errors/exceptions?
  65. return [true, 'File created successfully'];
  66. } else {
  67. return [false, ' Could not export, missing color scheme...'];
  68. }
  69. }
  70. }