WhiteLabel.php 3.0 KB

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