Theme.php 2.2 KB

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