CompiledBlueprints.php 2.6 KB

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