Theme.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 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\Page;
  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(Page $page, $deep = 'merge', $params = [], $type = 'themes') {
  59. return parent::mergeConfig($page, $deep, $params, $type);
  60. }
  61. /**
  62. * Simpler getter for the theme blueprint
  63. *
  64. * @return mixed
  65. */
  66. public function getBlueprint()
  67. {
  68. if (!$this->blueprint) {
  69. $this->loadBlueprint();
  70. }
  71. return $this->blueprint;
  72. }
  73. /**
  74. * Load blueprints.
  75. */
  76. protected function loadBlueprint()
  77. {
  78. if (!$this->blueprint) {
  79. $grav = Grav::instance();
  80. $themes = $grav['themes'];
  81. $this->blueprint = $themes->get($this->name)->blueprints();
  82. }
  83. }
  84. }