CompiledBlueprints.php 2.6 KB

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