CompiledConfig.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @package Grav\Common\Config
  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\Config;
  9. use Grav\Common\File\CompiledYamlFile;
  10. class CompiledConfig extends CompiledBase
  11. {
  12. /**
  13. * @var callable Blueprints loader.
  14. */
  15. protected $callable;
  16. /**
  17. * @var bool
  18. */
  19. protected $withDefaults;
  20. public function __construct($cacheFolder, array $files, $path)
  21. {
  22. parent::__construct($cacheFolder, $files, $path);
  23. $this->version = 1;
  24. }
  25. /**
  26. * Set blueprints for the configuration.
  27. *
  28. * @param callable $blueprints
  29. * @return $this
  30. */
  31. public function setBlueprints(callable $blueprints)
  32. {
  33. $this->callable = $blueprints;
  34. return $this;
  35. }
  36. /**
  37. * @param bool $withDefaults
  38. * @return mixed
  39. */
  40. public function load($withDefaults = false)
  41. {
  42. $this->withDefaults = $withDefaults;
  43. return parent::load();
  44. }
  45. /**
  46. * Create configuration object.
  47. *
  48. * @param array $data
  49. */
  50. protected function createObject(array $data = [])
  51. {
  52. if ($this->withDefaults && empty($data) && \is_callable($this->callable)) {
  53. $blueprints = $this->callable;
  54. $data = $blueprints()->getDefaults();
  55. }
  56. $this->object = new Config($data, $this->callable);
  57. }
  58. /**
  59. * Finalize configuration object.
  60. */
  61. protected function finalizeObject()
  62. {
  63. $this->object->checksum($this->checksum());
  64. $this->object->timestamp($this->timestamp());
  65. }
  66. /**
  67. * Function gets called when cached configuration is saved.
  68. */
  69. public function modified()
  70. {
  71. $this->object->modified(true);
  72. }
  73. /**
  74. * Load single configuration file and append it to the correct position.
  75. *
  76. * @param string $name Name of the position.
  77. * @param string $filename File to be loaded.
  78. */
  79. protected function loadFile($name, $filename)
  80. {
  81. $file = CompiledYamlFile::instance($filename);
  82. $this->object->join($name, $file->content(), '/');
  83. $file->free();
  84. }
  85. }