CompiledBlueprints.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @package Grav\Common\Config
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 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\Data\Blueprint;
  10. use Grav\Common\Data\BlueprintSchema;
  11. use Grav\Common\Grav;
  12. /**
  13. * Class CompiledBlueprints
  14. * @package Grav\Common\Config
  15. */
  16. class CompiledBlueprints extends CompiledBase
  17. {
  18. /**
  19. * CompiledBlueprints constructor.
  20. * @param string $cacheFolder
  21. * @param array $files
  22. * @param string $path
  23. */
  24. public function __construct($cacheFolder, array $files, $path)
  25. {
  26. parent::__construct($cacheFolder, $files, $path);
  27. $this->version = 2;
  28. }
  29. /**
  30. * Returns checksum from the configuration files.
  31. *
  32. * You can set $this->checksum = false to disable this check.
  33. *
  34. * @return bool|string
  35. */
  36. public function checksum()
  37. {
  38. if (null === $this->checksum) {
  39. $this->checksum = md5(json_encode($this->files) . json_encode($this->getTypes()) . $this->version);
  40. }
  41. return $this->checksum;
  42. }
  43. /**
  44. * Create configuration object.
  45. *
  46. * @param array $data
  47. */
  48. protected function createObject(array $data = [])
  49. {
  50. $this->object = (new BlueprintSchema($data))->setTypes($this->getTypes());
  51. }
  52. /**
  53. * Get list of form field types.
  54. *
  55. * @return array
  56. */
  57. protected function getTypes()
  58. {
  59. return Grav::instance()['plugins']->formFieldTypes ?: [];
  60. }
  61. /**
  62. * Finalize configuration object.
  63. *
  64. * @return void
  65. */
  66. protected function finalizeObject()
  67. {
  68. }
  69. /**
  70. * Load single configuration file and append it to the correct position.
  71. *
  72. * @param string $name Name of the position.
  73. * @param array $files Files to be loaded.
  74. * @return void
  75. */
  76. protected function loadFile($name, $files)
  77. {
  78. // Load blueprint file.
  79. $blueprint = new Blueprint($files);
  80. $this->object->embed($name, $blueprint->load()->toArray(), '/', true);
  81. }
  82. /**
  83. * Load and join all configuration files.
  84. *
  85. * @return bool
  86. * @internal
  87. */
  88. protected function loadFiles()
  89. {
  90. $this->createObject();
  91. // Convert file list into parent list.
  92. $list = [];
  93. /** @var array $files */
  94. foreach ($this->files as $files) {
  95. foreach ($files as $name => $item) {
  96. $list[$name][] = $this->path . $item['file'];
  97. }
  98. }
  99. // Load files.
  100. foreach ($list as $name => $files) {
  101. $this->loadFile($name, $files);
  102. }
  103. $this->finalizeObject();
  104. return true;
  105. }
  106. /**
  107. * @return array
  108. */
  109. protected function getState()
  110. {
  111. return $this->object->getState();
  112. }
  113. }