Theme.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use Grav\Common\Config\Config;
  10. use RocketTheme\Toolbox\File\YamlFile;
  11. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  12. /**
  13. * Class Theme
  14. * @package Grav\Common
  15. */
  16. class Theme extends Plugin
  17. {
  18. /**
  19. * Constructor.
  20. *
  21. * @param Grav $grav
  22. * @param Config $config
  23. * @param string $name
  24. */
  25. public function __construct(Grav $grav, Config $config, $name)
  26. {
  27. parent::__construct($name, $grav, $config);
  28. }
  29. /**
  30. * Get configuration of the plugin.
  31. *
  32. * @return array
  33. */
  34. public function config()
  35. {
  36. return $this->config["themes.{$this->name}"] ?? [];
  37. }
  38. /**
  39. * Persists to disk the theme parameters currently stored in the Grav Config object
  40. *
  41. * @param string $name The name of the theme whose config it should store.
  42. * @return bool
  43. */
  44. public static function saveConfig($name)
  45. {
  46. if (!$name) {
  47. return false;
  48. }
  49. $grav = Grav::instance();
  50. /** @var UniformResourceLocator $locator */
  51. $locator = $grav['locator'];
  52. $filename = 'config://themes/' . $name . '.yaml';
  53. $file = YamlFile::instance((string)$locator->findResource($filename, true, true));
  54. $content = $grav['config']->get('themes.' . $name);
  55. $file->save($content);
  56. $file->free();
  57. unset($file);
  58. return true;
  59. }
  60. /**
  61. * Load blueprints.
  62. *
  63. * @return void
  64. */
  65. protected function loadBlueprint()
  66. {
  67. if (!$this->blueprint) {
  68. $grav = Grav::instance();
  69. /** @var Themes $themes */
  70. $themes = $grav['themes'];
  71. $data = $themes->get($this->name);
  72. \assert($data !== null);
  73. $this->blueprint = $data->blueprints();
  74. }
  75. }
  76. }